1

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

它在本地工作正常,因为在本地我有 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 }

配置/环境/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。在开发模式下,我收到带有附件的电子邮件。但不在分期和开发模式下。

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

4

2 回答 2

4

使用 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}'}]}\""
于 2013-12-12T12:28:08.037 回答
1

终于能够找到上述问题的实际原因。

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

我也试图发送保存在 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
于 2013-12-12T12:43:04.157 回答