Tom Hammond
Tom Hammond

Reputation: 6080

Rails Email Generated But Not Received

I'm trying to test sending an email in development right now. According to my logs the email was sent but I never actually see it arriving nor in the spam folder. This is my first time trying to use a mailer so I'm probably just missing something basic. Any idea where I'm going wrong?

My mailer:

class WelcomeMailer < ActionMailer::Base
  default from: "[email protected]"

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

My development.rb has these set:

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

Here's my logs:

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"=>"[email protected]", "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('[email protected]') 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", "[email protected]"], ["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 [email protected] (10.0ms)
Date: Sun, 07 Sep 2014 09:45:13 -0500
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
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, [email protected]
===============================================

You have successfully signed up to example.com,
your username is: [email protected].

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, [email protected]</h1>
    <p>
      You have successfully signed up to example.com,
      your username is: [email protected].<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--

Edit: adding my SendGrid code that's setup in Heroku - just add the addon and add this to your 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  } 

Upvotes: 1

Views: 183

Answers (2)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42789

I usually use mail catcher as a mail server, it starts a local server and receives all mails on it, no matter who's sending it and to whom, pretty useful.

Upvotes: 1

Mandeep
Mandeep

Reputation: 9173

If you want to receive your email in a mailbox then you'll need to setup some mail server. I'll recommend you to use Sendgrid. If you just want to check how action mailer send mails then you can also use your Gmail account with following settings

#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  } 

Also inside your mailer method instead of hard-coding your link i'll recommend you to use url helpers

Upvotes: 1

Related Questions