【问题标题】:Rails user mailer not sending emails in productionRails 用户邮件程序未在生产中发送电子邮件
【发布时间】:2019-12-12 23:07:09
【问题描述】:

我有一个用户模型,其方法应在用户提交电子邮件地址时发送欢迎电子邮件。设置 sendgrid 后,我提交电子邮件时不会触发任何电子邮件。

这是我的 production.rb 代码:

config.action_mailer.default_url_options = { :host => ENV['DEFAULT_MAILER_HOST'] }

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => 'smtp.sendgrid.net',
    :port => '587',
    :authentication => :plain,
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :domain => 'heroku.com',
    :enable_starttls_auto => true
  }

部分用户模型:

class User < ActiveRecord::Base

  validates :email, presence: true, uniqueness: true, format: {
    with: /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i,
    message: 'Invalid email format.'
  }

  after_create :send_welcome_email

  private

  def send_welcome_email
    UserMailer.delay.signup_email(self)
  end
end

用户邮件代码:

class UserMailer < ActionMailer::Base
  default from: "Example <welcome@example.com>"

  def signup_email(user)
    @user = user

    mail(:to => user.email, :subject => "Thank you for joining!")
  end
end

heroku 日志

2019-08-04T20:23:22.053028+00:00 app[web.1]:  (2.0ms)  COMMIT
2019-08-04T20:23:22.054171+00:00 app[web.1]:  (0.6ms)  BEGIN
2019-08-04T20:23:22.056282+00:00 app[web.1]: User Exists (0.8ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'max.ston@protonmail.com' LIMIT 1
2019-08-04T20:23:22.058729+00:00 app[web.1]: User Exists (1.6ms)  SELECT  1 AS one FROM "users" WHERE "users"."referral_code" IS NULL LIMIT 1
2019-08-04T20:23:22.067815+00:00 app[web.1]: User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."referral_code" = $1 LIMIT 1  [["referral_code", "49af69325b"]]
2019-08-04T20:23:22.072798+00:00 app[web.1]: SQL (1.2ms)  INSERT INTO "users" ("email", "created_at", "updated_at", "referral_code") VALUES ($1, $2, $3, $4) RETURNING "id"  [["email", "max.ston@protonmail.com"], ["created_at", "2019-08-04 20:23:22.059018"], ["updated_at", "2019-08-04 20:23:22.059018"], ["referral_code", "49af69325b"]]
2019-08-04T20:23:22.109242+00:00 app[web.1]: SQL (1.3ms)  INSERT INTO "delayed_jobs" ("handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["handler", "--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class 'UserMailer'\nmethod_name: :signup_email\nargs:\n- !ruby/object:User\n  raw_attributes:\n    email: max.ston@protonmail.com\n    id: '3'\n    referral_code: 49af69325b\n    created_at: &1 2019-08-04 20:23:22.059018923 Z\n    updated_at: *1\n    referrer_id: \n  attributes: !ruby/object:ActiveRecord::AttributeSet\n    attributes: !ruby/object:ActiveRecord::LazyAttributeHash\n      types:\n        id: &3 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer\n          precision: \n          scale: \n          limit: \n          range: !ruby/range\n            begin: -2147483648\n            end: 2147483648\n            excl: true\n        email: &2 !ruby/object:ActiveRecord::Type::String\n          precision: \n          scale: \n          limit: \n        referral_code: *2\n        referrer_id: *3\n        created_at: &5 !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter\n          subtype: &4 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime\n            precision: \n            scale: \n            limit: \n        updated_at: &6 !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter\n          subtype: *4\n      values:\n        id: \n        email: \n        referral_code: \n        referrer_id: \n        created_at: \n        updated_at: \n      additional_types: {}\n      materialized: true\n      delegate_hash:\n        email: !ruby/object:ActiveRecord::Attribute::FromUser\n          name: email\n          value_before_type_cast: max.ston@protonmail.com\n          type: *2\n          value: max.ston@protonmail.com\n        id: !ruby/object:ActiveRecord::Attribute::FromUser\n          name: id\n          value_before_type_cast: '3'\n          type: *3\n          value: 3\n        referral_code: !ruby/object:ActiveRecord::Attribute::FromUser\n          name: referral_code\n          value_before_type_cast: 49af69325b\n          type: *2\n          value: 49af69325b\n        created_at: !ruby/object:ActiveRecord::Attribute::FromUser\n          name: created_at\n          value_before_type_cast: *1\n          type: *5\n          value: 2019-08-04 20:23:22.059018923 Z\n        updated_at: !ruby/object:ActiveRecord::Attribute::FromUser\n          name: updated_at\n          value_before_type_cast: *1\n          type: *6\n          value: 2019-08-04 20:23:22.059018923 Z\n        referrer_id: !ruby/object:ActiveRecord::Attribute::FromDatabase\n          name: referrer_id\n          value_before_type_cast: \n          type: *3\n  new_record: false\n  active_record_yaml_version: 0\n"], ["run_at", "2019-08-04 20:23:22.105001"], ["created_at", "2019-08-04 20:23:22.105297"], ["updated_at", "2019-08-04 20:23:22.105297"]]
2019-08-04T20:23:22.112611+00:00 app[web.1]:  (1.8ms)  COMMIT
2019-08-04T20:23:22.113058+00:00 app[web.1]: Redirected to https://enigmatic-temple-XXXXX.herokuapp.com/refer-a-friend
2019-08-04T20:23:22.113229+00:00 app[web.1]: Completed 302 Found in 70ms (ActiveRecord: 16.4ms)

【问题讨论】:

  • 创建用户时,日志文件中是否有任何条目? UserMailer.delay.signup_email 中的 delay 表示您正在使用某种后台作业。您是如何配置这些作业运行器的?
  • 我运行了“heroku rake jobs:work”并收到了这条消息:``` UserMailer#signup_email: 已在 20.7 毫秒内处理出站邮件 [Worker(host:bb4cc50d-c1fc-475a-b1ff-2004358ed429 pid :4)] Job UserMailer.signup_email (id=1) FAILED (0 次尝试) with ActionView::Template::Error: 没有将哈希隐式转换为字符串 2019-08-05T13:55:12+0000: [Worker(主机:bb4cc50d-c1fc-475a-b1ff-2004358ed429 pid:4)] Job UserMailer.signup_email (id=1) 失败(0 次尝试)与 ActionView::Template::Error: 哈希没有隐式转换为字符串```
  • 你能在你的问题中发布完整的回溯错误吗?看起来错误告诉你问题出在哪里。
  • @MaxRah 它说您的视图中有错误。请发布视图文件和完整的堆栈跟踪。
  • 修复了这个问题。 user mailer 视图中有几个语法错误。谢谢大家。

标签: ruby email ruby-on-rails-4 heroku sendgrid


【解决方案1】:

问题出在signup_email.html.erb 文件中。图片来源链接有错误,所以我改了行:

&lt;img src="&lt;%= Rails.application.config.action_mailer.default_url_options %&gt;

到这里:

&lt;img src="&lt;%= Rails.application.config.action_mailer.default_url_options[:host] %&gt;

这是因为 default_url_options 是 hash

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 2019-02-16
    • 2014-10-01
    • 1970-01-01
    • 2013-12-09
    • 2013-12-19
    相关资源
    最近更新 更多