Reputation: 862
I am using postmark to send email from the application. Its working fine for normal emails, but the email attachments are not working.
It works fine on local, as on local i have the smtp+postmark settings (as to work it on local we need to have postmark along with smtp)
But on staging and production am using only SMTP settings
config/environments/staging.rb and config/environments/production.rb
POSTMARK_API_KEY = "<my-api-key>"
config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { :api_key => POSTMARK_API_KEY }
config/environments/development.rb
POSTMARK_API_KEY = "<my-api-key>"
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.postmarkapp.com",
:port => 25,
:domain => 'example.com',
:user_name => POSTMARK_API_KEY,
:password => POSTMARK_API_KEY,
:authentication => 'plain',
:enable_starttls_auto => true }
user_mailer.rb
class UserMailer < ActionMailer::Base
default from: DEFAULT_EMAIL
def send_request(params, attachment)
if attachment.present?
mime_type = MIME::Types.type_for(attachment).first
attachments["#{attachment.split('/').last}"] = { mime_type: mime_type,
content: attachment, encoding: 'base64' }
end
mail(
to: <some_email>,
subject: "Refer Request",
tag: "refer_request")
end
Here attachment
is the url of file saved on S3.
In development mode i receive email with attachment.
but not in staging and development mode.
Any help or suggestions would be appreciated. Thanks.
Upvotes: 1
Views: 2980
Reputation: 862
Finally am able to find the actual cause of above issue.
I am using the gem postmark-rails, previously postmark was not supporting the email-attachments, so recently they had enhanced gem to support attachments and unfortunately i was using the old version of it, so i need to update gem version to latest as they have mentioned in one of their issues: attachment-issue
also i was trying to send url of file saved on S3, so instead of that i need to read that file from url and then send it as attachment
require "open-uri"
def send_refer_pt_request(params, attachment)
if attachment.present?
mime_type = MIME::Types.type_for(attachment).first
url_data = open(attachment).read()
attachments["#{attachment.split('/').last}"] = { mime_type: mime_type,
content: url_data }
end
mail(
to: <some_email>,
subject: "Refer Request",
tag: "refer_request")
end
Upvotes: 1
Reputation: 1422
This approach is using curl command.
Require to get the remote file contentrequire "open-uri"
Require to get the encoding-decoding methods
require "base64"
Pass your remote file url to get the content of that
file_data = open('https://example.com/dummy.pdf').read
Encryp the bin object to send it via email
encrypt_data = Base64.encode64(file_data)
You can now send the email with attachment using postmark api and make sure to pass your API-KEY in the X-Postmark-Server-Token
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: '[email protected]', To: '[email protected]', Subject: 'Postmark test for Attachment Email', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user you have received email with attachment.</body></html>', Attachments:[{'ContentType': 'application/pdf', 'Name': 'dummy.pdf', 'Content': '#{encrypt_data}'}]}\""
Upvotes: 4