Connectors

Contact support

Postmark - Getting started in Peliqan

Postmark - Getting started in Peliqan

Postmark is one of the connectors offered in Peliqan to send emails and receive emails.

Postmark offers a free license that does not expire, for up to 100 emails per month.

More info: https://postmarkapp.com/pricing

Connect Postmark

Sign up for an account on Postmark first. Create a “Server” in Postmark.

Copy the API token of the Server. In Peliqan, go to Connections > Add connection.

Find Postmark in the list of available connectors and select it. Paste the API token and save.

Send out emails

Here are example scripts in Peliqan to send out emails.

Send an email

email = {
    "from": "no-reply@acme.com",
    "to": "john@acme.com",
    "subject": "Hi there",
    "text": "We are happy to provide you with this report !",
    "html": "We are <b>happy</b> to provide you with this report !"
}

conn = pq.connect('Postmark')
result = conn.add('email', email)
st.json(result) # for debugging only, see result of sending email to Postmark

Send email with text file as attachment

import base64

text_file_content = "This will be in the attached text file."

email = {
    "from": "no-reply@acme.com",
    "to": "john@acme.com",
    "subject": "Test email with attachment",
    "text": "See Text file attached.",
    "html": "See Text file attached.",
    "attachment_name": "my_file.txt",
    "attachment_content_base64": base64.b64encode(text_file_content.encode("ascii")).decode("ascii"),
    "attachment_contenttype": "text/plain"
}

conn = pq.connect('Postmark')
result = conn.add('email_with_attachment', email)
st.json(result) # for debugging only, see result of sending email to Postmark

Send email with CSV or PDF as attachment

See the Alerting & Messaging section for examples on how to send out CSV files with data from a Peliqan table:

Alerting & messagingAlerting & messaging

See the Reporting section for a detailed example on how to generate a PDF file and send that PDF file as an attachment in an email:

Generate and distribute PDF reports

Receive emails (incoming emails)

In Peliqan, go to Admin > Settings and copy your Account Webhook URL.

This is your Peliqan URL to receive incoming webhook events. Every incoming webhook event will be stored in your Peliqan data warehouse, in a table webhook.incoming_webhooks. This table will automatically be created on the first incoming webhook.

In Postmark, go to your Server > Message Streams > Default inbound Stream.

Paste your Peliqan Account Webhook URL in Postmark in the field inbound webhook URL.

Note down your Postmark email address to receive emails. It will look like this:

41b12...569d@inbound.postmarkapp.com

Send an email to this address. Check if it is processed by Postmark and forwarded to the Peliqan webhook URL. Next, check if you can find a new row in the table webhook.incoming_webhooks in your Peliqan DWH.

Finally, you can process these incoming emails with a script in Peliqan:

dw = pq.dbconnect(pq.DW_NAME)
incoming_webhooks = dw.fetch(pq.DW_NAME, 'webhook', 'incoming_webhooks')

for incoming_webhook in incoming_webhooks:
    email = incoming_webhook["payload"]
    sender = email["From"]
    to = email["To"]
    subject = email["Subject"]
    body = email["TextBody"]
    st.write(subject) # add logic here to process incoming emails

Troubleshooting

If you use special characters in HTML such as ø or ö, you need to replace them with escaped unicode, before sending to Postmark. Example:

def replace_with_unicode(s):
    str_len = len(s)
    str_arr = []
    for i in range(0, str_len):
        ch = s[i]
        n = ord(ch)
        if n < 0x10000:
            str_arr.append('\\u%04x' % (n,))
        else:
            # surrogate pair
            n -= 0x10000
            s1 = 0xd800 | ((n >> 10) & 0x3ff)
            s2 = 0xdc00 | (n & 0x3ff)
            str_arr.append('\\u{0:04x}\\u{1:04x}'.format(s1, s2))
    return ''.join(str_arr)

postmark_api = pq.connect('Postmark')
email = {
    "from": "no-reply@acme.com",
    "to": "john@acme.com",
    "subject": "Example email from Peliqan using Postmark",
    "html": "This is from <b>Pøstmark</b>",
    "text": "This is from Postmark",
}

email['html'] = replace_with_unicode(email['html'])

postmark_api = pq.connect('Postmark')
result = postmark_api.add('email', email)
st.write(result)

There are various other special characters that have to be replaced in HTML, such as line breaks, tabs, double quotes. Here’s another example where we replace characters such as ö with o:

import unicodedata

html = "<b>My HTML</b>"

# Replace line break, tabs, double quotes and backslashes to make sure HTML will not corrupt json body payload to Postmark
html = html.replace("\n","").replace("\t","").replace('"', "'").replace("\\", "")

# Replace special characters such as ö with o
html = unicodedata.normalize('NFKD', html).encode('ASCII', 'ignore').decode('ASCII')

postmark_api = pq.connect('Postmark')
email = {
    'to': "john@acme.com",
    'from': "no-reply@acme.com",
    'html': html,
    'subject': "Example email from Peliqan using Postmark",
}
result = postmark_api.add('email', email)
st.write(result)