Sending email from localhost with ActionMailer

Most tutorials focus on setting up ActionMailer to send smtp emails via a gmail account.

But, if we are talking about your production server, then it is even simpler to send smtp email from your localhost:

  # Install postfix, which is a program that allows your server to send out smtp emails
  
  sudo apt-get install postfix

  # ... and accept all the defaults

And none of the below configuration necessary anymore, so it actually cleans things up!

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "railscasts.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"],
    password: ENV["GMAIL_PASSWORD"]
  }

If you run into a

hostname "localhost" does not match the server certificate

error, then you can add:

  # in config/application.rb or in the appropriate env file in config/environments/, add:
  config.action_mailer.smtp_settings = { enable_starttls_auto: false  }

This works well from the production server, but will likely NOT work from your development machine, since the emails will be rejected as coming from a non-trusted server. For instance, I found this below message in my Postfix log. So I recommend using the mailcatcher gem in your development environment instead.


# message in var/log/mail.log telling me my email was rejected as non-trusted:
A96DD803F0: to=, relay=gmail-smtp-in.l.google.com[173.194.79.27]:25, delay=4.8, delays=0.07/0/2.2/2.6, dsn=5.7.1, status=bounced (host gmail-smtp-in.l.google.com[173.194.79.27] said: 550-5.7.1 [116.25.10.5] The IP you're using to send mail is not authorized to 550-5.7.1 send email directly to our servers. Please use the SMTP relay at your 550-5.7.1 service provider instead. Learn more at 550 5.7.1 http://support.google.com/mail/bin/answer.py?answer=10336 ei3si11330705pbc.80 - gsmtp (in reply to end of DATA command))

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s