尝试使用 Gmail STP 发送电子邮件 (Rails)

Trying to send email with Gmail STP (Rails)

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

//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> 

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

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

ContactMailer.send_email.deliver_now