Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Peter Tsiampas
Added: Apr 15, 2018 12:17 PM
Views: 14
Tags: email
Here is a Python example of how you can send email to someone.
  1. import smtplib, email.mime.text
  2.  
  3. me = "joe@my.org.com"                # Put your own email here
  4. fred = "fred@his.org.com"            # And fred's email address here
  5. your_mail_server = "mail.my.org.com" # Ask your system administrator
  6.  
  7. # Create a text message containing the body of the email.
  8. # You could read this from a file, of course.
  9. msg = email.mime.text.MIMEText("""Hey Fred,
  10.  
  11. I'm having a party, please come at 8pm.
  12. Bring a plate of snacks and your own drinks.
  13.  
  14. Joe""" )
  15.  
  16. msg["From"] = me               # Add headers to the message object
  17. msg["To"] = fred
  18. msg["Subject"] = "Party on Saturday 23rd"
  19.  
  20. # Create a connection to your mail server
  21. svr = smtplib.SMTP(your_mail_server)
  22. response = svr.sendmail(me, fred, msg.as_string()) # Send message
  23. if response != {}:
  24.     print("Sending failed for ", response)
  25. else:
  26.     print("Message sent.")
  27.  
  28. svr.quit()                                   # Close the connection