Recently, I co-founded another and my second Startup: InternCity. The Platform is based on Ruby (v-1.9.3) On Rails (v-3.2). Developing appplication on RoR platform is most favourite platform right now specially for its rich features and community supports. Whatever, one of its important features was sending email to user on different activities. When it was Rails, then everything is very much handy. Rails has built in SMTP email library support: Action Mailer . Just, you will have to configure it properly. But , here is catch, it actually works in other SMTP but not in Gmail hosted SMTP. Because, Gmail / Google Mail requires SSL SMTP mailing service, meaning you cannot send email without creating a SSL connection to its SMTP server. So, sending email using Gmail SMTP, you need to create SSL connection first. There is always someone in Rails community who will save your ass to write a new library, plugin or GEM. I got nice GEM named: tlsmail and solved my problem.
So, add the tlsmail in project Gemfile (you can keep that in production group only because Rails doesn’t not process email in TEST ENVIORNMENT until you configure it differently )
gem 'tlsmail' |
Add following configuration in Production environment located at config/environment/production.rb
require 'tlsmail' Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE) 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 => "yourdomin.com", :enable_starttls_auto => true, :authentication => :login, :user_name => "[email protected]", :password => "your password" } config.action_mailer.raise_delivery_errors = true |
Now, you can create the mailer class or can generate it …
rails generate mailer UserMailer |
You will see a blank UserMailer class created at app/mailers/user_mail.rb as following
class UserMailer < ActionMailer::Base default :from => "[email protected]" end |
Add a welcome_email
method into the class
def welcome_email(user) @user = user mail(:to => user.email, :subject => "Welcome to My Awesome Site") end |
Finally, Call the welcome_email method from your any of the controllers by passing User Obeject
UserMailer.intern_email(@intern).deliver |
Checking it your production environment. It will send the email to desire user. Done 🙂
Ussalaam-Ou-Alaikum And Wishes, Hoping everything would be great.
As I am worried about this topic nowadays but I can’t see the routes and views over here kindly send me the email with all or any git repo of this topic.
Waiting for your reply
Thanks
Regards
M.Sumair Kaleem
https://plus.google.com/+MuhammadSumairKalim
https://www.linkedin.com/in/muhammad-sumair-kaleem-76803433/
https://www.facebook.com/msumairkaleem