2

I think I've set up ActionMailer successfully, however emails do not arrive in their inbox, despite the server stating differently:

Sent mail to julian@designimperial.com (2003ms)
Date: Fri, 30 Aug 2013 22:26:41 +0100
From: from@example.com
To: julian@gmail.com
Message-ID: <52210e11bedc9_16501d84f64257a@Julian-PC.mail>
Subject: Welcome to my awesome site!
Mime-Version: 1.0
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, Julian</h1>
    <p>
      You have successfully signed up to example.com,
      your username is: gogo.<br/>
    </p>
    <p>
      To login to the site, just follow this link: http://example.com/login.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

Do I need to upload my rails application to a server or heroku inorder for emails to be sent? I know this is the case with PHP mailing and MAMP/XAMPP

Starkers
  • 10,273
  • 21
  • 95
  • 158

2 Answers2

3

Having incorrect settings can result in email failing to send without raising any exceptions. Assuming you're using the SMTP protocol to deliver main, you should ensure that you've properly setup your ActionMailer configuration. Here's a basic configuration for utilizing Gmail (via the Rails Guides):

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  }
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • I'm following this tutorial, http://guides.rubyonrails.org/action_mailer_basics.html, and those parameters aren't mentioned. Are they for Rails 3? Thanks for saying that emails can be sent without exceptions! It's probably a stupid error of mine somewhere... – Starkers Aug 30 '13 at 22:10
  • Those parameters are explicitly stated in the guide you cited. Check out section 6.2 here: http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail – zeantsoi Aug 30 '13 at 22:15
  • Ah. Embarrassing. It is 2:00am here :P – Starkers Aug 30 '13 at 22:20
  • Did this help resolve your issue? If so, would you kindly consider accepting/voting up this answer? – zeantsoi Aug 30 '13 at 22:22
  • I've already upvoted! I'll have a crack at this tomorrow and mark as correct – Starkers Aug 30 '13 at 22:26
  • Did you have any luck with this? – zeantsoi Sep 01 '13 at 21:37
  • You can also use a gem like `mailcatcher` for a way to receive emails you send and to preview what they'll look like to an end user; see http://ifelse.io/2014/07/27/send-and-receive-mail-locally-in-rails/ – markthethomas Jul 27 '14 at 19:25
2

Enabling delivery errors regarding the action mailer to be raised is absurdly helpful in getting the action mailer working. It's also disabled by default.

To get it working, open your development environment file, located at:

yourappname/config/environments/development.rb

around line 17, there is a method that enables and disables error reporting by taking a boolean:

  config.action_mailer.raise_delivery_errors = false

Change it to 'true', and restart the rails server. You now have excellent error reporting from rails.

I also like to put my smtp settings directly below in the same file like this:

  #SMTP settings
  config.action_mailer.delivery_method = :smtp 
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "gmail.com",
    user_name: "mrexample",
    password: "01password02"
  }  

Remember to put your smtp settins in your production.rb and test.rb environment configuration files as well. This example hash is also 100% correct if you're using gmail as the sender address of the emails. The only strings you need to change are the password and username.

Another thing that isn't glaringly obvious with gmail is that your username is everything to left of the @ symbol, and the domain everything to the right of the @.

e.g

mrexample@gmail.com

==

  config.action_mailer.smtp_settings = {
   # address: "smtp.gmail.com",
   # port: 587,
    domain: "gmail.com",
    user_name: "mrexample",
   # password: "01password02"
  }  

Finally, if you're getting the error message

"Errno::ECONNREFUSED (No connection could be made because the target machine actively refused it. - connect(2))"

Chances are the only thing wrong with your application is your smtp settings. Probably your password.

Starkers
  • 10,273
  • 21
  • 95
  • 158