Skip to main content

Secure Email Relay Mail API

Project description

Proofpoint Secure Email Relay Mail API Package

PyPI Downloads
Library implements all the functions of the SER Email Relay API via Python.

Requirements:

  • Python 3.9+
  • requests
  • requests-oauth2client
  • pysocks

Installing the Package

You can install the tool using the following command directly from Github.

pip install git+https://github.com/pfptcommunity/ser-mail-api-python.git

or can install the tool using pip.

# When testing on Ubuntu 24.04 the following will not work:
pip install ser-mail-api

If you see an error similar to the following:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

You should use install pipx or you can configure your own virtual environment and use the command referenced above.

pipx install ser-mail-api

Creating an API client object

from ser_mail_api.v1 import *

if __name__ == "__main__":
    client = Client("<client_id>", "<client_secret>")

Sending an Email Message

import json

from ser_mail_api.v1 import *

if __name__ == "__main__":
    # Load API key
    with open("../ser.api_key", "r") as api_key_file:
        api_key_data = json.load(api_key_file)

    client = Client(api_key_data.get("client_id"), api_key_data.get("client_secret"))

    # Create a new Message object
    message = Message("This is a test email", MailUser("sender@proofpoint.com", "Joe Sender"))
    
    # Add content body
    message.add_content(Content("This is a test message", ContentType.Text))
    message.add_content(Content("<b>This is a test message</b>", ContentType.Html))
    
    # Add To
    message.add_to(MailUser("to_recipient1@proofpoint.com", "Recipient 1"))
    message.add_to(MailUser("to_recipien2@proofpoint.com", "Recipient 2"))
    
    # Add Cc
    message.add_cc(MailUser("cc_recipien1@proofpoint.com", "Carbon Copy 1"))
    message.add_cc(MailUser("cc_recipien2@proofpoint.com", "Carbon Copy 2"))
    
    # Add Bcc
    message.add_bcc(MailUser("bcc_recipien1@proofpoint.com", "Blind Carbon Copy 1"))
    message.add_bcc(MailUser("bcc_recipien2@proofpoint.com", "Blind Carbon Copy 2"))

    # Add Base64 encoded attachment
    message.add_attachment(Attachment("VGhpcyBpcyBhIHRlc3Qh", Disposition.Attachment, "test.txt", "text/plain"))

    # Add File attachment from disk, if disposition is not passed, the default is Disposition.Attachment
    message.add_attachment(FileAttachment(r"C:\temp\file.csv", Disposition.Attachment))

    # In the following example, we will create a byte stream from a string. This byte array is converted
    # to base64 encoding within the BinaryAttachment object
    text = "This is a sample text stream."

    # Convert the string into bytes
    bytes = text.encode("utf-8")

    # Add Byte array as attachment, if disposition is not passed, the default is Disposition.Attachment
    message.add_attachment(BinaryAttachment(bytes,"bytes.txt", "text/plain", Disposition.Attachment))

    result = client.send(message)

    print("HTTP Status", result.get_status())
    print("HTTP Reason", result.get_reason())

    print("Reason:", result.reason)
    print("Message ID:", result.message_id)
    print("Request ID:", result.request_id)

The following JSON data is a dump of the message object based on the code above.

{
  "attachments": [
    {
      "content": "VGhpcyBpcyBhIHRlc3Qh",
      "disposition": "attachment",
      "filename": "test.txt",
      "id": "d10205cf-a0a3-4b9e-9a57-253fd8e1c7df",
      "type": "text/plain"
    },
    {
      "content": "77u/IlVzZXIiLCJTZW50Q291bnQiLCJSZWNlaXZlZENvdW50Ig0KIm5vcmVwbHlAcHJvb2Zwb2ludC5jb20sIGxqZXJhYmVrQHBmcHQuaW8iLCIwIiwiMCINCg==",
      "disposition": "attachment",
      "filename": "file.csv",
      "id": "f66487f5-57c2-40e0-9402-5723a85c0df0",
      "type": "application/vnd.ms-excel"
    },
    {
      "content": "VGhpcyBpcyBhIHNhbXBsZSB0ZXh0IHN0cmVhbS4=",
      "disposition": "attachment",
      "filename": "byte_stream.txt",
      "id": "bc67d5fa-345a-4436-9979-5efa68223520",
      "type": "text/plain"
    }
  ],
  "content": [
    {
      "body": "This is a test message",
      "type": "text/plain"
    },
    {
      "body": "<b>This is a test message</b>",
      "type": "text/html"
    }
  ],
  "from": {
    "email": "sender@proofpoint.com",
    "name": "Joe Sender"
  },
  "headers": {
    "from": {
      "email": "sender@proofpoint.com",
      "name": "Joe Sender"
    }
  },
  "subject": "This is a test email",
  "tos": [
    {
      "email": "recipient1@proofpoint.com",
      "name": "Recipient 1"
    },
    {
      "email": "recipient2@proofpoint.com",
      "name": "Recipient 2"
    }
  ],
  "cc": [
    {
      "email": "cc1@proofpoint.com",
      "name": "Carbon Copy 1"
    },
    {
      "email": "cc2@proofpoint.com",
      "name": "Carbon Copy 2"
    }
  ],
  "bcc": [
    {
      "email": "bcc1@proofpoint.com",
      "name": "Blind Carbon Copy 1"
    },
    {
      "email": "bcc2@proofpoint.com",
      "name": "Blind Carbon Copy 2"
    }
  ],
  "replyTos": []
}

Proxy Support

Socks5 Proxy Example:

from ser_mail_api.v1 import *

if __name__ == '__main__':
    client = Client("<client_id>", "<client_secret>")
    credentials = "{}:{}@".format("proxyuser", "proxypass")
    client._session.proxies = {'https': "{}://{}{}:{}".format('socks5', credentials, '<your_proxy>', '8128')}

HTTP Proxy Example (Squid):

from ser_mail_api.v1 import *

if __name__ == '__main__':
    client = Client("<client_id>", "<client_secret>")
    credentials = "{}:{}@".format("proxyuser", "proxypass")
    client._session.proxies = {'https': "{}://{}{}:{}".format('http', credentials, '<your_proxy>', '3128')}

HTTP Timeout Settings

from ser_mail_api.v1 import *

if __name__ == '__main__':
    client = Client("<client_id>", "<client_secret>")
    # Timeout in seconds, connect timeout
    client.timeout = 600
    # Timeout advanced, connect / read timeout
    client.timeout = (3.05, 27)

Limitations

There are no known limitations.

For more information please see: https://api-docs.ser.proofpoint.com/docs/email-submission

Project details


Download files

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

Source Distribution

ser_mail_api-1.0.4.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

ser_mail_api-1.0.4-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file ser_mail_api-1.0.4.tar.gz.

File metadata

  • Download URL: ser_mail_api-1.0.4.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for ser_mail_api-1.0.4.tar.gz
Algorithm Hash digest
SHA256 19c9291cae3d7f17a671369ca1c29b8b624aa40540842508deba7221633c71ad
MD5 95e38a63c769f0c0a5147c1b61cbac19
BLAKE2b-256 6f6ea371d6b43ded8d778fbe81905295201e1eb5117ea9a45995654de50a80eb

See more details on using hashes here.

File details

Details for the file ser_mail_api-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ser_mail_api-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for ser_mail_api-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4bf0388590ab3fd0fdefa00a1a57fa4a6f4d969a2cd236e0b00a332712fbbc9f
MD5 2647069385f8f69b1b02da49c5c0ab55
BLAKE2b-256 6ab4e8cd61cc82e8a81fed9a70bea20308679d53f6252ded3546757f5220a8fd

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