我目前正在优先使用Devise Confirmable方法在用户确认其帐户后创建欢迎电子邮件。在当前设置下,在rails控制台中运行UserTransactionMailer.welcome_message(self).deliver_now会导致以下错误:
"NoMethodError: undefined method `email' for main:Object
from /Users/AnthonyEmtman/Documents/projects/Team_Development/kons/app/mailers/user_transaction_mailer.rb:6:in `welcome_message'"
以下是用于触发发送welcome_message电子邮件的user.rb中的def confirm!
覆盖。
models / user.rb:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable, :zxcvbnable
def confirm!
send_welcome_message
super
end
private
def send_welcome_message
UserTransactionMailer.welcome_message(self).deliver_now
end
end
以下文件是我的user_transaction_mailer.rb和base_mandrill_mailer.rb文件。 user_transaction_mailer.rb继承自base_mandrill_mailer.rb,因此我创建的所有未来邮件都可以访问mandrill_send方法,有效地减少了每次写入mandrill_send方法时需要写入的发送代码量。
寄件人/ user_transaction_mailer.rb:
class UserTransactionMailer < BaseMandrillMailer
def welcome_message(user, opts={})
options = {
:subject => "Welcome to Kontracking",
:email => user.email,
:global_merge_vars => [
{
name: "USER_NAME",
content: user.user_name
}
],
:template_name => "Welcome Message - Kontracking"
}
mandrill_send options
end
end
mailers / base_mandrill_mailer.rb:
require "mandrill"
class BaseMandrillMailer < ApplicationMailer
def mandrill_send(opts={})
message = {
:subject => "#{opts[:subject]}",
:from_name => "Kontracking",
:from_email => "admin@kontracking.com",
:to =>
[{"name" => "Some User",
"email" => "#{opts[:email]}",
"type" => "to"}],
:global_merge_vars => opts[:global_merge_vars]
}
sending = MANDRILL.messages.send_template opts[:template_name], [], message
rescue Mandrill::Error => e
Rails.logger.debug("#{e.class}: #{e.message}")
raise
end
end
在rails控制台中运行UserTransactionMailer.welcome_message(User.first).deliver_now会成功传递到我的电子邮件,包括正确处理我在电子邮件中显示的包含的user_name的merge_var。我对哈希没有经验,目前无法找到未定义方法问题的解决方案(可能相当简单)。如何使其正常工作?
另外,我目前要求使用'mandrill&#39;在初始化程序中,所以我应该能够从base_mandrill_mailer.rb文件中删除它,对吗?
答案 0 :(得分:0)
我解决了我的问题,因为当前的实现始终是自定义的。更改models / user.rb确认方法以包含自定义消息的用户和用户。
模型/ user.rb:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable, :zxcvbnable
def confirm!
send_welcome_message(self)
super
end
private
def send_welcome_message(user)
UserTransactionMailer.welcome_message(user).deliver_now
end
end
所有其他文件都是正确的。我建议这种方法适用于任何想要覆盖Devise邮件程序或特定方法的人,同时利用Mailchimp / Mandrill集成 - 特别是那些在开发团队中工作的人。此方法有效地允许您将电子邮件的设计和更新卸载到非技术成员,并且不需要将电子邮件视图更改部署到服务器(等待变量名称或电子邮件添加的更改)。
我已将我的文件包含在下面覆盖Devise中。
寄件人/ custom_devise_mailer.rb:
class CustomDeviseMailer < Devise::Mailer
def confirmation_instructions(record, token, opts={})
options = {
:subject => "Confirmation Instructions",
:email => record.email,
:global_merge_vars => [
{
name: "confirmation_link",
content: user_confirmation_url(confirmation_token: token)
}
],
:template_name => "Confirmation Instructions - Kontracking"
}
mandrill_send options
end
def reset_password_instructions(record, token, opts={})
options = {
:subject => "Password Reset Instructions",
:email => record.email,
:global_merge_vars => [
{
name: "password_reset_link",
content: reset_password_url(reset_password_token: record.reset_password_token)
}
],
:template_name => "Password Reset Instructions - Kontracking"
}
mandrill_send options
end
def unlock_instructions(record, token, opts={})
options = {
:subject => "Account Unlock Instructions",
:email => record.email,
:global_merge_vars => [
{
name: "account_unlock_link",
content: user_unlock_url(unlock_token: token)
}
],
:template_name => "Account Unlock Instructions - Kontracking"
}
mandrill_send options
end
def mandrill_send(opts={})
message = {
:subject => "#{opts[:subject]}",
:from_name => "Kontracking",
:from_email => "anthony.emtman@kredibleinc.com",
:to =>
[{"name" => "Some User",
"email" => "#{opts[:email]}",
"type" => "to"}],
:global_merge_vars => opts[:global_merge_vars]
}
sending = MANDRILL.messages.send_template opts[:template_name], [], message
rescue Mandrill::Error => e
Rails.logger.debug("#{e.class}: #{e.message}")
raise
end
end
您还需要更改Devise初始化程序中的邮件程序配置,以指向自定义邮件程序。
初始化/ devise.rb:
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'john.smith@example.com'
# Configure the class responsible to send e-mails.
config.mailer = 'CustomDeviseMailer'
我还为Mandrill设置了一个简单的初始化程序(我使用Mandrill API - 下面包含的文件)。
初始化/ mandrill.rb:
require 'mandrill'
MANDRILL = Mandrill::API.new ENV['SMTP_PASSWORD']
配置/环境/ production.rb:
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
config.action_mailer.default_url_options = { host: ENV["SMTP_DOMAIN"] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: ENV.fetch("SMTP_ADDRESS"),
authentication: :plain
domain: ENV.fetch("SMTP_DOMAIN"),
enable_starttls_auto: true,
password: ENV.fetch("SMTP_PASSWORD"),
port: "587",
user_name: ENV.fetch("SMTP_USERNAME")
}
配置/环境/ development.rb:
# Care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
host = 'localhost:3000'
config.action_mailer.default_url_options = { host: host }
config.action_mailer.perform_deliveries = true
配置/ application.yml:
SMTP_ADDRESS:smtp.mandrillapp.com SMTP_DOMAIN:localhost SMTP_PASSWORD:&#39;在此处插入您的Mandrill API密钥 - 没有引号&#39; SMTP_USERNAME:&#39;在此处插入您的Mandrill用户名 - 没有引号&#39;
我使用figaro来管理这些。我设置了一个初始化程序,因此如果它们没有在服务器上设置,则会导致错误。
初始化/ figaro.rb:
Figaro.require_keys("SMTP_ADDRESS", "SMTP_DOMAIN",
"SMTP_PASSWORD", "SMTP_USERNAME")
如果您有任何疑问,请与我联系!