
Objective
To use GMail or Google's server to enable email sending on your site even on development side. This method should also be applicable even on your development environment aka localhost.Specification
- Rails 3.2.X (currently I'm on 3.2.6 as of this writing)
- Ruby 1.9.3
- Gem 1.8.24
Steps
- First step is to open
config/application.rb
and add this inside your Application class. With this, we assume that you already have a valid Google email address.
ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.perform_deliveries = true ActionMailer::Base.raise_delivery_errors = true ActionMailer::Base.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :domain => "gmail.com", :user_name => "[INSERT GMAIL USERNAME HERE]", :password => "[INSERT PASSWORD HERE]", :authentication => "plain", :enable_starttls_auto => true } config.action_mailer.default_url_options = { :host => "localhost:3000" }
You may use google app to customize your google account with your own domain.
- Now create a mailer:
rails g mailer Notifier
- Now open
app/mailers/notifier.rb
def sendmail(email_from, email_to, sender_name, subject, message) @message = message mail(:subject => "#{subject}", :from => "no-reply@isecure.com", :cc => email_to) do |format| format.html end end
You may use a different parameter to pass.
- Inside the folder
app/views/notifier
. Createsendmail.html.erb
and copy the following:
A message coming from <%= @sender_name + " (#{@sender})" %> <br /> <br /> <b>SUBJECT:</b> <%= @subject %><br/> <br /> <b>MESSAGE:</b> <fieldset> <%= @message %> </fieldset>
You can also customize it depending on the message you will sent over the email. Remember to use <br/> for new line since we are rendering an HTML here. (The code above doesn't seem to display this properly here)
- That should do it. Now you can call this function to any part of your controllers or models to use it. Just follow this syntax:
# note the deliver function added at the end # syntax is [MAILER CLASS].[function and parameter].deliver Notifier.sendmail("[FROM EMAIL]", "[TO EMAIL]", "[FROM NAME]", "[SUBJECT]", @message).deliver
And that should do the trick for this version of rails.
No comments :
Post a Comment