hjaltiatlason

SendMail-SMTP-Python_script-Template

Jun 3rd, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. SMTPserver = 'hjalti-me.mail.protection.outlook.com'
  4. sender =     'sender@example.com'
  5. destination = ['destination@gmail.com']
  6.  
  7. #USERNAME = "username"
  8. #PASSWORD = "password"
  9.  
  10. # typical values for text_subtype are plain, html, xml
  11. text_subtype = 'plain'
  12.  
  13.  
  14. content="""\
  15. Test message
  16. """
  17.  
  18. subject="Sent from Python"
  19.  
  20. import sys
  21. import os
  22. import re
  23.  
  24. #from smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)
  25. from smtplib import SMTP                  # use this for standard SMTP protocol   (port 25, no encryption)
  26.  
  27. # old version
  28. # from email.MIMEText import MIMEText
  29. from email.mime.text import MIMEText
  30.  
  31. try:
  32.     msg = MIMEText(content, text_subtype)
  33.     msg['Subject']=       subject
  34.     msg['From']   = sender # some SMTP servers will do this automatically, not all
  35.  
  36.     conn = SMTP(SMTPserver)
  37.     conn.set_debuglevel(False)
  38.     #conn.login(USERNAME, PASSWORD)
  39.     try:
  40.         conn.sendmail(sender, destination, msg.as_string())
  41.     finally:
  42.         conn.quit()
  43.  
  44. except:
  45.     sys.exit( "mail failed; %s" % "CUSTOM_ERROR" ) # give an error message
Add Comment
Please, Sign In to add comment