Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. sender = "from@gmail.com"
  2. to_address = ["to@gmail.com"]
  3. subject = "Test subject"
  4. body = "Test body"
  5. title = "title"
  6.  
  7. attach_files = ['c:\dummy.pdf']
  8.  
  9. host = 'localhost.com'
  10. user = 'user@mail.com'
  11. password = 'password'
  12.  
  13. msg_root = MIMEMultipart('related')
  14. addr = '{} <{}>'.format(title, sender)
  15. name, address = parseaddr(addr)
  16. msg_root['From'] = formataddr((
  17. Header(name, 'utf-8').encode(),
  18. addr.encode('utf-8') if isinstance(addr, unicode) else addr))
  19.  
  20. msg_root['To'] = ','.join(to_address)
  21. msg_root['Subject'] = Header(subject, 'utf-8')
  22. msg_text = MIMEText(body, 'html', 'utf-8')
  23. msg_root.attach(msg_text)
  24.  
  25. for index, attach_file in enumerate(attach_files, start=1):
  26. with open(attach_file, 'rb') as attach_obj:
  27. attach = MIMEApplication(attach_obj.read(),
  28. _subtype="pdf",
  29. name=os.path.basename(attach_file))
  30. attach.add_header('Content-Disposition', 'attachment',
  31. filename=os.path.basename(attach_file))
  32. msg_root.attach(attach)
  33.  
  34. connection = smtplib.SMTP_SSL(host=host, timeout=5)
  35. try:
  36. connection.login(user=user, password=password)
  37. connection.sendmail(user, all_address, msg_root.as_string())
  38. finally:
  39. connection.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement