Search code examples
ruby-on-railsrubyactionmaileremail-attachmentspostmark

Email Attachments not working with Postmark


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.


Solution

  • 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