Rails 4 Mailchimp Mandrill 与设计
Posted
技术标签:
【中文标题】Rails 4 Mailchimp Mandrill 与设计【英文标题】:Rails 4 Mailchimp Mandrill with Devise 【发布时间】:2016-07-22 01:51:40 【问题描述】:我正在尝试使用我的设计和 manrill/mailchimp(合并)帐户设置 Rails 4。
我想从我的 mailchimp/mandrill 帐户发送一封设计确认电子邮件。
我的 heroku 日志显示此消息:
MandrillDeviseMailer#confirmation_instructions: processed outbound mail in 504.2ms
但是我没有收到电子邮件。
我不知道如何设置我的代码/mandrill/mailchimp 帐户才能工作。
我有一个如下的邮件:
class MandrillDeviseMailer < Devise::Mailer
default from: "hello@testhub.com"
require "mandrill"
def confirmation_instructions(record, token, opts=)
# code to be added here later
options =
:subject => 'Confirm your account',
:email => record.email,
:name => record.formal_name,
:global_merge_vars => [
name: 'email',
content: record.email
,
name: 'confirmation_link',
content: record.confirmation_token
],
:template => 'devise-confirmation-instructions'
mandrill_send(options)
end
def reset_password_instructions(record, token, opts=)
options =
:subject => "Reset your password",
:email => record.email,
:global_merge_vars => [
name: "password_reset_link",
content: "http://www.testhub.com/users/password/edit?reset_password_token=#token"
,
name: "PASSWORD_RESET_REQUEST_FROM",
content: record.full_name
],
:template => "Forgot Password"
mandrill_send options
end
def unlock_instructions(record, token, opts=)
# code to be added here later
end
def mandrill_send(opts=)
message =
:subject=> "#opts[:subject]",
:from_name=> "Welcome aboard",
:from_email=>ENV["OD_WELCOME"],
:to=>
["name"=>"#opts[:formal_name]",
"email"=>"#opts[:email]",
"type"=>"to"],
:global_merge_vars => opts[:global_merge_vars]
sending = MANDRILL.messages.send_template opts[:template], [], message
rescue Mandrill::Error => e
Rails.logger.debug("#e.class: #e.message")
raise
end
end
我有我的 production.rb 设置:
config.action_mailer.smtp_settings =
:port => "587",
:address => "smtp.mandrillapp.com",
:user_name => ENV['OD_WELCOME'],
:password => ENV['ROD_WELCOME'],
:domain => "testhub.com",
:authentication => :plain
我不知道如何在 mandrill /mailchimp 中设置我的电子邮件模板以使这些变量起作用。
我尝试了几种变体,但不是使用动态输入填充变量,而是使用管道和 ruby 标记以及应编译为代码的文本发送电子邮件。
在我的 mailchimp 模板中,我有:
嗨
您可以通过以下链接确认您的帐户电子邮件:
上面显示的是文本。我也尝试将 ruby 标签放在 || 标签内,但那些也只是打印出来。
有人知道怎么设置吗?
进一步尝试
我还尝试将我的邮件黑猩猩模板设置为使用合并标签:
|password_reset_link |
当我在生产中尝试此操作时,它会发送一封电子邮件,并在正文中打印:
<a href="*|password_reset_link|*">Change my password </a>
【问题讨论】:
你说你没有收到消息,但是消息发送了吗?您应该检查 Mandrill 控制台以查看是否正在发送消息。他们可能会进入您的垃圾邮件文件夹。 【参考方案1】:您的邮件程序可能工作正常,但这是我的评论版本:
/app/mailers/mandrill_devise_mailer.rb
require "mandrill"
class MandrillDeviseMailer < Devise::Mailer
default(
from: "email@yourdomain.com",
reply_to: "email@yourdomain.com"
)
def reset_password_instructions(record, token, opts=)
subject = "Password reset link from Your App"
# These become available in your Mailchimp template and can be used
# using the usual *|DISPLAY_NAME|*
merge_vars =
"PASSWORD_RESET_LINK" => "https://www.yourdomain.com/users/password/edit?reset_password_token=#token",
"DISPLAY_NAME" => record.display_name || record.email
# The name of your template in Mailchimp
# make sure to "send to mandrill" from the edit dropdown
body = mandrill_template("Password Reset Template", merge_vars)
send_mail(record.email, subject, body)
end
private
def send_mail(email, subject, body)
mail(to: email, subject: subject, body: body, content_type: "text/html")
end
def mandrill_template(template_name, attributes)
mandrill = Mandrill::API.new(ENV["SMTP_PASSWORD"])
merge_vars = attributes.map do |key, value|
name: key, content: value
end
mandrill.templates.render(template_name, [], merge_vars)["html"]
end
end
你的配置看起来也不错,但这是我的:
# /config/environments/production.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings =
address: ENV["SMTP_ADDRESS"],
authentication: :plain,
domain: ENV["SMTP_DOMAIN"],
enable_starttls_auto: true,
password: ENV["SMTP_PASSWORD"],
port: "587",
user_name: ENV["SMTP_USERNAME"]
config.action_mailer.default_url_options = host: ENV["SMTP_DOMAIN"]
您可能缺少的是 devise.rb
初始化程序更改,需要如下所示:
config.mailer_sender = "email@yourdomain.com"
# Configure the class responsible to send e-mails.
config.mailer = 'MandrillDeviseMailer'
【讨论】:
以上是关于Rails 4 Mailchimp Mandrill 与设计的主要内容,如果未能解决你的问题,请参考以下文章
Rails 4 Mailchimp Mandrill 与设计