Skip to content

Instantly share code, notes, and snippets.

@imtapps
Created September 11, 2014 19:19
Show Gist options
  • Save imtapps/e6bafe8298ca94b082e7 to your computer and use it in GitHub Desktop.
Save imtapps/e6bafe8298ca94b082e7 to your computer and use it in GitHub Desktop.
Python email example
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
sender = "[email protected]"
sender_password = 'someones-password'
receiver = "[email protected]"
# Create message container
msg = MIMEMultipart('alternative')
msg['Subject'] = "This is a python email"
msg['From'] = sender
msg['To'] = receiver
# Create the body of the message (a plain-text and an HTML version).
text = "Hello!\nThis is an email from a python program\n\nThanks,\nJarrod"
html = """\
<html>
<head></head>
<body>
<p>
Hello!<br/>
This is an email from a python program<br><br>
Thanks,<br>
Jarrod
</p>
</body>
</html>
"""
plain_text_message = MIMEText(text, 'plain')
html_message = MIMEText(html, 'html')
msg.attach(plain_text_message)
msg.attach(html_message)
# Send the message via the gmail server. Update as needed
s = SMTP('smtp.gmail.com:587')
s.starttls()
s.login(sender, sender_password)
s.sendmail(sender, receiver, msg.as_string())
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment