Skip to content

Instantly share code, notes, and snippets.

@julien-h julien-h/send-email.py
Last active Dec 8, 2018

Embed
What would you like to do?
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.