IvanHid
IvanHid

Reputation: 709

Rails mails "Sent" but never recieved

Hello I'm trying to send an email from my rails application and I've tried multiple solutions from questions similar to this one but it still doesn't work, here is my UserMailer:

class UserMailer < ActionMailer::Base
    def welcome_email(user)
        @user = user
        @url = 'http://fundacionccb2017.herokuapp.com'
        mail(to: '[email protected]', subject: 'Bienvenido a la aplicación de ccb')
    end
end

Here is my welcome_email html

    <!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up to example.com,
      your username is: Pancho.<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>

And here's how I call it in my controller (inside my Users controller):

def create
        @user = User.new(user_params)
        if @user.save
                        UserMailer.welcome_email(@user).deliver_later
            session[:user_id] = @user.id
            redirect_to user_path(@user)
        else
            render 'new'
        end
    end

And this is my message in console:

Started POST "/users" for 127.0.0.1 at 2018-01-31 14:10:31 -0600
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"COkRNuVqcaqn2Wrso24y7lmWyKt1OX0/gai2FlXfeLBg9R61RmJj81JDsXdiuBZgBxKi4nW6WErl99bDMVp0kA==", "user"=>{"name"=>"Usuario", "last_name"=>"Apellido1", "second_last_name"=>"Apellido2", "mail"=>"[email protected]", "username"=>"Usuario6", "telephone_number"=>"555555555555", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
Unpermitted parameter: password_confirmation
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."mail") = LOWER(?) LIMIT ?  [["mail", "[email protected]"], ["LIMIT", 1]]
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER(?) LIMIT ?  [["username", "Usuario6"], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "users" ("name", "mail", "telephone_number", "last_name", "second_last_name", "username", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["name", "Usuario"], ["mail", "[email protected]"], ["telephone_number", "555555555555"], ["last_name", "Apellido1"], ["second_last_name", "Apellido2"], ["username", "Usuario6"], ["password_digest", "$2a$10$jdHJTDiCxL9NPg6ajMye9eZ251RVwfVBAUb2YZEKNvc05lYpqGnFa"], ["created_at", 2018-01-31 20:10:31 UTC], ["updated_at", 2018-01-31 20:10:31 UTC]]
   (4.5ms)  commit transaction
[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 922ae0c1-b5ed-4339-b25b-40752f651b9e) to Async(mailers) with arguments: "UserMailer", "welcome_email", "deliver_now", #<GlobalID:0x007f8f94ebd9a0 @uri=#<URI::GID gid://fundacion-ccb/User/7>>
Redirected to http://localhost:3000/users/7
Completed 302 Found in 567ms (ActiveRecord: 6.8ms)


  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Performing ActionMailer::DeliveryJob from Async(mailers) with arguments: "UserMailer", "welcome_email", "deliver_now", #<GlobalID:0x007f8f94e04a90 @uri=#<URI::GID gid://fundacion-ccb/User/7>>
Started GET "/users/7" for 127.0.0.1 at 2018-01-31 14:10:32 -0600
Processing by UsersController#show as HTML
  Parameters: {"id"=>"7"}
  User Load (1.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendering user_mailer/welcome_email.html.erb
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendered user_mailer/welcome_email.html.erb (0.9ms)
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendering user_mailer/welcome_email.text.erb
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendered user_mailer/welcome_email.text.erb (0.3ms)
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] UserMailer#welcome_email: processed outbound mail in 356.4ms
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Sent mail to [email protected] (3.6ms)
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Date: Wed, 31 Jan 2018 14:10:32 -0600
To: [email protected]
Message-ID: <[email protected]>
Subject: =?UTF-8?Q?Bienvenido_a_la_aplicaci=C3=B3n_de_ccb?=
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5a7222b8645e5_c343fc7ca75eb689082b";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5a7222b8645e5_c343fc7ca75eb689082b
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: base64

QmllbnZlbmlkbyBhIGxhIGZ1bmRhY2nDs24hCj09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PQpFc3RlIG1lbnNhamUgaGEgc2lkbyBlbnRyZWdhZG8g
ZXhpc3Rvc2FtZW50ZS4K

----==_mimepart_5a7222b8645e5_c343fc7ca75eb689082b
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, Usuario</h1>
    <p>
      You have successfully signed up to example.com,
      your username is: Pancho.<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_5a7222b8645e5_c343fc7ca75eb689082b--

[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Performed ActionMailer::DeliveryJob from Async(mailers) in 373.06ms
  Rendering users/show.html.erb within layouts/application
   (0.1ms)  SELECT COUNT(*) FROM "projects" WHERE "projects"."user_id" = ?  [["user_id", 7]]
  Rendered users/show.html.erb within layouts/application (476.8ms)
  Rendered layouts/_navigation.html.erb (1.7ms)
Completed 200 OK in 1027ms (Views: 632.3ms | ActiveRecord: 2.2ms)

As you can see the email is 'sent' but I never recieved it in my inbox, I have tried with both @gmail and @hotmail accounts. How can I fix this?

Upvotes: 0

Views: 176

Answers (2)

Mrunal Selokar
Mrunal Selokar

Reputation: 177

This is because you might not have configured AcionMailer correctly. In file "/config/environments/production.rb" or "/config/environments/development.rb" according to the environments you are in, add following lines:

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 address:              'smtp.gmail.com',
 port:                 587,
 user_name:            'your_gmail_username',
 password:             'your_gmail_password',
 authentication:       'plain',
 enable_starttls_auto: true
}

You can also use a gem called mailcatcher. In your "/config/environments/development.rb" file, add the following lines:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 }

Restart all services. In your console, move into your project directory and type mailcatcher. You should now be able to see all the sent and received emails via your app at url: http://127.0.0.1:1080

Thanks!

Upvotes: 3

razvans
razvans

Reputation: 3251

If you are in development you need to set up action mailer to actually send the email. You can use your gmail to do that. See here: Action mailer SMTP google apps

Or you can use letter_opener, and probably others like mailcatcher. I used letter_opener successfully.

Upvotes: 0

Related Questions