Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import email
  2. import smtplib
  3. import mimetypes
  4. from email.MIMEText import MIMEText
  5. from email.MIMEMultipart import MIMEMultipart
  6. from email.MIMEBase import MIMEBase
  7. from email import encoders
  8.  
  9. from_addr = 'ms015a.2014@gmail.com'
  10. to_addr = 'ms015a.2014@gmail.com'
  11. subject_header = 'Subject: Testing SMTP'
  12. attachment = 'disk_report.pdf'
  13. body = '''
  14.    This message sends a PDF attachment '''
  15.  
  16. username = 'ms015a.2014'
  17. password = 'pythoniscool'
  18. m = MIMEMultipart()
  19. m["To"] = to_addr
  20. m["From"] = from_addr
  21. m["Subject"] = subject_header
  22.  
  23. ctype, encoding = mimetypes.guess_type(attachment)
  24. print ctype, encoding
  25. maintype, subtype = ctype.split('/', 1)
  26. print maintype, subtype
  27.  
  28. m.attach(MIMEText(body))
  29. pdffile = open(attachment, "rb")
  30. msg = MIMEBase(maintype, subtype)
  31. msg.set_payload(fp.read())
  32. pdffile.close()
  33. encoders.encode_base64(msg)
  34. msg.add_header("Content-Disposition", "attachment", filename=attachment)
  35. m.attach(msg)
  36.  
  37. server = smtplib.SMTP('smtp.gmail.com:587')
  38.  
  39. server.starttls()
  40. server.login(username, password)
  41. server.sendmail(fromaddr, toaddr, m.as_string())
  42. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement