/mailersend-python

The official MailerSend Python SDK

Primary LanguagePython

MailerSend Python SDK

MIT licensed

Table of Contents

Installation

$ python3 -m pip install mailersend

Usage

Requires the MAILERSEND_API_KEY environment variable

Sending a basic email.

import mailersend

mailer = mailersend.NewApiClient()

subject = "Subject"
text = "This is the text content"
html = "<p>This is the HTML content</p>"

my_mail = "owner@verified_domain.com"
recipient_list = [ 'pamela@dundermifflin.com',
'dwight@dunderfmifflin.com', 'jim@dundermifflin.com']

mailer.send(my_mail, recipient_list, subject, html, text, None, None)

Sending a basic email using template ID.

import mailersend

mailer = mailersend.NewApiClient()

subject = "Subject"
template_id = '351ndrxrx45zqx8k'

my_mail = "owner@verified_domain.com"
recipient_list = [ 'pamela@dundermifflin.com',
'dwight@dunderfmifflin.com', 'jim@dundermifflin.com']

mailer.send(my_mail, recipient_list, subject, None, None, template_id, None)

Sending an email with simple personalization.

import mailersend

mailer = mailersend.NewApiClient()

subject = "Hello from {$company}"
text = "This is the text content"
html = "<p>This is the HTML content, {$name}</p>"

my_mail = "owner@verified_domain.com"
recipient_list = [ 'pamela@dundermifflin.com',
'dwight@dunderfmifflin.com', 'jim@dundermifflin.com']
variables = [{
    "email": "pamela@dundermifflin.com",
    "substitutions": [{
        "var": "name",
        "value": "Pam",
    },
    {
        "var": "company",
        "value": "Dunder Mifflin",
    },]
}]

mailer.send(my_mail, recipient_list, subject, html, text, None, variables)

Get ID by name

This helper function allows to programatically gather IDs for domains and subscribers for later use in the code. Takes 2 arguments, category and name, and returns the respective ID of the searched-for item.

Available options for category:

  • domains
  • recipients
import mailersend

mailer = mailersend.NewApiClient()

mailer.getIdByName("domains", "mailersend.com")

Get recipients by domain

import mailersend

mailer = mailersend.NewApiClient()

mailer.getRecipientsForDomain('<domainId>')

Get activity list

The activity list can be filtered using the available query parameters, found at MailerSend official documentation.

import mailersend

mailer = mailersend.NewApiClient()

mailer.getDomainActivity("DOMAIN_ID")

Get activity by date

The activity list can be filtered using the available query parameters, found at MailerSend official documentation.

import mailersend

mailer = mailersend.NewApiClient()

eventList = [
    'processed', 'queued', 'sent', 'delivered',
    'soft_bounced', 'hard_bounced', 'junk', 'opened',
    'clicked', 'unsubscribed', 'spam_complaints',
]

mailer.getActivityByDate('<domainId>', 1443661141, 1620772339, '<groupBy>', eventList)

Get domain list

The activity list can be filtered using the available query parameters, found at MailerSend official documentation.

import mailersend

mailer = mailersend.NewApiClient()

mailer.getDomains("DOMAIN_ID")

Update domain settings

There is a function that can be used per domain setting (available domain settings).

A full example for all domain settings is showcased here:

import mailersend

mailer = mailersend.NewApiClient()

# enable send_paused
mailer.sendPaused('<domainID>', True)

# enable clicks tracking
mailer.trackClicks('<domainID>', True)

# enable opens tracking
mailer.trackOpens('<domainID>', True)

# enable unsubscribes tracking
mailer.trackUnsubscribe('<domainID>', True)

# add unsubscribe custom plaintext string
mailer.trackUnsubscribePlain('<domainID>', 'Click here to unsubscribe')

# add unsubscribe custom HTML string
mailer.trackUnsubscribeHtml('<domainID>', '<p>Click here to <a href=\"{$unsubscribe}\">unsubscribe<\/a><\/p>')

# enable content tracking
mailer.trackContent('<domainID>', True)

# enable custom tracking
mailer.customTracking('<domainID>', True)

# set custom tracking subdomain
mailer.setCustomTrackingSubdomain('<domainID>', 'track.dundermifflin.com')

Delete a domain

import mailersend

mailer = mailersend.NewApiClient()

mailer.deleteDomain(mailer.getIdByName('<domainID>')

Testing

To be implemented

Support and Feedback

In case you find any bugs, submit an issue directly here in GitHub.

You are welcome to create SDK for any other programming language.

If you have any troubles using our API or SDK free to contact our support by email info@mailersend.com

The official documentation is at https://developers.mailersend.com

License

The MIT License (MIT)