Tuesday, 3 June 2014

EMAIL SENDING IN RAILS...


SMTP CONNECTION IN RAILS



Step 1: Here smtp connection for newsletter.

$rails g scaffold newsletter name:string email:string



Step 2: Migration


$rake db:migrate


Step 3: To generate mailer related files for newsletter

$rails g mailer newsletter_mailer


Step 4:Run bundle command


$bundle install


Step 5:Create a new file in /config/initializers/setup_mail.rb


ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "saritha.chakilala41@gmail.com",
:password => ".........",
:authentication => "plain",
:enable_starttls_auto => true
}

ActionMailer::Base.default_url_options[:host] = "localhost:3000"


Step 6: Write below lines of code in app/mailers.newsletter_mailer.rb


class NewsletterMailer < ActionMailer::Base

    default from: "saritha.chakilala41@gmail.com"
#To send email to multiple  recipients
    default to: Proc.new { Newsletter.pluck(:email) }
    def daily
        mail(:subject => "TESTING MAIL")
    end 

end


Step 7: Create and Write your mailing related text in /app/views/newsletter_mailer/daily.html.erb

I write some text here...

 <html>
    <body>
        <h1><font color="red">Hello Every One...There is one new product...lets try </font></h1>
    </body>
</html>

Step 8: Run the below command on rails console

NewsletterMailer.daily.deliver



SOURCE FROM:http://asciicasts.com/episodes/206-action-mailer-in-rails-3


@@@@@@@HAPPY RAILS CODING@@@@@@

No comments:

Post a Comment