2
votes

I want to send automated emails via Mailgun either SMTP or API. The problem is that in tutorials I find they explain how to do that manually e.i creating mailer class etc. For example like that:

def send_simple_message
  RestClient.post "https://api:YOUR_API_KEY"\
  "@api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
  :from => "Excited User <mailgun@YOUR_DOMAIN_NAME>",
  :to => "[email protected], YOU@YOUR_DOMAIN_NAME",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomness!"
end

This is from official Mailgun documentation.

But I am using Devise gem which has email sending implemented.

For example I want to send password reset email. When I click forgot password and submit my email from logs I see that my email is tried to be sent, but not sent of course, I need to set up email server.

So the question is where is this code for sending recovery email is written in devise, how to override it? I want it to oveeride so it will use Mailgun API for example.

I have already generated registrations_controller.rb using

rails generate devise:controllers registrations

command. So I suppose I am overriding it here? Any suggestions?

1

1 Answers

3
votes

have you read this tutorial? Looks like you need to setup it in config/environments/development.rb

Something like:

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: ENV['GMAIL_DOMAIN'],
authentication: 'plain',
user_name: ENV['GMAIL_USERNAME'],
password: ENV['GMAIL_PASSWORD']
}

Also, you can try to use mail gun gem. Looks like It's really easy to setup it

config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
        api_key: '<mailgun api key>',
        domain: '<mailgun domain>'
}

Hope it helps you.