Skip to main content

FreshMail Python Client

Project description

Python API Client for Freshmail API V3

Official API V3 Documentation

This API V3 client covers only sending transactional emails by FreshMail Application. Additional features will be added in future.

Requirements

  • Python 3.5+

Installation

pip install freshmail

Usage

Send transactional email

from messaging.email.mailbag import MailBag
from sender.sender import FreshmailSender
from transport.api.email import EmailApiTransport

token = "MY_APP_TOKEN"
transport = EmailApiTransport(bearer_token = token)
sender = FreshmailSender(transport=transport)

message = MailBag()

message.from_ = {
    "email": "from@address.com",
    "name": "Office"
}
message.subject = "That's my awesome first mail!"
message.contents = {
    "html": "<html><body><strong>Look!</strong> its working!</body></html>"
}
message.tos = [
    {
        "email": "recipient email address"
    }
]

response = sender.send(message=message)

Handle with response object

if response.is_success():
    response_data = response.get_data()

Handle with raw Requests Response

response.get_requests_response();

Send personalized email

from freshmail_api.messaging.email.mailbag import MailBag
from freshmail_api.sender.sender import FreshmailSender
from freshmail_api.transport.api.email import EmailApiTransport

token = "MY_APP_TOKEN"
transport = EmailApiTransport(bearer_token = token)
sender = FreshmailSender(transport=transport)

message = MailBag()

message.from_ = {
    "email": "from@address.com",
    "name": "Office"
}
message.subject = "Hello $$first_name$$! I've got promotion code for You!"
message.contents = {
    "html": "<html><body>Your code is <strong>$$code$$</strong></body></html>"
}
message.tos = [
    {
        "email": "recipient email address",
        "personalization": {
            "first_name": "Joshua",
            "code": "CODE1234"
        }
    }
]

response = sender.send(message=message)

Send multiply emails

You can send multiple emails by one request. It's much faster than sending one email by one request. In one request You can send up to 100 emails.

from freshmail_api.messaging.email.mailbag import MailBag
from freshmail_api.sender.sender import FreshmailSender
from freshmail_api.transport.api.email import EmailApiTransport

token = "MY_APP_TOKEN"
transport = EmailApiTransport(bearer_token = token)
sender = FreshmailSender(transport=transport)

message = MailBag()

message.from_ = {
    "email": "from@address.com",
    "name": "Office"
}
message.subject = "Hello $$first_name$$! I've got promotion code for You!"
message.contents = {
    "html": "<html><body>Your code is <strong>$$code$$</strong></body></html>"
}
message.tos = [
    {
        "email": "recipient email address",
        "personalization": {
            "first_name": "Joshua",
            "code": "10percentDISCOUNT"
        }
    },
    {
        "email": "second recipient email address",
        "personalization": {
            "first_name": "Donald",
            "code": "25percentDISCOUNT"
        }
    },
    {
        "email": "third recipient email address",
        "personalization": {
            "first_name": "Abbie",
            "code": "FREEshippingDISCOUNT"
        }
    }
]

response = sender.send(message=message)

Send email from template

You can use FreshMail Templates mechanism to optimize requests to API. Additionally You can modify content of Your emails in FreshMail, not modifying the code of Your application.

from freshmail_api.messaging.email.mailbag import MailBag
from freshmail_api.sender.sender import FreshmailSender
from freshmail_api.transport.api.email import EmailApiTransport

token = "MY_APP_TOKEN"
transport = EmailApiTransport(bearer_token = token)
sender = FreshmailSender(transport=transport)

message = MailBag()

message.from_ = {
    "email": "from@address.com",
    "name": "Support"
}
message.subject = "Hello, that's my email genereted by template!"
message.template_hash = "TEMPLATE_HASH"
message.tos = [
    {
        "email": "recipient email address"
    }
]

response = sender.send(message=message)

Send email with attachments

You can sent emails with attachments. You can upload up to 10 files. Weight of all attachments in email can't exceed 10Mb.

from freshmail_api.messaging.email.attachment import Base64Attachment, LocalFileAttachment
from freshmail_api.messaging.email.mailbag import MailBag
from freshmail_api.sender.sender import FreshmailSender
from freshmail_api.transport.api.email import EmailApiTransport

from base64 import b64encode

token = "MY_APP_TOKEN"
transport = EmailApiTransport(bearer_token = token)
sender = FreshmailSender(transport=transport)

# attachment from hard drive
local_file_attachment = LocalFileAttachment(
    filepath="/my/local/path/file.extension",
    name="optional file name"
)

# attachment from base64 
base64_attachment = Base64Attachment(
    name="base64text.txt",
    content=b64encode("example content".encode())
)

message = MailBag()

message.from_ = {
    "email": "from@address.com",
    "name": "Support"
}
message.subject = "Hello, thats mail with attachments!!"
message.contents = {
    "html": "<html><body><strong>Attachments</strong> in mail</body></html>"
}
message.tos = [
    {
        "email": "recipient email address"
    }
]

message.attachments = [
    local_file_attachment,
    base64_attachment
]

response = sender.send(message=message)

Error handling

API throws exceptions for errors that occurred during requests and errors occurred before sending requests.

  • If request is not sent to server because of wrong API usage, an exception that extends freshmail.exceptions.ApiUsageException is thrown. This kind of exception means, for example, wrong parameters pass to some methods or passing both content and template in transactional mail, which means request won't be accepted, so API does not make any request.
  • If request is sent and some network issue occurred (DNS error, connection timeout, firewall blocking requests), a freshmail.exceptions.RequestException is thrown.
  • If request is sent, a response is received but some server error occurred (500 level http code), a freshmail.exceptions.ServerException is thrown.
  • If request is sent, a response is received but some client error occurred (400 level http code), a freshmail.exceptions.ClientException is thrown.
from freshmail_api.exceptions import ClientException

try:
    response = sender.send(message=message)
except ClientException as e:
    handle_exception(e)

Set logger to level DEBUG to log request data and request response.

Proxy setup

If You need to configure proxy You can use a dictionary and pass it as an argument to Transport object.

from freshmail_api.transport.api.email import EmailApiTransport

token = 'MY_APP_TOKEN'
proxies = {
    "http": "http://10.10.10.10:8000",
    "https": "http://10.10.10.10:8000"
}
transport = EmailApiTransport(bearer_token=token, proxies=proxies)

Logging and debugging

If You need to log or debug Your requests You can pass logging.Logger object.

Example logger usage

import logging
from logging.handlers import SysLogHandler
from freshmail_api.transport.api.email import EmailApiTransport

def setup_logger(logger, level=logging.INFO, path="stdout"):
    logger.setLevel(logging.DEBUG)

    if path == 'syslog':
        ch = SysLogHandler(address='/dev/log')
    elif path == 'stdout':
        ch = logging.StreamHandler()
    else:
        ch = logging.StreamHandler()

    ch.setLevel(level)
    formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s:%(lineno)d %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)

bearer_token = "MY_APP_TOKEN"
logger = logging.getLogger("freshmail_api_client")
setup_logger(logger, level=logging.DEBUG)

transport = EmailApiTransport(bearer_token=bearer_token, logger=logger)

Project details


Release history Release notifications | RSS feed

This version

0.7

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

freshmail-api-pwd+123673@freshmail.pl-0.7.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file freshmail-api-pwd+123673@freshmail.pl-0.7.tar.gz.

File metadata

  • Download URL: freshmail-api-pwd+123673@freshmail.pl-0.7.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for freshmail-api-pwd+123673@freshmail.pl-0.7.tar.gz
Algorithm Hash digest
SHA256 c9e1465407df40cbda23ad1c1069de231a0985c5a7dbde335980c6eaeb71406f
MD5 3c2dc1b1fa375f1bbe910a85c478a6f2
BLAKE2b-256 146f6ca3dbb7872378b5f8f727c7605e52ccb78cf6f41bb948aad3818f0df2ec

See more details on using hashes here.

File details

Details for the file freshmail_api_pwd_123673_freshmail.pl-0.7-py3-none-any.whl.

File metadata

  • Download URL: freshmail_api_pwd_123673_freshmail.pl-0.7-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for freshmail_api_pwd_123673_freshmail.pl-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5e879f70086110fe69b9b0895331c2b4ef20f3d2fd110827ca5d9f6c64c93901
MD5 2131121312e58859cd1407ac423b61cc
BLAKE2b-256 a6a264869175806d004fb931bdc6dc8681e751ed774b797c60c2bd37f81788b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page