为什么设计不通过gmail smtp发送电子邮件?
我正在使用设计进行身份验证。它提供了一个忘记的密码链接。当我发送电子邮件时,电子邮件不会被发送。以下是我使用的设置。你能告诉我为什么gmail不发送邮件吗?我还打开了“允许安全性较低的应用程序发送电子邮件”,并且我还启用了gmail设置中的imap。为什么设计不通过gmail smtp发送电子邮件?
application.rb具有以下设置。
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:domain => 'mail.google.com',
:port => 587,
:user_name => '[email protected]',
: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 [email protected] (1097.6ms)
Date: Thu, 29 Dec 2016 09:50:41 +0000
From: [email protected]
Reply-To: [email protected]
To: [email protected]
Message-ID: <[email protected]l>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello [email protected]!</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)
UPDATE:
我只是按照本教程。
https://www.youtube.com/watch?v=ZEk0Jp2dThc
发送电子邮件没有与在此视频中指定的设置工作。
这应该是有效的。 也许你也可以打开config.action_mailer.raise_delivery_errors = true
在config/environments/development.rb
。 调试起来更容易。
也许你忘了设置config.action_mailer.perform_deliveries = true
在config/environments/development.rb
?
谢谢,但即使添加perform_deliveries设置和raise_delivery_errors标志邮件没有发送。我已经使用控制台消息更新了我的帖子,该消息是在点击“发送重置密码指令”后打印的,该消息应该将电子邮件发送到提供的电子邮件。 – kofhearts
你试过吗?
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "[email protected]",
:password => "secret",
:authentication => "plain"
# :enable_starttls_auto => true # I don't have this, but it should work anyway
}
它它发送也许你不接受,因为垃圾邮件过滤器的话,首先要检查:
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
# ...
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"
- 如果您正在使用Gmail,您应激活 的权限。外部应用程序通过SMTP发送电子邮件。
-
在发展模式Rails不发送电子邮件,你应该运行在生产
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: "[email protected]", subject: 'not reply this message').deliver
end
我从我的控制器调用该函数。如果你有任何错误,你应该打印它来检查是什么问题。
begin
myMailer.send_email
rescue Exception => e
flash[:error] = "Imposible sent email, #{e.inspect}"
end
启用不够安全的应用在我的帐户设置为我工作。
谷歌帐户设置>登录到谷歌>向下滚动到下
打开允许安全性较低的应用程序。尝试再次发送电子邮件。
可能是重复http://stackoverflow.com/questions/25735206/rails-4-smsmpauthenticationerror-535-5-7-0-authentication-failed/25884665#25884665 –
我知道这不是你的答案问题,但我放弃了通过Gmail使用smtp。我现在使用'sendinblue',他们有一个smpt服务,每天约300封电子邮件免费。也是一个很好的红宝石,所以它很容易使用。 – Iceman