Rails Rails 中怎么才能设置多个 mail 发送邮箱

zealinux · 2012年11月26日 · 最后由 runup 回复于 2016年06月05日 · 5819 次阅读

config/environments/$RAILS_ENV.rb 中只能设置一个发送服务器

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'baci.lindsaar.net',
  :user_name            => '<username>',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

怎么才能设置多个,比如 [email protected], [email protected], [email protected] ...

如果你有自己的 SMTP 服务器,设置多个邮件的from信息是没有问题的。但是如果你用 Gmail 发送邮件,最好就用你的 Gmail 的 account 作为邮件发件人。

class Notifier < ActionMailer::Base
  def welcome(recipient)
    @account = recipient
    mail(:to => recipient.email_address_with_name,
         :from => '[email protected]', # 这里设置邮件的from
         :bcc => ["[email protected]", "Order Watcher <[email protected]>"])
  end
end

Gmail 不支持直接在 ActionMailer 里设置不同的 from;不管你怎么设,它都只会用 smtp_settings 里设备的用户来发。

几年前自己写过一个 SMTP 的 mokey patch 来同时设置多个 smtp 发件用户(那时候是为了能够多发送一些邮件--Gmail 一个账号每天有发送限额)。可惜这份代码丢了。

#1 楼 @lgn21st 为什么我用 QQ 的企业服务器无法发送邮件呢?
config.action_mailer.smtp_settings = { :address => "smtp.exmail.qq.com", :port => 587, :domain => 'xishr.com, :user_name => '', :password => '', :authentication => 'plain', :enable_starttls_auto => true }

#3 楼 @glorySpring 邮件发送过程中,有什么错误信息呢?或者是否邮件已经发出,但是发到了垃圾邮件箱里面去了呢?

#4 楼 @lgn21st 没有任何错误。这是我最纠结的...垃圾邮件里面也没有。

#5 楼 @glorySpring 这种时候,你可以尝试在 console 里面试试看发一封邮件,通常都是有 log 的。

#6 楼 @lgn21st 嗯,我试试吧。

#6 楼 @lgn21st 呃,忘了说:谢谢。

:user_name            => '<username>',

上面的的值在哪里进行设置,然后在 log 文件中可以过滤掉

需要 登录 后方可回复, 如果你还没有账号请 注册新账号