【问题标题】:Trying to send email with Gmail STP (Rails)尝试使用 Gmail STP (Rails) 发送电子邮件
【发布时间】:2016-10-18 01:08:17
【问题描述】:

我正在尝试从我的网络应用程序发送一些电子邮件。这是我的配置:

//config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'domain.com',
user_name:            'myusername',
password:             'mypassword',
authentication:       'plain',
enable_starttls_auto: true  } 

// mailers/contact_mailer
class ContactMailer < ApplicationMailer
default from: 'from@gmail.com'

def send_email()
    mail(to: 'to@gmail.com', subject: 'Welcome to My Awesome Site')
end
end 

// views/contact_mailer/send_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, name</h1>
<p>
<p>Thanks for joining and have a great day!</p>
</body>
</html> 

所以为了测试它,我从 Rails 命令运行了这个命令:

ContactMailer.send_email

我得到了这个结果:

irb(main):003:0> ContactMailer.send_email
Rendered contact_mailer/send_email.html.erb within layouts/mailer (0.1ms)

ContactMailer#send_email: processed outbound mail in 10.4ms
=> #<Mail::Message:69980432504260, Multipart: false, Headers: <From:    adib.elaraki@gmail.com>, <To: adib.amine.mdr@gmail.com>, <Subject: Welcome   to My Awesome Site>, <Mime-Version: 1.0>, <Content-Type: text/html>>
irb(main):004:0> 

很遗憾,在查看了我的电子邮件后,我没有收到电子邮件

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 smtp gmail


    【解决方案1】:

    mail 只生成一个邮件对象。您需要附加 deliver_now 之类的内容才能实际发送。例如:

    ContactMailer.send_email.deliver_now
    

    【讨论】:

    • 看起来更好,但我现在收到此错误:Net::SMTPAuthenticationError: 534-5.7.14 accounts.google.com/signin/….但是我确实从我的 gmail 帐户启用了协议 POP/IMAP..
    • 我假设您已经更改了登录详细信息,实际上是 domain: 'domain.com', user_name: 'myusername', password: 'mypassword', ?
    • 我还记得在设置 SMTP 时需要执行类似 support.google.com/a/answer/6260879?hl=en 的操作,但禁用风险自负。
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2015-09-09
    • 2015-12-21
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多