2015-06-10 105 views
0

((我发现了类似的问题,但它们是第三方的mandrill gem或Heroku,我没有使用其中的任何一个,只是mandrill-api本身。) )用Rails发送MailChimp邮件

我正在尝试使用电子邮件确认(我们使用Devise)创建帐户。我设置了可以确认的,所有这一切都很好。最初,我使用基本的设计电子邮件来处理所有事情,并且它很好,并发送 - 但之后我试图将其切换到我们的MailChimp/Mandrill帐户,并且电子邮件不会发送。我已经尝试了所有我能想到的事情,并且我被卡住了。

请注意,它似乎与Mandrill通信 - 当我从MailChimp删除确认指令模板,或不发送它到Mandrill,我得到一个模板找不到错误。但是当我创建模板时,它实际上并没有发送。

在日志中唯一的一点是:

NotificationMailer#confirmation_instructions: processed outbound mail in 5676.4ms 

的邮件:

class NotificationMailer < ActionMailer::Base 
    require 'mandrill' 
    default from: "XXXXXXXXX <[email protected]>" 

    def mandrill_client 
     @mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY 
    end 

    def confirmation_instructions(record, token, opts={}) 
     template_name = "confirmation-instructions" 
     template_content = [] 
     message = { 
      to: [{email: record.email}], 
      subject: "Confirmation Instructions", 
      var_user_email: record.email, 
      merge_vars: [ 
      {rcpt: record.email, 
       vars: [ 
        {name: "USER_EMAIL", content: record.email}, 
        {name: "CONFIRMATION_LINK", content: user_confirmation_url(confirmation_token: token)} 
       ] 
      }] 
     } 
     mandrill_client.messages.send_template template_name, template_content, message 
    end 

end 

Development.rb文件相对部分:

config.action_mailer.raise_delivery_errors = true 
config.action_mailer.perform_deliveries = true 

config.action_mailer.delivery_method = :smtp 
config.action_mailer.default_url_options = { host: '192.168.33.111' } 

config.action_mailer.smtp_settings = { 
    address: "smtp.mandrillapp.com", 
    port: 587, 
    enable_starttls_auto: true, 
    user_name: "XXXXXXXXXXXXXXXXX", 
    password: "XXXXXXXXXXXXXXXXXX", 
    authentication: "login", 
} 

我也从控制台进行了测试,而且在那里也没有错误。似乎没有任何错误,但电子邮件从不发送。有任何想法吗?

回答

0

所以经过很多痛苦之后,我仍然不完全确定最初的邮件格式有什么问题,但是我基于其他网站方法稍微改变了它,以下是它的工作原理。我什么也没有改变。

class NotificationMailer < ActionMailer::Base 
    require 'mandrill' 

    default from: "XXXXX <[email protected]>" 

    def mandrill_client 
    @mandrill_client ||= Mandrill::API.new XXXXXXXXXXXX 
    end 

    def confirmation_instructions(record, token, opts={}) 
    template_name = "confirmation-instructions" 
    template_content = [] 
    message = { 
     :subject=> "Confirmation Instructions", 
     :from_name=> "XXXXXXXXX", 
     :text=> "Confirm your email", 
     :to=>[ 
     { 
      :email=> record.email, 
      :name=> record.first_name + " " + record.last_name 
     } 
     ], 
     :from_email=>"[email protected]", 
     :merge_vars=>[ 
     { 
      rcpt: record.email, 
      vars: 
      [ 
      {name: "USER_EMAIL", content: record.email}, 
      {name: "USER_NAME", content: record.first_name + " " + record.last_name}, 
      {name: "CONFIRMATION_LINK", content: user_confirmation_url(confirmation_token: token)} 
      ] 
     } 
     ] 
    } 
    mandrill_client.messages.send_template template_name, template_content, message 
    end 
end 

注意,

:text=> "Confirm your email" 

场不使用,我还在调整它 - 只是想后这让其他人也可能难以幸免的痛苦我是从MailChimp /山魈的非常可笑繁琐格式化和可怕的文件。

和公正的信息,我使用的唯一的宝石是山魈的API宝石:

gem 'mandrill-api', '~> 1.0.51', require: "mandrill" 
相关问题