Skip to main content

Sending emails with Mandrill Discussion

General • Asked by Chris Oliver

Excellent tutorial. Congrats.


Hey Chris, would you recommend gem 'mandrill-rails' too? or is gem "madrill-api" better?

Hey Angelo! I believe the mandrill-rails gem only supports Mandrill's inbound email webhooks.

The primary goal of Mandrill::Rails is to make supporting Mandrill web hooks as easy and Rails-native as possible.

The mandrill-api gem should be for accessing their REST API which you can find here: https://mandrillapp.com/api...

Right! My bad hehe thanks!


Thank you. Very complete tutorial.
(I would use it in companion of the dotenv gem to avoid adding the the api-key on the configuration files and prevent committing it into the repository.)


Why I can not sign up on Mandrill, when I signed up , it just returned "Error 417"


Not sure if I am watching this wrong. I was trying to follow along adding a functioning mailer to an application I'm already working on. It seems like this tutorial begins in the middle of something else, with more features added than I was told in the video.

I was expecting the tutorial to go from being a basic app with no functioning mailer to being a basic app with a functioning mailer

Check out the handful of episodes just before this one to get the bigger picture.

Thanks for the timely reply. I've been at it this whole time. Trying to piece it together. I'll stick around :)


Just wondering what are benefits to use Mandrill over the simple mail() method that you mentioned apart from the flexibility of templates manipulation out of code?

Basically, you need to run a mail server somewhere in either case. Mandrill serves as that whether you just use it for SMTP or you hit their API instead to send the emails without using SMTP.

It also seems Mandrill does much of the legwork with ISPs and Email services to give you a better chance of your mail actually getting delivered — and not flagged as spam.


Don't you usually put the ActionMailer smtp settings and everything else in the environment files?

these are Devise email templates right?

This isn't Devise, but Devise's emails work the same way. It uses your ActionMailer config so it will send them as well.

Yes, because usually I'll have a separate account between development (often disabled or a test account that's a free plan) and production.


I want to pass in specific information from the form through the request parameter url. How can I do that. For example a user submits a project for bidding in a form, and the project has a project title, how do I pass the project's title to the mail sent?

You can pass in the object into the mailer as an argument. Just set that to an instance variable and render it in your mailer view. If you're using Mandrill templates, you can pass the values over as merge variables instead.

Hi Chris, that was helpful, using the merge tags with my Mandrill templates resolved it. Also, the "mandrill_mailer" gem has a helpful documentation for anyone how cares. Thanks.


To help out future rookies who will find this:
I don't know if the mandrill gem changed but following your steps, I could not get emails to send. Turns out in the Mandrill gem docs they say "The gem assumes your API key is stored as an environment variable called MANDRILL_APIKEY." http://help.mandrill.com/en...

So I figured out foreman, and how to load an .env file etc and then it started working.

I went through this whole process with Foreman, Unicorn, Figaro, and Resque. Is there anything you installed or did you might think I'm missing here?


Also, has anyone else seen a delay in sending emails from Mandrill to Gmail? I would see a delay of up to 15 mins sometimes. Usually it is around a minute. Checked Mandrill status, no problems there.

It depends on their SMTP system, I've seen the same delays at times. They might be queuing or batching emails behind the scenes or any multitude of things between them and GMail could be slow. I'm not quite sure.

I was seeing 5 minute+ delays and no shows - which is a problem for password resets! Chris has done the hard work so switching between providers isn't a chore. I'm testing another SMTP service and this is giving a better response time.


Thanks Chris, awesome as usual ;)


I'm trying to pass the multiple arguments into the example new_book method, in this case a book and a user, and I can't get this to work and I'm a little frustrated. It works fine with one argument, the book, but adding multiple arguments messes it up. The after_create call gets called BEFORE the book is put in the database, so how do I access the book information to pass it? I was thinking with some kind of params call, but it doesn't seem to be accessible. Any ideas are welcome!

I tried refactoring the new_user method as well, and the same thing happened. It goes through in the console, but nothing gets to the Mandrill servers BUT, the reset password does go through, so it doesn't seem to be an SMTP problem but something within the new_book method.

Ok, after all of this, it seems that in my particular app, in the AdminMailer.rb file, the “default from: 'xxxxx'" under ”Class AdminMailer < ActionMailer::Base" does not work when attempting to send a template designed in Mandrill. You need to specifically add a "from_email" call in the new_book(book) function for it work.

Also, in order to get the two arguments passed, I had to make things look like this:

Book.rb

after_create :send_email

def send_email
user = self.user
AdminMailer.new_book(self, user).deliver_now
end

AdminMailer.rb

def new_event(book, user)
Mandrill Info
end

Hope this helps someone out there.

Glad you got it working! As an aside, one great way to test all this is to manually create and test your emails from the Rails console. It can help you poke around and find bugs like this quicker than going through the browser.


I am getting <actionmailer::base::nullmail:0x007f91f5a2e5c0> error. Why is that??


user_name: is that the email you created the mandrill account, heroku, github, what account are you talking about. Do i make up and email address? Feeling very lost about what is going on with mailers.

When you register for Mandrill, you have to sign up with an email address. That's the email you will put into the user_name field so it knows which account is sending the emails.

Thank you, I was playing around with different emails and i got it working.
It's great to have a current resource.


How would you catch tests when using the Mandrill API and templates? At the moment I'm just skipping it using something like:

# MyMailer.rb
def new_book(book)
return if Rails.env.test?
...
end


I have a 10 day e-mail drip campaign set up in MailChimp. What's the best way to have new signups be automatically added to the list for this campagin? From what I'm reading it looks like you have to code the logic for automatic e-mails your self instead of just using mailchimp.. is this correct?

The Gibbon gem should let you do this. You can add someone to the list when they sign up. This is what I do with GoRails on registrations so that I can send the announcement emails out with Mailchimp. That *should* do the trick for you there.

Ok thanks! I'll check it out after lunch. I appreciate the guidance

Always glad to help! Let me know how it goes (and how lunch is). ;)

Hi Chris, I am reading gibbon gem but unsure on what it is exactly for? Would you describe how you are using it for gorails to illustrate?


Great vid, how would I get the CSS to work in the mailer.html.erb templates?

Want t to style the mailer.html.erb so that when someone get the email in their gmail or other it looks presentable - Tried styling in myself and with bootstrap but the styles don't make it with the content to the recipients email account.

Unfortunately you'll have to paste all the bootstrap styles inline into the email templates. Emails only work with limited CSS.

Nevermind - You need Inline style like this in template:<head><style> hr {color:sienna;} p {margin-left:20px;} body {background-image:url("images/back40.gif");} </style> </head>

late reply, but this can be helpful: http://foundation.zurb.com/...


Thank you very much Chris. Great video. Helped me setup up smtp for a class project.


Hello chris. I followed all the steps as you showed in this screen cast but unfortunately I am not receiving the emails. I neither setup the domain in Mandrill inbound option and all I get the response in Outbound is that Email Rejected Reason Unsigned. I don't know why. I registered to mandrill with my normal gmail account. Can you please tell me what I would be doing wrong?

It sounds like you need to verify your domain ownership. Check the note at the bottom here: https://mandrill.zendesk.co...


Thanks for the update @excid3:disqus! Yea.. wanted notify anyone else looking at this tutorial that Mandrill is a paid service after today's announcement. Perhaps a tutorial on how to set up with Amazon SES or just Gmail if the load is not heavy..? :)

That Would de great! A tutorial on either Amazon SES or Sendgrid would be highly useful!!


The others mail services that you provided, do they have ruby gems?


I'm getting uninitialized constant AdminMailer::MANDRIL_API_KEY on rspec test, how can i add the mailers to my rspec tests?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.