Automating Email Sending with Python
Introduction
In today’s digital world, email is a crucial communication tool. Automating the process of sending emails can save significant time and effort. This article explains various methods of programmatically sending emails using Python, covering several services and their APIs.
Configuration Data
Before we start, it’s important to note that we’ll be using a JSON file to store sensitive data such as API keys, usernames, passwords, and the path to an example PDF file. This is a common practice to keep sensitive data separate from the code. Here’s an example of what this file might look like:
{ "pdf_path": "path_to_your_file.pdf", "smtp": { "email": "your_email", "password": "your_password" }, "outlook": { "email": "your_email", "password": "your_password" }, "gmail": { "token": "token.json" }, "sendgrid": { "api_key": "SENDGRID_API_KEY" }, "mailgun": { "domain": "your-domain.com", "api_key": "your-api-key" }, "getresponse": { "api_key": "your-api-key", "from_field_id": "from_field_id", "campaign_id": "campaign_id" }, "moosend": { "campaign_id": "campaign_id", "api_key": "your-api-key" }, "brevo": { "api_key": "your-api-key" }, "netcore": { "api_key": "your-api-key" }, "postmark": { "server_token": "your-server-token" }, "message_bird": { "access_key": "your-access-key" } } You should replace all placeholders with your actual data. Now, let’s import this configuration data into our Python script: import json with open('config.json') as f: config = json.load(f) |
Now, config is a dictionary containing all our sensitive data. We can access this data like so: config[‘smtp’][’email’], config[‘sendgrid’][‘api_key’], etc.
Sending an Email with Python
Python’s built-in smtplib library allows us to send emails using the Simple Mail Transfer Protocol (SMTP). Here’s a basic example:
import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(config['smtp']['email'], config['smtp']['password']) msg = "Hello, this is a test email!" server.sendmail(config['smtp']['email'], "recipient_email", msg) server.quit() |
In this code, we first establish a connection to the SMTP server. We then log in with our email and password, create a message, and send it. Finally, we terminate the connection.
Sending Email via Outlook Using PID Process
Outlook can be automated using the pywin32 library, which allows Python to call Microsoft COM objects. This method requires an active Outlook instance running on your machine.
import win32com.client as win32 outlook = win32.Dispatch('outlook.application') namespace = outlook.GetNamespace("MAPI") mail = outlook.CreateItem(0) mail.Subject = 'Test' mail.Body = 'Hello, this is a test email!' mail.To = 'recipient_email' mail.Send() import win32com.client as win32 outlook = win32.Dispatch('outlook.application') namespace = outlook.GetNamespace("MAPI") mail = outlook.CreateItem(0) mail.Subject = 'Test' mail.Body = 'Hello, this is a test email!' mail.To = 'recipient_email' mail.Send() |
Here, we create a Dispatch object for Outlook. We then create a new mail item, set the subject, body, and recipient of the email, and send it.
I like this method because you are fully independent from third party services. The weak point is that you cannot send to many emails otherwise later or sooner your email will go to spam lists.
Sending Email via Outlook Using API
Microsoft provides the Outlook Mail API that can be used to send emails. This requires registering an application in the Azure portal to get the necessary credentials.
import requests from O365 import Account credentials = (config['outlook']['client_id'], config['outlook']['client_secret']) account = Account(credentials) mailbox = account.mailbox() message = mailbox.new_message() message.to.add('recipient_email') message.subject = 'Hello' message.body = 'Hello, this is a test email!' message.send() |
In this code, we first set up our credentials. We then create a new message, set the recipient, subject, and body of the email, and send it. Depending if you have an access to azure cloud the method might me less or more comfortable to send email automatically.
Gmail API to Send Email
Google provides a powerful tool for email automation – the Gmail API. This service allows you to interact with Gmail programmatically, enabling you to send emails, read messages, and perform many other email-related tasks. To use the Gmail API, you’ll need to set up a project in the Google Cloud Console and obtain the necessary credentials.
Sending Emails via the Gmail API
Here’s an example of how to send an email using the Gmail API. This code sends a simple email which can be either plain text or HTML.
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials # Load credentials from file creds = Credentials.from_authorized_user_file(config['gmail']['token']) # Build the Gmail service service = build('gmail', 'v1', credentials=creds) # Define the email body body = { 'raw': 'SGVsbG8gd29ybGQ=' # 'Hello world' in base64url } # Send the email message = service.users().messages().send(userId='me', body=body).execute() |
In the example code, we first load our credentials from a file and use them to build the Gmail service. We then define the body of our email and send it. The body must be a base64url encoded string.
Sending Emails with Attachments via the Gmail API
Sending an email with an attachment involves creating a multipart email. Here’s an example of how to send an email with a PDF attachment using the Gmail API:
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders # Load credentials from file creds = Credentials.from_authorized_user_file(config['gmail']['token']) # Build the Gmail service service = build('gmail', 'v1', credentials=creds) # Create a multipart message and set headers message = MIMEMultipart() message['to'] = 'recipient@example.com' message['subject'] = 'subject' # Open PDF file in binary mode with open(config['gmail']['pdf_path'], 'rb') as f: pdf_file = f.read() # Add file as application/octet-stream mime_part = MIMEBase('application', 'octet-stream') mime_part.set_payload(pdf_file) # Encode file in ASCII characters encoders.encode_base64(mime_part) # Add header mime_part.add_header('Content-Disposition', 'attachment; filename=config["pdf_path"]') # Add MIME part to the message message.attach(mime_part) # Send the email service.users().messages().send(userId='me', body=message).execute() |
We create a multipart message, open our PDF file in binary mode using the path specified in our configuration file, and add it to the message as an octet-stream MIME part. We then encode the file in ASCII characters, add a header to the MIME part, and attach it to our message. Finally, we send the email.
SendGrid API to send email
SendGrid is a cloud-based email service that provides reliable transactional email delivery, scalability, and real-time analytics. It offers flexible APIs that make custom integration easy, making it a popular choice for sending emails programmatically.
Sending Emails via SendGrid API
Here’s an example of how to send an email using the SendGrid API:
import sendgrid from sendgrid.helpers.mail import Mail # Set up the SendGrid API client sg = sendgrid.SendGridAPIClient(api_key=config['sendgrid']['api_key']) # Create a new email email = Mail( from_email='from_email@example.com', to_emails='to@example.com', subject='Sending with SendGrid is Fun', plain_text_content='and easy to do anywhere, even with Python' ) # Send the email response = sg.send(email) |
The code presents how to set up the SendGrid API client using the API key from our configuration data. We then create a new email, specifying the sender’s email address, the recipient’s email address, the subject, and the content of the email. Finally, we send the email using the send
method of our SendGrid API client.
Sending Emails with Attachments via SendGrid API
To send an email with an attachment, you would need to create a FileContent instance for the attachment, and add it to the Mail instance. Here’s an example:
from sendgrid.helpers.mail import FileContent, FileName, FileType, Disposition, Attachment # Read the PDF file and encode it into base64 with open(config['gmail']['pdf_path'], 'rb') as f: pdf_data = f.read().encode('base64') # Create a FileContent instance file_content = FileContent(pdf_data) # Create other necessary instances file_type = FileType('application/pdf') file_name = FileName('example.pdf') disposition = Disposition('attachment') # Create an Attachment instance attachment = Attachment() attachment.file_content = file_content attachment.file_type = file_type attachment.file_name = file_name attachment.disposition = disposition # Add the attachment to the email email.add_attachment(attachment) # Send the email response = sg.send(email) |
In this code, we read the PDF file specified in our configuration data, encode it into base64, and create a FileContent instance. We then create a FileType instance for the MIME type of the file, a FileName instance for the name of the file, and a Disposition instance for the disposition of the file. We create an Attachment instance, set its properties, add it to the email, and send the email.
Mailgun API to send emails
Mailgun is a robust email service that offers an API for sending, receiving, and tracking emails. It’s favored by developers due to its scalability, reliability, and comprehensive analytics.
Sending Emails via Mailgun API
Here’s an example of how to send an email using the Mailgun API with Python:
import requests def send_simple_message(): return requests.post( "https://api.mailgun.net/v3/" + config['mailgun']['domain'] + "/messages", auth=("api", config['mailgun']['api_key']), data={"from": "Excited User <mailgun@" + config['mailgun']['domain'] + ">", "to": ["recipient@example.com"], "subject": "Hello", "text": "Testing some Mailgun awesomeness!"}) send_simple_message() |
In this code:
- We first import the requests library, which allows us to send HTTP requests in Python.
- We define a function send_simple_message(). Inside this function, we use requests.post() to send a POST request to the Mailgun API.
- The URL we’re sending the request to is “https://api.mailgun.net/v3/” + config[‘mailgun’][‘domain’] + “/messages”.
- We use the auth parameter to pass in our API credentials.
- We use the data parameter to specify the details of the email. This includes the sender’s email address (“from”), the recipient’s email address (“to”), the subject of the email (“subject”), and the body of the email (“text”).
- Finally, we call the send_simple_message() function to send the email
Sending Emails with Attachments via Mailgun API
To send an email with an attachment, you would need to modify the data dictionary to include the file. Here’s an example:
def send_message_with_attachment(): return requests.post( "https://api.mailgun.net/v3/" + config['mailgun']['domain'] + "/messages", auth=("api", config['mailgun']['api_key']), files=[("attachment", open(config['gmail']['pdf_path']))], data={"from": "Excited User <mailgun@" + config['mailgun']['domain'] + ">", "to": ["recipient@example.com"], "subject": "Hello", "text": "Testing some Mailgun awesomeness with attachment!"}) send_message_with_attachment() |
In this code, we added a files parameter to the requests.post() function. This parameter takes a list of tuples, each representing a file to be attached. Each tuple contains the string “attachment” and the file object obtained by opening the file in binary mode.
Sending Email via GetResponse API
GetResponse is an online marketing platform that offers a suite of powerful email marketing features. Whether you’re managing campaigns, newsletters, or autoresponders, GetResponse has got you covered. But what if you want to programmatically send emails using their services? That’s where the GetResponse API comes into play!
Sending Emails via GetResponse API
In this section, we’ll walk through the steps to send an email using the GetResponse API. We’ll assume you’ve already set up your GetResponse account and obtained the necessary credentials. Now let’s write some Python code to send an email via the GetResponse API. We’ll use the requests
library for making HTTP requests. Here’s an example:
import requests import json def send_getresponse(): url = "https://api.getresponse.com/v3/newsletters" headers = { 'X-Auth-Token': 'api-key ' + config['getresponse']['api_key'], 'Content-Type': 'application/json' } data = { "subject": "Hello from GetResponse!", "fromField": {"fromFieldId": config['getresponse']['from_field_id']}, "campaign": {"campaignId": config['getresponse']['campaign_id']}, "content": {"html": "<h1>Hello, world!</h1>", "plain": "Hello, world!"} } response = requests.post(url, headers=headers, data=json.dumps(data)) return response.status_code |
- We construct the API endpoint URL (url) for creating newsletters.
- The headers include our API key and specify the content type.
- The data dictionary contains the email details: subject, sender, campaign, and content (both HTML and plain text).
- Finally, we make a POST request to the GetResponse API.
Sending Emails with Attachments via GetResponse API
To send an email with an attachment via the GetResponse API, you’ll need to modify the data dictionary. Add an attachment field with the appropriate file path or content. For example:
data = { "subject": "Hello with Attachment", "fromField": {"fromFieldId": config['getresponse']['from_field_id']}, "campaign": {"campaignId": config['getresponse']['campaign_id']}, "content": {"html": "<h1>Hello, world!</h1>", "plain": "Hello, world!"}, "attachments": [{"filename": "example.pdf", "content": "base64-encoded-content"}] } |
Replace “example.pdf” with your actual attachment file name and provide the base64-encoded content. Remember to keep your configuration file secure and never expose sensitive data in public repositories.
Summary about email sending with python
Automating email sending can save a lot of time and effort, especially when dealing with a large number of emails. Python provides several libraries and APIs to make this task easier. Whether you’re using SMTP, Outlook, Gmail, or any other email service, there’s a Python library that can help you automate your email sending.