Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. from __future__ import unicode_literals, print_function
  4. import smtplib
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.base import MIMEBase
  7. from email.mime.text import MIMEText
  8. from email.utils import formatdate
  9. from email import encoders
  10.  
  11.  
  12. def send_mail(send_from, send_to, subject, text, filename):
  13. '''
  14. send_from: str
  15. send_to: list
  16. subject: str
  17. text: str
  18. filename: file path
  19. '''
  20.  
  21. msg = MIMEMultipart()
  22. msg['From'] = send_from
  23. msg['To'] = ', '.join(send_to)
  24. msg['Date'] = formatdate(localtime=True)
  25. msg['Subject'] = subject
  26. msg.attach(MIMEText(text, _subtype='plain', _charset='utf8'))
  27. # html = '<html><head><title>test</title></head><body>hello</body></html>'
  28. # msg.attach(MIMEText(html, _subtype='html', _charset='utf8'))
  29. part = MIMEBase('application', 'octet-stream')
  30. part.set_payload(open(filename, 'rb').read())
  31. encoders.encode_base64(part)
  32. part.add_header('Content-Disposition', 'attachment; filename="{}"'.format(filename))
  33. msg.attach(part)
  34.  
  35. # FIXME: using you smtp server and login information
  36. server = ''
  37. port = 587
  38. username = ''
  39. password = ''
  40. conn = get_smtp_conn(server, port, username, password)
  41. conn.sendmail(send_from, send_to, msg.as_string())
  42. conn.quit()
  43.  
  44.  
  45. def get_smtp_conn(server, port, username, password, isTls=True):
  46. smtp = smtplib.SMTP(server, port)
  47. if isTls:
  48. smtp.starttls()
  49. smtp.login(username, password)
  50. return smtp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement