我正在尝试让Rails发送电子邮件。它似乎工作,我没有在我的本地服务器上得到任何错误,但我无法收到任何电子邮件。一切似乎工作正常,只是因为没有抛出错误,但电子邮件没有被传递。
application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'test@example.com'
layout 'send_message'
end
mailer.rb
class Mailer < ApplicationMailer
default from: "test@example.com"
layout 'send_message'
def send_message
mail(to: 'test_email@test.com', subject: 'Sample Email')do |format|
format.html { render layout: 'send_message' }
format.text
end
end
end
development.rb
config.action_mailer.raise_delivery_errors = false
application.rb
我尝试了几种不同的方法,一种方式是不对此文件进行更改,另一种方式是使用下面的更改
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => '25',
:authentication => :plain,
:domain => 'gmail.com',
:user_name => 'my_gmail_email@gmail.com',
:password => 'password'
}
电子邮件在我的控制器中发送
def create
@collected_email = CollectedEmail.new(collected_email_params)
if @collected_email.save
Mailer.send_message.deliver_now
render json: @collected_email, status: :created, location: @collected_email
else
render json: @collected_email.errors, status: :unprocessable_entity
end
end
我有两个视图视图/布局和视图/邮件程序。我不确定为什么这是必要的,但如果我删除一个邮件程序不起作用。在每个视图文件夹中,我有一个send_message.html.erb和一个send_message.text.erb,它们非常简单,并且它们在每个视图文件夹之间完全相同。
send_message.text.erb
tester123
send_message.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com</h1>
<p>
You have successfully signed up to example.com,
your username is:<br>
</p>
<p>
To login to the site, just follow this link.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
这是我的服务器日志中的一个条目。
Started POST "/collected_emails" for ::1 at 2015-08-06 21:09:22 -0400
ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by CollectedEmailsController#create as */*
Parameters: {"collected_email"=>{"email"=>"afsdlkjafs@asdfljk.cc"}}
(0.2ms) BEGIN
CollectedEmail Exists (0.9ms) SELECT 1 AS one FROM "collected_emails" WHERE ("collected_emails"."email" = 'afsdlkjafs@asdfljk.cc' AND "collected_emails"."email" = 'afsdlkjafs@asdfljk.cc') LIMIT 1
SQL (0.4ms) INSERT INTO "collected_emails" ("email", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["email", "afsdlkjafs@asdfljk.cc"], ["created_at", "2015-08-07 01:09:22.533596"], ["updated_at", "2015-08-07 01:09:22.533596"]]
(14.9ms) COMMIT
Rendered mailer/send_message.html.erb within layouts/send_message (2.2ms)
Rendered mailer/send_message.text.erb within layouts/send_message (0.6ms)
Mailer#send_message: processed outbound mail in 426.4ms
Sent mail to testeremail@email.com (30020.6ms)
Date: Thu, 06 Aug 2015 21:09:23 -0400
From: test@example.com
To: testeremail@email.com
Message-ID: <55c405439564_42a63fd0b5b8f02c676b8@William-Sargents-MacBook- Pro.local.mail>
Subject: Sample Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_55c4054363e0_42a63fd0b5b8f02c67578";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_55c4054363e0_42a63fd0b5b8f02c67578
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
tester123
----==_mimepart_55c4054363e0_42a63fd0b5b8f02c67578
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com</h1>
<p>
You have successfully signed up to example.com,
your username is:<br>
</p>
<p>
To login to the site, just follow this link.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
----==_mimepart_55c4054363e0_42a63fd0b5b8f02c67578--
Completed 201 Created in 30677ms (Views: 1.4ms | ActiveRecord: 19.4ms)