如何使用“mail”gem指定发送电子邮件的人



我使用mail gem发送电子邮件,使用下面非常简单的代码。当我发送电子邮件时,smtp_account.user_namesmtp_account.from可以是例如support@thebestcompany.com。这会使收件人看到他收到了来自support的电子邮件(你知道,在他的收件箱中,他可以在那里获得最后x封电子邮件的概述)。我如何指定这一点,以便收件人能够看到更有趣的东西,例如The Best Company

require 'mail'
class SmtpMails
  def self.send_mails(smtp_account, to, subject, text)
    options = {
      :address              => smtp_account.address,
      :port                 => smtp_account.port,
      :domain               => smtp_account.domain,
      :user_name            => smtp_account.user_name,
      :password             => smtp_account.password,
      :authentication       => smtp_account.authentication,
      :enable_starttls_auto => smtp_account.enable_starttls_auto
    }
    Mail.defaults do
      elivery_method :smtp, options
    end
    mail = Mail.deliver do
      to to
      from smtp_account.from
      subject subject
    end
  end
end

在指定电子邮件的发件人时,您需要添加一些附加信息。下面的代码片段应该做你想要的:

mail = Mail.deliver do
  to to
  from "The Best Company <#{smtp_account.from}>"
  subject subject
end

将电子邮件地址放在<>括号之间是为发送的电子邮件添加友好名称的标准做法。

mail.from.addresses
mail.sender.address

试试这个。

最新更新