Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2017
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. from smtplib import SMTP # Use this for standard SMTP protocol (port 25, no encryption)
  2. # from smtplib import SMTP_SSL as SMTP # This invokes the secure SMTP protocol (port 465, uses SSL)
  3. from email.mime.text import MIMEText
  4.  
  5.  
  6. class Email(object):
  7. SMTP_CONFIG = dict(
  8. server="your_smtp_server_hostname",
  9. username="your_smtp_server_username",
  10. password="your_smtp_server_password"
  11. )
  12.  
  13. # typical values for text_subtype are plain, html, xml
  14. DEFAULT_CONTENT_TYPE = "plain"
  15. DEFAULT_SENDER = "john.doe@example.com"
  16.  
  17. def __init__(self, SMTP_CONFIG=None, debug=False):
  18. if not SMTP_CONFIG:
  19. SMTP_CONFIG = self.SMTP_CONFIG
  20.  
  21. self.connection = SMTP(SMTP_CONFIG["server"])
  22. self.connection.set_debuglevel(debug)
  23. self.connection.login(SMTP_CONFIG["username"], SMTP_CONFIG["password"])
  24.  
  25. def send(self, subject, message, receivers, sender=None, content_type=None):
  26. if not content_type:
  27. content_type = self.DEFAULT_CONTENT_TYPE
  28.  
  29. if not sender:
  30. sender = self.DEFAULT_SENDER
  31.  
  32. msg = MIMEText(message, content_type)
  33. msg["Subject"] = subject
  34. msg["From"] = sender
  35. self.connection.sendmail(sender, receivers, msg.as_string())
  36.  
  37.  
  38. if __name__ == '__main__':
  39. email = Email()
  40. email.send("Test", "Test email", ["someone.i.know@gmail.com"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement