Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Email on Rails

Email on Rails

An overview of ActiveMailer and Sending Email on Rails IRL

Ivan Storck

November 22, 2013
Tweet

More Decks by Ivan Storck

Other Decks in Programming

Transcript

  1. Generating a Mailer in Rails $  rails  generate  mailer  UserMailer

      create    app/mailers/user_mailer.rb   invoke    erb   create        app/views/user_mailer/  
  2. Sending email - ActionMailer::Base class class UserMailer < ActionMailer::Base default

    from: '[email protected]' def welcome_email(user) @user = user @url = 'http://example.com/login' mail(to: @user.email, subject: 'Welcome to My Awesome Site') end end A cross between a Model and a Controller
  3. Sending Email in Plain Text Welcome to example.com, <%= @user.name

    %> =============================================== You have successfully signed up to example.com, your username is: <%= @user.login %>. To login to the site, just follow this link: <%= @url %>. Thanks for joining and have a great day! app/views/user_mailer/welcome_email.text.erb A view that is rendered over SMTP
  4. Sending Email in HTML <!DOCTYPE html> <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> app/views/user_mailer/welcome_email.html.erb
  5. Receiving email in Rails “Receiving and parsing emails with Action

    Mailer can be a rather complex endeavor. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that.” - guides.rubyonrails.org/action_mailer_basics.html
  6. Receiving email in Rails “Receiving and parsing emails with Action

    Mailer can be a rather complex endeavor. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that.” - guides.rubyonrails.org/action_mailer_basics.html
  7. • Endpoints for web APIs to POST to • Enable:

    • Service Integration • Compostability, Encapsulation, Polymorphism, all good things! • http://localhost:3000/incoming_email • a Controller with an action to handle POST
  8. end