我有两个问题,过去 3 天我一直在浪费大量时间。我非常感谢有经验的编码人员在这里帮助我。
我有一个模型User
,我使用来自注册控制器的 Devise 邀请发送确认电子邮件。
User.invite!({:email => email, ...)
现在我有这些设置
在 config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: 'mail.myapp.com',
port: 26,
domain: 'myapp.com',
user_name: 'notifications@myapp.com',
password: 'xxxx',
authentication: 'plain',
enable_starttls_auto: true,
openssl_verify_mode: 'none'
}
和config/devise.rb
config.mailer_sender = 'notifications@myapp.com'
在调试输出显示“Sent email to ...”后,这封邮件正好在 20 分钟后送达
现在,我还在代码中的其他位置使用自定义 NotificationsMailer,除了基于设计的注册过程之外,我在其中发送通知电子邮件。我输入了config/initializers/setup_mail.rb
与上面相同的设置,在这种情况下,邮件是即时发送的。为什么会延迟?
问题/疑问#2 我想如果我可以拦截发送邮件的传递过程,我将能够通过日志记录检查 Mail::Message 对象,然后使用 Sidekiq 开发一种异步处理方式。
所以在config/application.rb
ActionMailer::Base.add_delivery_method :queued, :QueuedDelivery
在 config/environments/development.rb
config.action_mailer.delivery_method = :queued
在lib/queued_delivery.rb
class QueuedDelivery
def deliver!(mail)
logger.debug "mail object string rep: #{mail.to_s}"
logger.debug "mail object settings: #{mail.deliver_method.settings}"
end
end
尽管更改了发送方式,但奇怪的是,Devise 仍然像往常一样发送迟到 20 分钟的邮件,而且我看不到我上面放置的调试级别日志记录。
Devise 是如何发送这些电子邮件的?!我签devise_mail()
入了设计 gem,它最终使用了这个代码mail headers_for(action, opts)
。这里发生了什么?