Sending Email through Mailgun API in Rails

Add gem to Gemfile.

gem 'mailgun-ruby', '~>1.1.6'

Define mail gun api key in the secrets.yml:

development:
  secret_key_base: d8fc3a9ced2de8ed70
  mail_gun_api_key: key-123456

In the rails console:

client = Mailgun::Client.new(Rails.application.secrets.mail_gun_api_key)
email =  { from: 'bala.paranj@zepho.com',
           to:   'bparanj@gmail.com',
           subject: 'The Ruby SDK is awesome!',
           text:    'It is really easy to send a message!'
         }
client.send_message 'zepho.com', email
Mailgun::CommunicationError (Permission bits for '/Users/bparanj/.netrc' should be 0600, but are 644)

This fails to send email in development environment. Change the permissions on the .netrc file.

$ ll ~/.netrc
-rw-r--r--  1 bparanj  staff  70 May 21  2017 /Users/bparanj/.netrc
$ chmod 600 $HOME/.netrc
$ ll ~/.netrc
-rw-------  1 bparanj  staff  70 May 21  2017 /Users/bparanj/.netrc

Permission problem is resolved. The new problem is that my domain zepho.com does not have anything hosted. The new error is:

Mailgun::CommunicationError (404 Not Found: Domain not found: zepho.com)


Related Articles