Action Mailer不执行Proc以生成TO字段

时间:2015-07-30 16:19:15

标签: ruby-on-rails-4 actionmailer proc rails-activejob

UP 04/08/2015:实际上是否可以同时使用.deliver_later和proc?

我的问题:

我的电子邮件没有执行生成TO字段的过程,因此在postfix上发送错误的电子邮件

后缀日志

sudo tail -n 150 /var/log/mail.log

Jul 30 17:39:44 je postfix/pickup[3974]: 0DA531FC2AFA: uid=1030 from=<candidature@myorg.com>
Jul 30 17:39:44 je postfix/cleanup[8430]: 0DA531FC2AFA: message-id=<55ba453fc4fd1_20e02375a9c04882a@myorg.mail>
Jul 30 17:39:44 je postfix/qmgr[3506]: 0DA531FC2AFA: from=<candidature@telecom-etude.com>, size=18915, nrcpt=2 (queue active)
Jul 30 17:39:44 je postfix/error[8522]: 0DA531FC2AFA: to=<Proc:0xbbd2989c@/var/www/intranet_rails_production/releases/20150729170507/app/mailers/mailing_lists_mailer.rb:42>, relay=none, delay=0.41, delays=0.22/0.02/0/0.17, dsn=5.1.3, status=bounced (bad address syntax)
Jul 30 17:39:44 je postfix/error[8522]: 0DA531FC2AFA: to=<#<Proc:0xbbd2989c@/var/www/intranet_rails_production/releases/20150729170507/app/mailers/mailing_lists_mailer.rb:42>>, relay=none, delay=0.52, delays=0.22/0.02/0/0.28, dsn=5.1.3, status=bounced (bad address syntax)

控制器

...
if Rails.env.production? # Using :sendmail as delivery method
  MailingListsMailer.action(@mail).deliver_later
else # Typically using :letter_opener or :file as delivery method
  MailingListsMailer.action(@mail).deliver_now
...

邮件程序

class MailingListsMailer < ActionMailer::Base
   def action(message)
    format_mail_params(message)
    mail(
      to: Proc.new {read_emails_file},
      ...
    )
   end

    private

    def read_emails_file
      File.read('emails.txt').split('\n')
    end
end

配置

config.action_mailer.delivery_method = :sendmail
  config.action_mailer.smtp_settings = {
    :address => "localhost",
    :domain => "myorg.fr"
  }
    config.action_mailer.default_url_options = { host: 'myorg.fr', protocol: 'https' }

编辑:我正在使用ActionMailer Basics #2.3.3

中建议的过程

1 个答案:

答案 0 :(得分:1)

你可能根本不需要Proc包装器,试试这个:

def action(message)
    format_mail_params(message)
    mail(
        to: read_emails_file,
        ...
    )
end

要调试它,请执行以下操作:

def action(message)
    format_mail_params(message)

    puts "*"*50
    emails = read_emails_file
    puts emails
    puts "*"*50

    mail(
        to: emails,
        ...
    )
end

然后检查服务器日志以查找类似于以下内容的消息:

*******************************************************************
[...]
*******************************************************************

迭代电子邮件

因为您不想透露所有人的电子邮件,并避免垃圾邮件检测,您可能想要迭代电子邮件数组:

def action(message)
  format_mail_params(message)

  read_emails_file.each do |email|
    mail(
        to: email,
        ...
    )
  end
end