Send email using python

In this post, we’ll discuss how to enable your application to send email using Python.

At SocketLabs, we recently built a new Python library for sending email messages through our send email API (known as our Injection API at SocketLabs). This library enables you to use Python to send email without an SMTP server. You can build and send any type of message supported by our API, from a simple message to a single recipient all the way to a complex bulk message sent to a group of recipients with unique merge data per recipient.

Your application must be written in Python 3 to send email using our Python library.

In building this Python package we decided to support Python version 3.4 and up. Here are our top reasons for supporting Python 3+ and not 2.7:

  • Strings are Unicode by default
  • Unicode/bytes separation
  • Enum support
  • Function annotations
  • Exception chaining
  • Syntax for keyword-only arguments
  • Extended unpacking
  • Non-local variable declarations

One of the biggest reasons is due to Unicode support. Almost all email clients offer support for Unicode, and most use it by default. Unicode, specifically UTF-8, offers the widest character set support and is the SocketLabs default character set.

Additionally, Python 2.7 has been identified as End Of Life and will not be maintained past 2020.

Try These Python Code Samples to Send Email

If you’re using an API for sending email, then the great thing about our Python send email library is that you can start sending by simply dropping our pre-written code samples into your Python application. In the sections below, you’ll find some of our most popular Python code examples.

Paste these code samples into your application to send email using Python

Note: To obtain your API Key and Server ID you’ll need to create a free developer account.

Basic Message
A basic message is an email message like you would send from a personal email client such as Outlook. A basic message can have many recipients, including multiple, To Addresses, CC Addresses, and even BCC Addresses. You can also send a file attachment in a basic message.

from socketlabs.injectionapi import SocketLabsClient
from socketlabs.injectionapi.message.basicmessage import BasicMessage
from socketlabs.injectionapi.message.emailaddress import EmailAddress

# Your SocketLabs ServerId and Injection API key
client = SocketLabsClient(10000, "YOUR-API-KEY");

message = BasicMessage()

message.subject = "Sending A BasicMessage"
message.html_body = "This is the Html Body of my message."
message.plain_text_body = "This is the Plain Text Body of my message.";

message.from_email_address = EmailAddress("[email protected]")

# A basic message supports up to 50 recipients 
# and supports several different ways to add recipients

# Add a To address by passing the email address
message.to_email_address.append(EmailAddress("[email protected]"))
message.to_email_address.append(EmailAddress("[email protected]", "Recipient #2"))

# // Adding CC Recipients
message.add_cc_email_address("[email protected]")
message.add_cc_email_address("[email protected]", "Recipient #4")

# Adding Bcc Recipients
message.add_bcc_email_address(EmailAddress("[email protected]"))
message.add_bcc_email_address(EmailAddress("[email protected]", "Recipient #6"))

response = client.send(message)

Bulk Message
A bulk message usually contains a single recipient per message and is generally used to send the same content to many recipients, optionally customizing the message via the use of MergeData.

from socketlabs.injectionapi import SocketLabsClient
from socketlabs.injectionapi.message.bulkmessage import BulkMessage
from socketlabs.injectionapi.message.bulkrecipient import BulkRecipient
from socketlabs.injectionapi.message.emailaddress import EmailAddress

# Your SocketLabs ServerId and Injection API key
client = SocketLabsClient(10000, "YOUR-API-KEY");

message = BulkMessage()

message.plain_text_body = "This is the body of my message sent to %%Name%%"
message.html_body = "This is the HtmlBody of my message sent to %%Name%%"
message.subject = tests
message.from_email_address = EmailAddress("[email protected]")

recipient1 = BulkRecipient("[email protected]")
recipient1.add_merge_data("Name", "Recipient1")
message.add_to_recipient(recipient1)

recipient2 = BulkRecipient("[email protected]", "Recipient #2")
recipient2.add_merge_data("Name", "Recipient2")
message.add_to_recipient(recipient2)

response = client.send(message)

To learn more about sending email using Python, visit our Python Library Page.

For more examples and use cases check out our GitHub page.

If you’d like to learn more about our full suite of Email APIs to help you send, track, and parse email from your application, then visit our Developer Hub.