我有一个用户模型,其方法应在用户提交其电子邮件地址时发送欢迎电子邮件。设置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)
答案 0 :(得分:0)
问题出在signup_email.html.erb
文件中。图片来源链接有错误,因此我更改了这一行:
<img src="<%= Rails.application.config.action_mailer.default_url_options %>
对此:
<img src="<%= Rails.application.config.action_mailer.default_url_options[:host] %>
这是因为default_url_options是hash
。