- Published on
Send Emails using Python and Sendgrid using SMTPlib
- Authors
- Name
- Ruan Bekker
- @ruanbekker
Quick tutorial on how to send emails using Python and smtplib.
Sendgrid
Sendgrid offers 100 free outbound emails per day, sign up with them via sendgrid.com/free, create a API Key and save your credentials in a safe place.
You first need to verify your account by sending a mail using their API, but it's step by step so won't take more than 2 minutes to complete.
Python Code
Once the verification is completed, our Python Code:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
mail_from = 'Ruan Bekker <ruan@ruanbekker.com>'
mail_to = 'Ruan Bekker <xxxx@gmail.com>'
msg = MIMEMultipart()
msg['From'] = mail_from
msg['To'] = mail_to
msg['Subject'] = 'Sending mails with Python'
mail_body = """
Hey,
This is a test.
Regards,\nRuan
"""
msg.attach(MIMEText(mail_body))
try:
server = smtplib.SMTP_SSL('smtp.sendgrid.net', 465)
server.ehlo()
server.login('apikey', 'your-api-key')
server.sendmail(mail_from, mail_to, msg.as_string())
server.close()
print("mail sent")
except:
print("issue")
When I ran the code, I received the mail, and when you inspect the headers you can see that the mail came via sendgrid:
Received: from xx.xx.s2shared.sendgrid.net (xx.xx.s2shared.sendgrid.net. [xx.xx.xx.xx])
Resources:
Great post on SSL / TLS:
Enjoy :D
Thank You
Thanks for reading, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon