Skip to content

Instantly share code, notes, and snippets.

@adamki
Created December 8, 2015 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamki/cea50f99eb7ec8586056 to your computer and use it in GitHub Desktop.
Save adamki/cea50f99eb7ec8586056 to your computer and use it in GitHub Desktop.
Implementing Mandrill Email
Pre-Work:
Got to Mandrill and set up and account. Have your `username` and `keys` ready.
In Rails, run ` rails g mailer NotificationsMailer` (or something similar) will set up a new file in the `app/mailer/` folder. In this case it generates a `notifications_mailer.rb`. It also creates a new dir: `app/views/notifications`
1. Configure your Mail APP . In `config/application.rb` add the following configuration :
```
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
domain: 'example.com',
user_name: ENV[mandrill_username],
password: ENV[mandrill_password],
authentication: 'plain',
enable_starttls_auto: true
}
```
2. Set up the controller actions!
In the NotificationsController :
```
class NotificationsController < ApplicationController
def show
end
def create
NotificationsMailer.contact(email_params).deliver_now
redirect_to :back, notice: "Your Email was Sent"
end
private
def email_params
params.permit(:name, :email, :message)
end
end
```
3. Add the contact method in your class!
In notificaitons_mailer.rb
```
class NotificationsMailer < ApplicationMailer
def contact(email_params)
@message = email_params[:message]
mail(
to: email_params[:email],
subject: "Message for #{email_params[:name]}"
)
end
end
```
Also, add this to your `ApplicationMailer` class:
```
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout 'mailer'
end
```
You'll now need to create two templates. You can simply render `@message` in your email views.
1. `app/views/notifications_mailer.contact.html.erb`
2. `app/views/notifications_mailer.contact.text.erb`
At this point, your email should work(send)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment