Skip to content

Instantly share code, notes, and snippets.

@tedheich
Created October 22, 2009 08:21
Show Gist options
  • Save tedheich/215824 to your computer and use it in GitHub Desktop.
Save tedheich/215824 to your computer and use it in GitHub Desktop.
Simple python snippet to send mail
# 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 = 'ted@fornoobs.info' # Who is this email coming from
strTo = 'whoareYou@sendingIt.to' # 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