Last active
January 15, 2022 18:57
-
-
Save julien-h2/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`.
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
# 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 = '[email protected]' | |
SMTP_PASSWORD = 'mysecretpassword' | |
# Here goes your email data | |
# Usually, SENDER_EMAIL must match SMTP_LOGIN | |
SENDER_NAME = 'Linus T.' | |
SENDER_EMAIL = '[email protected]' | |
RECIPIENT = '[email protected]' | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the gist.
There is a little typo in line 36:
SENDER_EMAIl => SENDER_EMAIL