Rails生成电子邮件但未收到

时间:2014-09-07 14:52:12

标签: ruby-on-rails actionmailer

我正试图在开发中测试发送电子邮件。根据我的日志,电子邮件已发送,但我从未真正看到它到达,也没有在垃圾邮件文件夹中。这是我第一次尝试使用邮件,所以我可能只是遗漏了一些基本的东西。我知道哪里出错了?

我的邮件:

class WelcomeMailer < ActionMailer::Base
  default from: "judy@example.com"

  def welcome_email(user)
    @user = user 
    @url = 'http://localhost:3000/signin'
    mail(to: @user.email, subject: 'Welcome to TheoremReach')
  end
end

我的development.rb有这些设置:

  config.action_mailer.perform_deliveries = true 
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000'}

这是我的日志:

Started POST "/users" for 127.0.0.1 at 2014-09-07 09:45:13 -0500
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"EZk7Tk0aYgo6LNElJoKxIvjaQW1+v9w/9VJaBnDGjKo=", "user"=>{"email"=>"example@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
  [1m[35m (0.0ms)[0m  begin transaction
  [1m[36mUser Exists (0.0ms)[0m  [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('example@gmail.com') LIMIT 1[0m
Binary data inserted for `string` type on column `password_digest`
  [1m[35mSQL (2.0ms)[0m  INSERT INTO "users" ("created_at", "email", "password_digest", "remember_token", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Sun, 07 Sep 2014 14:45:13 UTC +00:00], ["email", "example@gmail.com"], ["password_digest", "$2a$10$m0uR9SvhakmmG4N0EjZOO.yZ.HDJ3Zr0qopHcGtPT0kekwmX1lUJu"], ["remember_token", "db605c9854f526e622164911e4491d9e2b9ceae0"], ["updated_at", Sun, 07 Sep 2014 14:45:13 UTC +00:00]]
  [1m[36m (24.0ms)[0m  [1mcommit transaction[0m
  Rendered survey_mailer/welcome_email.html.erb (0.0ms)
  Rendered survey_mailer/welcome_email.text.erb (0.0ms)

Sent mail to example@gmail.com (10.0ms)
Date: Sun, 07 Sep 2014 09:45:13 -0500
From: judy@epic.com
To: example@gmail.com
Message-ID: <540c6f7971a46_248c344a18c34247@EPIC25234.mail>
Subject: Welcome to TheoremReach
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_540c6f7970aa5_248c344a18c3418a";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_540c6f7970aa5_248c344a18c3418a
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Welcome to example.com, example@gmail.com
===============================================

You have successfully signed up to example.com,
your username is: example@gmail.com.

To login to the site, just follow this link: http://localhost:3000/signin.

Thanks for joining and have a great day!
----==_mimepart_540c6f7970aa5_248c344a18c3418a
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, example@gmail.com</h1>
    <p>
      You have successfully signed up to example.com,
      your username is: example@gmail.com.<br>
    </p>
    <p>
      To login to the site, just follow this link: http://localhost:3000/signin.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>
----==_mimepart_540c6f7970aa5_248c344a18c3418a--

编辑:添加我在Heroku中设置的SendGrid代码 - 只需添加插件并将其添加到您的production.rb:

config.action_mailer.default_url_options = { :host => 'example.com'}

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:              'smtp.sendgrid.net',
  port:                 '587',
  domain:               'heroku.com',
  user_name:            ENV['SENDGRID_USERNAME'],
  password:             ENV['SENDGRID_PASSWORD'],
  authentication:       :plain,
  enable_starttls_auto: true  } 

2 个答案:

答案 0 :(得分:1)

如果您想在邮箱中收到电子邮件,则需要设置一些邮件服务器 。我建议你使用Sendgrid。如果您只是想检查动作邮件如何发送邮件,那么 您还可以use your Gmail account进行以下设置

#development.rb
config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000'}

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'example.com',
user_name:            '<username>',
password:             '<password>',
authentication:       'plain',
enable_starttls_auto: true  } 

另外在您的邮件方法中,而不是对链接进行硬编码,我建议您使用url helpers

答案 1 :(得分:1)

我通常使用mail catcher作为邮件服务器,它启动本地服务器并接收所有邮件,无论是谁发送邮件给谁,非常有用。