Postmark Email

The code below transmits your emails via the Postmark (https://postmarkapp.com/) web service.

To use this method, you must have…

  • Created a Postmark account,
  • Confirmed your sending email address,
  • Added your API token and sending email address to your settings, as postmarktoken and postmarkfrom respectively,
  • Installed the postmarker package in Python on the ReportList server.
import os
import json
import traceback
 
pth = os.path.dirname(__file__) + "/content.json"
f = open(pth,'r')
content = f.read()
f.close()
 
content = json.loads(content)
messages = content['messages']
fromaddr = content["options"]["postmarkfrom"]
token = content["options"]["postmarktoken"]
 
from postmarker.core import PostmarkClient
 
try:
    postmark = PostmarkClient(server_token = token)    
 
    for msg in messages:
        try:            
            email = postmark.emails.Email(From=fromaddr,To=msg['to']['name'],Subject=msg['title'],HtmlBody=msg['richcontent'])
 
            for at in msg['attachments']:
                email.attach(at['path'])
 
            email.send()
 
            print("-----")
            print("SUCCESS")
        except:
            print("-----")
            print("FAILURE")
            traceback.print_exc()
            pass
 
except Exception as ex:
    print("-----")
    print("FAILURE")