Skip to content

Instantly share code, notes, and snippets.

@julien-h
Last active January 15, 2022 18:57
Show Gist options
  • Save julien-h/6bf7bde361b1dde2aef285070eb8ae46 to your computer and use it in GitHub Desktop.
Save julien-h/6bf7bde361b1dde2aef285070eb8ae46 to your computer and use it in GitHub Desktop.
Template to send emails using the python-emails library. Install it with `pip install emails`.
# Sending emails from python.
# Template provided by https://Scripting.Tips
# You may freely reuse, modify and share this piece of code
import emails
# Here goes the configuration of your email provider.
# Look online to find it.
# For instance for Yahoo!Mail:
SMTP_SERVER = 'smtp.mail.yahoo.com'
SMTP_LOGIN = 'linus.t@yahoo.com'
SMTP_PASSWORD = 'mysecretpassword'
# Here goes your email data
# Usually, SENDER_EMAIL must match SMTP_LOGIN
SENDER_NAME = 'Linus T.'
SENDER_EMAIL = 'linus.t@yahoo.com'
RECIPIENT = 'james@gmail.com'
SUBJECT = 'Learn how to automate emails!'
CONTENT = """
<p>
Dear James, <br>
I found out about an awesome website
to learn scripting and though you'd be
interesed. Check it out:
https://scripting.tips
</p>
"""
#Note: you can use HTML to format the content
message = emails.html(
subject=SUBJECT,
html=CONTENT,
mail_from=(SENDER_NAME, SENDER_EMAIl)
)
# If you want to attach an image of a file, use this code:
# message.attach(
# data=open('screenshot.jpg', 'rb'),
# filename='screenshot.jpg'
# )
config = {
'host': SMTP_SERVER,
'timeout': 5,
'ssl': True,
'user': SMTP_LOGIN,
'password': SMTP_PASSWORD
}
r = message.send(to=RECIPIENT, smtp=config)
if r.success:
print('Mail sent!')
else:
print('Something wrong happened')
print('Here is the smtp status code', r.status_code)
@appcoders
Copy link

Thanks for the gist.

There is a little typo in line 36:

SENDER_EMAIl => SENDER_EMAIL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment