Search code examples
ruby-on-railsemailherokuconfigurationgmail

Email for user confirmation not sending in production. Rails, Heroku, Gmail


I have looked all over Google and stackoverflow for an answer, but have been unable to find any solution. I'm trying to set up smtp emailing for user confirmation after signing up. It works fine in development. Even with MailCatcher on, it bypasses it somehow and sends to the right email from my assigned gmail.

config.action_mailer.raise_deliver_errors = true
config.action_mailer.perform_deliveries = true
config.assets.raise_runtime_errors = false
config.assets.quiet = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
 :address        => "smtp.gmail.com",
 :enable_starttls_auto => true, 
 :domain => 'hieu-vo.herokuapp.com',
 :port           => 587 ,
 :user_name      => '[email protected]',
 :password       => 'xxxxxx',
 :authentication => 'plain',
 :openssl_verify_mode  => 'none' }
config.action_mailer.default_url_options = { host: 'hieu-vo.herokuapp.com' }

And here is the logs when I use heroku logs --tail

2018-09-11T16:23:35.044137+00:00 app[web.1]: I, [2018-09-11T16:23:35.043151 #4]  INFO -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] Started POST "/users" for 113.188.247.92 at 2018-09-11 16:23:35 +0000
2018-09-11T16:23:35.045638+00:00 app[web.1]: I, [2018-09-11T16:23:35.045531 #4]  INFO -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] Processing by UsersController#create as HTML
2018-09-11T16:23:35.045793+00:00 app[web.1]: I, [2018-09-11T16:23:35.045709 #4]  INFO -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"cJWQmMOh4zUFre1fps03tYuYbbyA8ykZZF3djpwUpe8KWfabD5PUA8ZaC4VQ8VmF9nGVNrxUOomPCRpsKJk7aQ==", "user"=>{"email"=>"[email protected]", "name"=>"hello"}, "commit"=>"Send to me"}
2018-09-11T16:23:35.050036+00:00 app[web.1]: D, [2018-09-11T16:23:35.049890 #4] DEBUG -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55]    (0.6ms)  BEGIN
2018-09-11T16:23:35.055921+00:00 app[web.1]: D, [2018-09-11T16:23:35.055783 #4] DEBUG -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55]   SQL (3.4ms)  INSERT INTO "users" ("name", "email", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["name", "hello"], ["email", "[email protected]"], ["created_at", "2018-09-11 16:23:35.050372"], ["updated_at", "2018-09-11 16:23:35.050372"]]
2018-09-11T16:23:35.059314+00:00 app[web.1]: D, [2018-09-11T16:23:35.059167 #4] DEBUG -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55]    (2.6ms)  COMMIT
2018-09-11T16:23:35.520783+00:00 heroku[router]: at=info method=POST path="/users" host=hieu-vo.herokuapp.com request_id=1b2a2e36-58d4-4845-acfd-2b6fa1621d55 fwd="113.188.247.92" dyno=web.1 connect=1ms service=495ms status=500 bytes=1827 protocol=https
2018-09-11T16:23:35.508523+00:00 app[web.1]: D, [2018-09-11T16:23:35.508354 #4] DEBUG -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] UserMailer#registration_confirmation: processed outbound mail in 447.9ms
2018-09-11T16:23:35.517607+00:00 app[web.1]: I, [2018-09-11T16:23:35.517469 #4]  INFO -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] Sent mail to [email protected] (8.8ms)
2018-09-11T16:23:35.517699+00:00 app[web.1]: D, [2018-09-11T16:23:35.517609 #4] DEBUG -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] Date: Tue, 11 Sep 2018 16:23:35 +0000
2018-09-11T16:23:35.517702+00:00 app[web.1]: From: [email protected]
2018-09-11T16:23:35.517704+00:00 app[web.1]: To: [email protected]
2018-09-11T16:23:35.517706+00:00 app[web.1]: Message-ID: <5b97ec077d6b2_411cabc85239@ba563531-54e7-4e4c-8f59-50279af29818.mail>
2018-09-11T16:23:35.517708+00:00 app[web.1]: Subject: [MAILTO HIEUVO] SOMEONE VISIT YOUR WEBSITE!!!
2018-09-11T16:23:35.517710+00:00 app[web.1]: Mime-Version: 1.0
2018-09-11T16:23:35.517711+00:00 app[web.1]: Content-Type: text/plain;
2018-09-11T16:23:35.517713+00:00 app[web.1]: charset=UTF-8
2018-09-11T16:23:35.517715+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
2018-09-11T16:23:35.517716+00:00 app[web.1]:
2018-09-11T16:23:35.517718+00:00 app[web.1]: [email protected] TALK TO YOU: hello
2018-09-11T16:23:35.518083+00:00 app[web.1]: I, [2018-09-11T16:23:35.517991 #4]  INFO -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] Completed 500 Internal Server Error in 472ms (ActiveRecord: 6.6ms)
2018-09-11T16:23:35.519035+00:00 app[web.1]: F, [2018-09-11T16:23:35.518945 #4] FATAL -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55]
2018-09-11T16:23:35.519131+00:00 app[web.1]: F, [2018-09-11T16:23:35.519042 #4] FATAL -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
2018-09-11T16:23:35.519216+00:00 app[web.1]: F, [2018-09-11T16:23:35.519135 #4] FATAL -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55]
2018-09-11T16:23:35.519334+00:00 app[web.1]: F, [2018-09-11T16:23:35.519255 #4] FATAL -- : [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] app/controllers/users_controller.rb:40:in `block in create'
2018-09-11T16:23:35.519337+00:00 app[web.1]: [1b2a2e36-58d4-4845-acfd-2b6fa1621d55] app/controllers/users_controller.rb:38:in `create'
2018-09-11T16:23:35.932275+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=hieu-vo.herokuapp.com request_id=f7d117e3-a805-41b3-b315-524ce64899c2 fwd="113.188.247.92" dyno=web.1 connect=4ms service=9ms status=200 bytes=143 protocol=https

I wonder if i was wrong with configuration, but in my local machine it still work fine.so it might be a users problem with heroku. Many thank!


Solution

  • Check this youtube video youtube.com/watch?v=INPqBOerfTw&t=63s

    I prefer to use mandrill app instead of gmail https://mandrillapp.com/login/?referrer=%2F

      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
          :address => "smtp.mandrillapp.com",
          :port => 587,
          :domain => 'www.xyz.com',
          :user_name => 'xyz',
          :password => 'xxx',
          :authentication => :plain,
          :enable_starttls_auto => true 
      }