We’ll start with a simple example using the built-in Python SMTP libraries to send a message.
After that example, you can find more complete instructions that use the SendGrid Python library.
Using Python’s SMTP Library
This Python program will build a multi-part MIME email and send it through SendGrid.
Python has built in libraries to send and receive emails. This example uses:
#! /usr/bin/pythonimportsmtplibfromemail.mime.multipartimportMIMEMultipartfromemail.mime.textimportMIMEText# Your From email addressfromEmail="you@yourdomain.com"# RecipienttoEmail="person@youaremailing.com"# Create message container - the correct MIME type is multipart/alternative.msg=MIMEMultipart('alternative')msg['Subject']="Example Python Email"msg['From']=fromEmailmsg['To']=toEmail# Create the body of the message (a plain-text and an HTML version).# text is your plain-text email# html is your html version of the email# if the reciever is able to view html emails then only the html# email will be displayedtext="Hi!\nHow are you?\n"html="""\n<html> <head></head> <body> Hi!<br> How are you?<br> </body></html>"""# Login credentialsusername='yourlogin'password="yourpassword"# Record the MIME types of both parts - text/plain and text/html.part1=MIMEText(text,'plain')part2=MIMEText(html,'html')# Attach parts into message container.msg.attach(part1)msg.attach(part2)# Open a connection to the SendGrid mail servers=smtplib.SMTP('smtp.sendgrid.net',587)# Authenticates.login(username,password)# sendmail function takes 3 arguments: sender's address, recipient's address# and message to send - here it is sent as one string.s.sendmail(fromEmail,toEmail,msg.as_string())s.quit()
Using the SendGrid Python Library
This library allows you to quickly and easily send emails through SendGrid using Python.
SendGrid provides two methods of sending email: the Web API, and SMTP
API. SendGrid recommends using the SMTP API for sending emails, but the
Web API has less communication overhead. Read more about SMTP vs Web
API.
This library implements a common interface to make it very easy to use either API.
Mail Pre-Usage
Before we begin using the library, its important to understand a few things about the library architecture:
Sending an email is as simple as :
* Creating a SendGrid Instance
* Creating a SendGrid Mail object, and setting its data
* Sending the mail using either SMTP API or Web API
importsendgrid# make a secure connection to SendGrids=sendgrid.Sendgrid('username','password',secure=True)# make a message objectmessage=sendgrid.Message("from@mydomain.com","message subject","plaintext message body","HTML message body")# add a recipientmessage.add_to("someone@example.com","John Doe")# use the Web API to send your messages.web.send(message)
Or change the last line to use the SMTP API instead:
Using the message.addto() method, you can add recipient email address (optionally with names), but you can also add CC/BCC recipient addresses (without names) using message.add_cc() and message.add_bcc().
Note: Only the SMTP API supports CC at this time, though we have code and hooks in place for the Web API implementation of this library for future use.
Both the Web API and SMTP API support BCC.
The message.add_cc() and message.add_bcc() calls support passing a single address, or a list of addresses, as shown in the examples below.
message=sendgrid.Message("from@mydomain.com","message subject","plaintext message body","HTML message body")# add a To: recipient with a namemessage.add_to("someone1@example.com","John Doe")# add a To: recipient without a namemessage.add_to("someone8@example.com")# add several To: recipients without namesmessage.add_to(["someone9@example.com","someone10@example.com"])# add several To: recipients without names# note: count of assigned names must match the count of email addressesmessage.add_to(["someone11@example.com","someone12@example.com"],["John Smith","Jane Smith"])# add a single CC: recipient by passing a string, SMTP API onlymessage.add_cc("someone2@example.com")# add several CC: recipients by passing a list, SMTP API onlymessage.add_cc(["someone3@example.com","someone4@example.com"])# add a single BCC: recipient by passing a stringmessage.add_bcc("someone5@example.com")# add several BCC: recipients by passing a listmessage.add_bcc(["someone6@example.com","someone7@example.com"])# send message to To, CC and BCC recipients using SMTP APIs.smtp.send(message)# send message to To and BCC recipients using Web API (no CC support)s.web.send(message)
Note: Using CC/BCC with SMTP API requires v0.1.3 or higher
Using Attachments
Attaching files to your message uses the message.add_attachment() method. This method takes two parameters, the intended name of the attachment you want your recipients to see, and the full file system path to the file. Note: File attachments are limited to 7 MB per file, and your total message size must be under 20MB.
A common problem with file attachments is that the second parameter does not exist as a file, in which case the library will attach a 0-byte blank file. Here’s a simple check to assist you:
An optional third parameter can be passed to the message.add_attachment() call which lets you specify a Content-ID header for each file. The Content-ID is used to reference attached files (typically images) within the HTML message. For example:
message=sendgrid.Message("from@mydomain.com","message subject","I have attached my picture, I hope you like it","Here is my inline picture<br><img src=\"cid:picture1\"><br>I hope you like it.")message.add_to("someone@example.com","John Doe")message.add_attachment("my_picture.png","/path/to/my_picture.png","picture1")s.web.send(message)
Using Categories
You can mark messages with optional categories to give better
visibility to email statistics (opens, clicks, etc.). You can add up to
10 categories per email message. Read more about Categories.
To add categories to your message, use the message.add_category() method and pass a list of one or more category names. SendGrid will begin tracking statistics with these category names if the category name is new, or aggregate statistics for existing category names.
Unique Arguments are used for tracking purposes on the message, and
can be seen in the Email Activity screen on your account dashboard or
through the Event API. Use the message.add_unique_argument() method,
which takes two parameters, a key and a value. To pass multiple
keys/values, use message.set_unique_arguments() (note the plural method
name) and pass a dictionary of key/value pairs. More about Unique
Arguments.
message=sendgrid.Message("from@mydomain.com","subject","plain body","<b>Html here</b>")# set 'Customer' to a value of 'Someone'message.add_unique_argument("Customer","Someone")
Alternately, you can pass a dict parameter and add multiple arguments using message.add_unique_arguments() like this:
message=sendgrid.Message("from@mydomain.com","subject","plain body","<b>Html here</b>")# set multiple unique arguments for a messagemessage.set_unique_arguments({"customerAccountNumber":"55555","activationAttempt":"1"})
Using Substitutions
SendGrid also allows you to send multi-recipient messages with unique
information per recipient. This is commonly used for sending unique URLs
or codes to a list of recipients in a single batch. You simply expand
the data you pass to the message.add_to() method like the example below.
Read more about Substitutions.
message=sendgrid.Message("from@mydomain.com","subject","Hello %name%, your code is %code%","<b>Hello %name%, your code is %code%</b>")message.add_to({'example1@example.com':{'%name%':'Name 1','%code%':'Code 1'},'example2@example.com':{'%name%':'Name 2','%code%':'Code 2'},})
Using Sections
Used in conjunction with Substitutions, Sections can be used to
further customize messages for the end users, and acts like a second
tier of substitution data. You can use message.set_section() to add a
single section, or a pluralized message.set_sections() method to add
several sections. More about Sections.
message=sendgrid.Message("from@mydomain.com","subject","Hello %name%, you work at %place%","<b>Hello %name%, you work at %place%</b>")message.add_to({'example1@example.com':{'%name%':'Name 1','%place%':'%home%'},'example2@example.com':{'%name%':'Name 2','%place%':'%office%'},'example3@example.com':{'%name%':'Name 3','%place%':'%office%'},}).set_sections({"%office%":"an office","%home%":"your house"})
Using Custom Headers
Custom SMTP headers can be added as necessary using the message.add_header() method.
message=sendgrid.Message("from@mydomain.com","subject","plain body","<b>Html here</b>")message.add_filter_setting("footer","text/plain","Here is a plain text footer")message.add_filter_setting("footer","text/html","<p style='color:red;'>Here is an HTML footer")