-
-
Save jillmd501/f1e3a63295067af006a6 to your computer and use it in GitHub Desktop.
Implementing Mandrill Email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]" | |
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