Wednesday, 14 December 2016

Send Mails Rails

1. Create the Mailer
     rails generate mailer UserMailer

2. create user:- app/mailer/user_mailer.rb

def welcome_email(user)
    @user = user
    @url  = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end

3. create view:- app/view/user_mailer/welcome_email.html.erb
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up to example.com,
      your username is: <%= @user.login %>.<br>
    </p>
    <p>
      To login to the site, just follow this link: <%= @url %>.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

4. add_email smtp setting:- config/environments/development

config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true config.action_mailer.default_options = {from: 'localhost:3000'} config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'localhost', user_name: 'testing.bittern@gmail.com', password: 'zzzzzzzzz', authentication: 'plain', enable_starttls_auto: true }

5. send_email:-

UserMailer.welcome_email(User.first)

No comments:

Post a Comment