Last active
August 29, 2015 14:21
-
-
Save nizaroni/d4da09750794ad3baf0d to your computer and use it in GitHub Desktop.
Using Rails ActionMailer to send emails.
This file contains 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
# rails generate mailer ContactMailer | |
class ContactMailer < ApplicationMailer | |
def contact_email(params) | |
@name = params[:name] | |
self.mail({ | |
to: "owner@example.com", | |
subject: "SiteContact" | |
}) | |
end | |
end |
This file contains 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
<p>Hey Owner,</p> | |
<p>You've got a new contact submission from: <%= @name %></p> | |
<p>Here's their info:</p> | |
INFO |
This file contains 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
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 |
This file contains 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
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