问题描述
我正面临一个奇怪的错误。
I'm facing a weird bug.
尝试在用户注册后设置欢迎电子邮件。
Trying to set up welcome email after a user's registration.
根据服务器日志,我的代码发送电子邮件,但用户没有收到。
According to server logs, my code sends an email but a user doesn't receive it.
这是我的代码:
models / user.rb
after_create :welcome_message
private
def welcome_message
UserMailer.welcome_email(self).deliver
end
和 mailers / user_mailer.rb
def welcome_email(user)
@user = user
mail to: @user.email, subject: t("mailer.welcome_email.welcome")
end
这里是环境。这两个错误发生在他们的
here are environments. The error occurs in both of them
config / environments / development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'localhost:3000',
user_name: '[email protected]',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
和配置/环境/production.rb
config.action_mailer.default_url_options = { :host => 'mysite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'mysite.com',
user_name: '[email protected]',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
我正在使用Devise注册用户。所以,也试图通过以下方式来做到:确认。结果是一样的 - 测试用户没有收到欢迎电子邮件。
I'm using Devise to register users. So, also tried to do that via :confirmable. Outcome is the same – test user doesn't get a welcome email.
Gemfile
gem 'rails', '3.2.12'
gem 'devise', '3.0.3'
gem "mail"
这里是服务器日志
Sent mail to [email protected] (1920ms)
Date: Sat, 10 May 2014
From: [email protected]
Reply-To: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome [email protected]!</p>
最后,[email protected]最终没有欢迎电邮
Yet eventually there is no welcome email at [email protected]
任何想法?
UPD:
在互联网上我看到没有人面临同样的问题
所以,我猜猜我只是错过了一些东西。但是不能弄清楚什么和在哪里。请帮助我。
UPD:on the internet I see that nobody else faces the same problemSo, I guess I'm just missing something. But cannot figure out what and where. Please help me.
推荐答案
尝试这样: -
在config / initializers /文件夹中命名为setup_mail.rb的文件,使其看起来像
Create a file named as setup_mail.rb in config/initializers/ folder so that it looks like
config/initializers/setup_mail.rb
并将以下代码放在该文件中: -
and put the below code in that file:-
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:domain => "mysite.com",
:port => 80,
:user_name => "username",
:password => "blahblahblah",
:authentication => :plain
}
ActionMailer::Base.default_url_options[:host] = "mysite.com"
从 config / environments / development.rb 和 config / environments / production.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: '587',
domain: 'localhost:3000',
user_name: '[email protected]',
password: 'blahblahblah',
authentication: 'plain',
enable_starttls_auto: true
}
这篇关于电子邮件:服务器发送但用户没有收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!