Created
October 22, 2009 08:21
-
-
Save tedheich/215824 to your computer and use it in GitHub Desktop.
Simple python snippet to send mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Send an HTML email with an embedded image and a plain text message for | |
# email clients that don't want to display the HTML. | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
from email.MIMEImage import MIMEImage | |
import smtplib | |
# Define these once; use them twice! | |
strFrom = '[email protected]' # Who is this email coming from | |
strTo = '[email protected]' # who are you sending it to | |
# Create the root message and fill in the from, to, and subject headers | |
msgRoot = MIMEMultipart('related') | |
msgRoot['Subject'] = 'test message' | |
msgRoot['From'] = strFrom | |
msgRoot['To'] = strTo | |
msgRoot.preamble = 'This is a multi-part message in MIME format.' | |
# Encapsulate the plain and HTML versions of the message body in an | |
# 'alternative' part, so message agents can decide which they want to display. | |
msgAlternative = MIMEMultipart('alternative') | |
msgRoot.attach(msgAlternative) | |
msgText = MIMEText('This is the alternative plain text message.') | |
msgAlternative.attach(msgText) | |
# Send the email (this example assumes SMTP authentication is required) | |
smtp = smtplib.SMTP() | |
smtp.connect('domainnameOrIpOfYourMailHost') # Your smtp server | |
smtp.login('Your email login', 'yourPassword') # If you were to login to your mail server, this would be it | |
smtp.sendmail(strFrom, strTo, msgRoot.as_string()) | |
smtp.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment