Posts

Showing posts with the label Sending emails in rails

How Action Mailer Works in Rails

Sending emails in a rails application is very easy and quite handy using action mailer. First we will go through the basic setup of action mailer then we will go in details how it works. Step 1   We can setup a mailer class using rails generator   rails generate mailer UserMailer Step 2   Then we can create any instance method inside it.     def welcome_email(user)       @user = user       @url  = 'http://example.com/login'       mail(to: @user.email, subject: 'Welcome')     end Step 3   We can call this method form model or controller or any kind of callbacks using    Like     UserMailer.welcome_email(@user).deliver Step 4   You can set up a view template for the welcome_email in side the views/mailer directory   Like   welcome_email.html.erb   and you can specify the layout format here and can also access the instance variables defined in the       welcome_email(user) method Step 5   Additionally we can set up the smtp configuration inside