Guest User

Untitled

a guest
Aug 21st, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. How to clear all data in MIMEBase (email module)
  2. import smtplib
  3. from email.MIMEMultipart import MIMEMultipart
  4. from email.MIMEBase import MIMEBase
  5. from email.MIMEText import MIMEText
  6. from email.Utils import COMMASPACE, formatdate
  7. from email import Encoders
  8. import os
  9.  
  10. def sendMail(to, subject, text, files=[],server="smtp.gmail.com:587"):
  11. assert type(to)==list
  12. assert type(files)==list
  13. fro = "psaoflamand@live.com>"
  14.  
  15. msg = MIMEMultipart()
  16. msg['From'] = fro
  17. msg['To'] = COMMASPACE.join(to)
  18. msg['Date'] = formatdate(localtime=True)
  19. msg['Subject'] = subject
  20.  
  21. msg.attach( MIMEText(text) )
  22. a=0
  23. username = 'psaoflamand@gmail.com'
  24. password = 'pass'
  25.  
  26. # The actual mail send
  27.  
  28.  
  29. smtp = smtplib.SMTP(server)
  30. smtp.starttls()
  31. smtp.login(username,password)
  32.  
  33. for file in files:
  34. a+=1
  35. print a
  36. part = MIMEBase('application', "octet-stream")
  37. part.set_payload( open(file,"rb").read() )
  38. Encoders.encode_base64(part)
  39. part.add_header('Content-Disposition', 'attachment; filename="%s"'
  40. % os.path.basename(file))
  41. msg.attach(part)
  42. if a==21:
  43. smtp.sendmail(fro, to, msg.as_string() )
  44. a=0
  45. print 'sent'
  46.  
  47.  
  48. smtp.quit()
  49.  
  50.  
  51. sendMail(
  52. ["psaoflamand@live.com"],
  53. "hello","cheers",
  54. ["Thousands of one megabyte files"]
Add Comment
Please, Sign In to add comment