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

时间:2016-06-16 13:19:04

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

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

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

不幸的是,在查看我的电子邮件后,我没有收到电子邮件

1 个答案:

答案 0 :(得分:1)

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

ContactMailer.send_email.deliver_now