Skip to content

Instantly share code, notes, and snippets.

@nizaroni
Last active August 29, 2015 14:21
Show Gist options
  • Save nizaroni/d4da09750794ad3baf0d to your computer and use it in GitHub Desktop.
Save nizaroni/d4da09750794ad3baf0d to your computer and use it in GitHub Desktop.
Using Rails ActionMailer to send emails.
# rails generate mailer ContactMailer
class ContactMailer < ApplicationMailer
def contact_email(params)
@name = params[:name]
self.mail({
to: "owner@example.com",
subject: "SiteContact"
})
end
end
<p>Hey Owner,</p>
<p>You've got a new contact submission from: <%= @name %></p>
<p>Here's their info:</p>
INFO
Rails.application.configure do
# ...
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
# Use SMTP for sending emails
config.action_mailer.delivery_method = :smtp
# Gmail SMTP settings
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
# Add these environment variables to figaro's application.yml
domain: ENV['GMAIL_DOMAIN'],
user_name: ENV['GMAIL_USERNAME'],
password: ENV['GMAIL_PASSWORD'],
authentication: 'plain',
enable_starttls_auto: true }
# ...
end
class ContactController < ApplicationController
# POST /contact
def send_email
ContactMailer.contact_email(params).deliver_now
redirect_to "/contact"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment