Skip to content

Instantly share code, notes, and snippets.

@rayansostenes
Created February 12, 2019 14:27
Show Gist options
  • Save rayansostenes/0eefda4cf17f86d50e4df2040bc2a7b5 to your computer and use it in GitHub Desktop.
Save rayansostenes/0eefda4cf17f86d50e4df2040bc2a7b5 to your computer and use it in GitHub Desktop.
Sending e-mail with python
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
password = "your_password"
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email
text = """\
Hi,
How are you?
Real Python has many great tutorials:
www.realpython.com"""
html = """\
<html>
<body>
<p>Hi,<br>
How are you?<br>
<a href="http://www.realpython.com">Real Python</a>
has many great tutorials.
</p>
</body>
</html>
"""
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment