Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailgun for Rails Application

At the moment I am working on implementing Mailgun into my rails application but I seem to be confused as to the best method.

The way I have already implemented is to setup the areas, add the gemfile and then setup the background processor through Active Job. I have not yet setup the background processor due to being unsure if this is the best way.

I've seen other resources say that I should simply use the Heroku Add-on and then the additional, minimal coding. I also want to mention that I have Devise setup so I do not need emails sent for the typical situations.

As you can see, I am quite lost at this point and am just trying to understand which method is best. If you happen to know yourself or have a good resource to use, please let me know =)

Side note: I was also told along the way "Don't trigger the email sending from an ActiveRecord callback - Do it from the controller." I wrote that down to refer to when the time came for it and here I am =P Would you agree with that statement?

I can add code if it would help but I am confused as to which code would be actually helpful...

Thank you so much!

Joe

Edit

One more question: Ideally I would like to be able to send out a custom email to all the users on the application (IE: Weekly updates, new site features, etc.). Would Mailgun even be the correct method of doing this?

like image 718
Joe Dayvie Avatar asked Jul 26 '15 05:07

Joe Dayvie


People also ask

How do you implement a mailgun?

In Mailgun, navigate to Sending > Domains click Add New Domain. Add your domain and follow the setup. You will be given two TXT (SPF and DKIM), two MX, and a CNAME record. Update these records in your DNS provider.

Is mailgun an API?

Send transactional or bulk email effortlessly with Mailgun Email API. Regardless of your business case, Mailgun Email API empowers senders like you to build with the most powerful and reliable email service provider in the industry.

What is API mailgun net?

The Mailgun API is built on HTTP. Our API is RESTful and it: Uses predictable, resource-oriented URLs. Uses built-in HTTP capabilities for passing parameters and authentication. Responds with standard HTTP response codes to indicate errors.


1 Answers

Mailgun is no different from Sendgrid or Mandrill, so when looking for some guidance, you can also consider these solutions. Hands down the best guide for your use case is Sending Emails in Rails Applications. You can also choose from the existing gems to Mailgun API Official gem or ActiveRecord integration.

The way I have already implemented is to setup the areas, add the gemfile and then setup the background processor through Active Job. I have not yet setup the background processor due to being unsure if this is the best way.

Yes, this is the correct way how to set it up. Prior to Rails 4.2, and I believe most of those who have coded without the existence of ActiveJob still prefer things like dj, sidekiq or resque and they are really popular and can be found in almost every Rails app. You can use them with the interface of ActiveJob. Nevertheless, to concept is the same.

To give you an idea about the missing part of code, you can do something like this (from the 1st link)

class ExampleMailer < ActionMailer::Base

  def sample_email(user)
    @user = user
    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {:from    => ENV['gmail_username'],
                      :to      => @user.email,
                      :subject => 'Sample Mail using Mailgun API',
                      :text    => 'This mail is sent using Mailgun API via mailgun-ruby'}
    mg_client.send_message ENV['domain'], message_params
  end
end

Side note: I was also told along the way "Don't trigger the email sending from an ActiveRecord callback - Do it from the controller." I wrote that down to refer to when the time came for it and here I am =P Would you agree with that statement?

I would definitely agree with the statement. One thing is that chaining everything to the ActiveRecord callbacks might seem really practical at first sight, but then, as the application grows, you will see yourself putting in conditional statements or using things like state machines to be covered up. The case behind is that you might create records in your application from multiple sources (user registration, admin import etc.) and not in every case you would like the email to be sent. Furthermore for me and to new team members it is more practical to see it directly in the controller code, rather then going through the models' callbacks. In the end this can be considered also as a personal preference.

If you are using Heroku - then take a look at this Delayed Job guide. On Heroku, you will have to run these background processes in the separate workers (which are not free), for some cost effective solutions take a loot at Running delayed jobs on Heroku for free.

like image 174
adamliesko Avatar answered Sep 19 '22 15:09

adamliesko