【问题标题】:Email Attachments not working with Postmark电子邮件附件不适用于邮戳
【发布时间】:2013-12-29 21:19:40
【问题描述】:

我正在使用邮戳从应用程序发送电子邮件。它对普通电子邮件工作正常,但电子邮件附件不起作用。

它在本地工作正常,因为在本地我有 smtp+邮戳设置(要在本地工作,我们需要有邮戳和 smtp)

但在暂存和生产上,我只使用 SMTP 设置

config/environments/staging.rbconfig/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

这里attachment 是保存在S3 上的文件的url。 在开发模式下,我收到带有附件的电子邮件。 但不处于暂存和开发模式。

任何帮助或建议将不胜感激。谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby actionmailer email-attachments postmark


    【解决方案1】:

    使用 Postmark Api 发送带附件的电子邮件

    这种方法是使用 curl 命令。

    需要获取远程文件内容
    require "open-uri" 
    
    需要获取编解码方法
    require "base64"
    
    传递您的远程文件 url 以获取该文件的内容
    file_data  = open('https://example.com/dummy.pdf').read
    
    加密 bin 对象以通过电子邮件发送
    encrypt_data   = Base64.encode64(file_data)
    
    您现在可以使用邮戳 api 发送带有附件的电子邮件,并确保在 X-Postmark-Server-Token 中传递您的 API-KEY
    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 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}'}]}\""
    

    【讨论】:

      【解决方案2】:

      终于找到了上述问题的真正原因。

      我正在使用 gem postmark-rails,以前邮戳不支持电子邮件附件,所以最近他们增强了 gem 以支持附件,不幸的是我使用的是旧版本,所以我需要更新 gem 版本他们在其中一个问题中提到的最新消息:attachment-issue

      我也试图发送保存在 S3 上的文件的 url,所以我需要从 url 读取该文件,然后将其作为附件发送

        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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-26
        • 2017-06-26
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 1970-01-01
        • 2012-12-18
        • 2019-11-15
        相关资源
        最近更新 更多