From last two days we were running into an issue with the Postmark API based email attachments, specially when you have to use the remote file url for the attachment. Also we were not seeing any error logs for non-delivery of attachments.
So Swati and I started with different approaches to solve the problem. I will be explaining here the approach where I directly used the Postmark API key and tried to send an attachment with email using CURL command. Whereas Swati will soon add here post about sending email with attachment using postmark-rails gem and what was causing issue while she was trying to send an email with attachment.
Curl command to send the email is very straight forward however to send an attachment with the remote file url is little bit tricky.
To make it work important is to use libraries like open-uri and base64
-
open-uri - Required to get the remote file content
-
base64 - Require to get the encoding-decoding methods
-
Open-uri will help you to get the content of the Remote file, pass your remote file url and it will return the data
-
Now Base64.encode64 will help to encrypt the bin object to send it via email
-
Once all these steps are done make sure you set your Postmark-API-Key in the X-Postmark-Server-Token
-
That is all once you are done it will send the email with attachment
Below code snippet you can directly run in your IRB screen
require "open-uri" require "base64" file_data = open('https://example.com/dummy.pdf').read encrypt_data = Base64.encode64(file_data) system "curl -X POST \"http://api.postmarkapp.com/email\" \ -H \"Accept: application/json\" \ -H \"Content-Type: application/json\" \ -H \"X-Postmark-Server-Token: POSTMARK_API_KEY\” \ -v \ -d \"{From: ‘from@example.com', To: ‘to@example.com', Subject: 'Postmark test', HtmlBody: ' Hello dear Postmark user.', Attachments:[{'ContentType': 'application/pdf', 'Name': 'dummy.pdf', 'Content': '#{encrypt_data}'}]}\""