我正在使用设计进行身份验证。它提供了忘记密码链接。当我发送电子邮件时,电子邮件不会发送。以下是我使用过的设置。你能告诉我为什么gmail没有发送电子邮件吗?我也打开了“允许不太安全的应用程序发送电子邮件”,我也在gmail设置中启用了imap。
application.rb具有以下设置。
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:domain => 'mail.google.com',
:port => 587,
:user_name => 'validemail@gmail.com',
:password => 'validpassword',
:authentication => 'login',
:enable_starttls_auto => true
}
development.rb有
config.action_mailer.default_url_options = { host: '127.0.0.1'}
config.action_mailer.delivery_method = :smtp
发送电子邮件后,我在控制台中收到以下文字。
Devise::Mailer#reset_password_instructions: processed outbound mail in 215.2ms
Sent mail to validemail@gmail.com (1097.6ms)
Date: Thu, 29 Dec 2016 09:50:41 +0000
From: please-change-me-at-config-initializers-devise@example.com
Reply-To: please-change-me-at-config-initializers-devise@example.com
To: validemail@gmail.com
Message-ID: <5864dc7161acb_173921a07788707d@kofhearts-rubyonrails-3267120.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello validemail@gmail.com!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><a href="http://127.0.0.1/users/password/edit?reset_password_token=WQxYad91mPghMxaurYA5">Change my password</a></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Redirected to https://rubyonrails-kofhearts.c9users.io/users/sign_in
Completed 302 Found in 1965ms (ActiveRecord: 14.7ms)
更新
我正在关注本教程。
https://www.youtube.com/watch?v=ZEk0Jp2dThc
发送电子邮件无法使用此视频中指定的设置。
答案 0 :(得分:1)
在我的帐户设置中启用安全性较低的应用对我有用。
Google帐户设置&gt;登录Google&gt;向下滚动
启用不太安全的应用。尝试再次发送电子邮件。
答案 1 :(得分:0)
这应该有用。
也许你也可以打开config.action_mailer.raise_delivery_errors = true
在config/environments/development.rb
。
它更容易调试。
也许您忘了在config.action_mailer.perform_deliveries = true
中设置config/environments/development.rb
?
答案 2 :(得分:0)
你试过这个吗?
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "myinfo@gmail.com",
:password => "secret",
:authentication => "plain"
# :enable_starttls_auto => true # I don't have this, but it should work anyway
}
它被发送可能是因为垃圾邮件过滤器而没有收到它,首先要检查:
class UserMailer < ActionMailer::Base
default :from => "myinfo@gmail.com"
# ...
end
您还要检查Gmail中的Forwarding and POP/IMAP
..
在development.rb
config.action_mailer.default_url_options = {host: 'your server' } # ex. localhost:3000
config.action_mailer.raise_delivery_errors = true # to raise error if smtp has error on setup
config.action_mailer.default :charset => "utf-8"
答案 3 :(得分:0)
rails s -e production
或打开config.action_mailer.raise_delivery_errors = true
mail().deliver
或Rails 5 + mail().delivery_now
看看这是我的SMTP配置
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => ENV['mailer_address'],
:port => ENV['mailer_port'],
:user_name => ENV['mailer_username'],
:password => ENV['mailer_password'],
:authentication => "plain",
:enable_starttls_auto => true
}
这是我的班级邮件程序
def send_email
mail(to: "example@email.com", subject: 'not reply this message').deliver
end
我从我的控制器调用该函数。如果您有任何错误,请打印它以检查问题所在。
begin
myMailer.send_email
rescue Exception => e
flash[:error] = "Imposible sent email, #{e.inspect}"
end