我用邮戳从申请表中发送电子邮件。它可以正常工作的电子邮件,但电子邮件附件不工作。
它可以在本地工作,因为在本地我有smtp+邮戳设置(至于在本地工作,我们需要有邮戳和smtp)
但在登台和生产时,am仅使用SMTP设置
config/environments/staging.rb和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  }

用户邮箱.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。
在开发模式下,我接收带有附件的电子邮件。
但不是分期开发模式。
任何帮助或建议将不胜感激谢谢。

最佳答案

使用邮戳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}'}]}\""

09-09 21:58
查看更多