Ruan Bekker's Blog

From a Curious mind to Posts on Github

Send Emails Using Python and Sendgrid Using SMTPlib

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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:

1
Received: from xx.xx.s2shared.sendgrid.net (xx.xx.s2shared.sendgrid.net. [xx.xx.xx.xx])

Resources:

Great post on SSL / TLS: - https://stackabuse.com/how-to-send-emails-with-gmail-using-python/

Enjoy :D