Skip to content

Instantly share code, notes, and snippets.

@psahni
Forked from wrburgess/gist:3767468
Created July 7, 2014 10:46

Revisions

  1. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    * [Railscast: Sending HTML Email](http://railscasts.com/episodes/312-sending-html-email)
    * [Gem: Mail_View](https://github.com/37signals/mail_view) *preview email without sending*
    * [Gist: Setting Up SettingsLogic Gem](https://gist.github.com/3753085)
    * [Blog: How to test mailers in Rails 3 with RSpec](http://blog.lucascaton.com.br/?p=574)

    ## Generate a Mailer Model and View

  2. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -227,4 +227,46 @@ describe User do
    end

    end
    ```

    ### Add a spec for the mailer

    create a file named ```spec/mailers/user_mailer_spec.rb```

    ```ruby
    require "spec_helper"

    describe UserMailer do

    describe 'user_email' do
    let(:user) { FactoryGirl.create(:user, email: "test@example.com", password: "secret") }
    let(:mail) { UserMailer.user_email(user) }

    #ensure that the subject is correct
    it 'renders the subject' do
    mail.subject.should == 'User Email Test'
    end

    #ensure that the receiver is correct
    it 'renders the receiver email' do
    mail.to.should == [user.email]
    end

    #ensure that the sender is correct
    it 'renders the sender email' do
    mail.from.should == ["test@example.com"]
    end

    #ensure that the @name variable appears in the email body
    it 'assigns @name' do
    mail.body.encoded.should match(user.name)
    end

    #ensure that the @confirmation_url variable appears in the email body
    it 'assigns @confirmation_url' do
    mail.body.encoded.should match("http://application_url/#{user.id}/confirmation")
    end

    end
    end
    ```
  3. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 40 additions and 1 deletion.
    41 changes: 40 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -188,4 +188,43 @@ end
    ```
    ### Visit the Preview

    Go to ```http://example.com/preview_email``` to review
    Go to ```http://example.com/preview_email``` to review

    ## Setting up Tests

    ### Ensure the test environment won't send emails

    Check that this line is un-commented in ```config/environments/test.rb```

    ```ruby
    config.action_mailer.delivery_method = :test
    ```

    ### Add a method to a model that will send an email

    Add to ```app/models/user.rb```

    ```ruby
    def send_user_email(user)
    UserMailer.user_email(user).deliver
    end
    ```

    ### Add a spec to the model to test the send method
    ```ruby
    require 'spec_helper'

    describe User do

    describe 'mailers' do

    it "sends a user_email" do
    user = FactoryGirl.create(:user, email: "test@example.com", password: "secret")
    user.send_user_email(user)
    ActionMailer::Base.deliveries.last.to.should == [user.email]
    end

    end

    end
    ```
  4. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -136,10 +136,10 @@ UserMailer.user_email(@user).deliver

    ### Install gem

    Add to your Gemfile with specific commit to prevent breaking
    Add to your Gemfile (:test and :dev) with specific ```Github``` commit to prevent breaking

    ```ruby
    group :development do
    group :test, :development do
    gem 'mail_view', git: 'git://github.com/37signals/mail_view.git', ref: '6a4bc7f01a'
    end
    ```
  5. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    * [Railscast: ActionMailer in Rails 3](http://railscasts.com/episodes/206-action-mailer-in-rails-3)
    * [Railscast: Sending HTML Email](http://railscasts.com/episodes/312-sending-html-email)
    * [Gem: Mail_View](https://github.com/37signals/mail_view) *preview email without sending*
    * [Gist: Setting Up SettingsLogic Gem](https://gist.github.com/3753085)

    ## Generate a Mailer Model and View

  6. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 60 additions and 1 deletion.
    61 changes: 60 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    * [RailsGuides: ActionMailer Basics](http://guides.rubyonrails.org/action_mailer_basics.html)
    * [Railscast: ActionMailer in Rails 3](http://railscasts.com/episodes/206-action-mailer-in-rails-3)
    * [Railscast: Sending HTML Email](http://railscasts.com/episodes/312-sending-html-email)
    * [Gem: Mail_View](https://github.com/37signals/mail_view) *preview email without sending*

    ## Generate a Mailer Model and View

    @@ -128,4 +129,62 @@ Fire off an email using this line of ruby:

    ```ruby
    UserMailer.user_email(@user).deliver
    ```
    ```

    ## Install and Setup MailView Gem for Email Previews

    ### Install gem

    Add to your Gemfile with specific commit to prevent breaking

    ```ruby
    group :development do
    gem 'mail_view', git: 'git://github.com/37signals/mail_view.git', ref: '6a4bc7f01a'
    end
    ```

    In Terminal run ```bundle install```

    ### Add Preview Code

    Add lines to the Mailer models

    ```ruby
    class UserMailer < ActionMailer::Base
    layout 'mailer_default'

    def user_email(user)
    @user = user
    mail(
    charset: "utf-8",
    content_type: "text/html",
    from: Settings.mailer.from,
    subject: "User Email Test",
    to: user.email
    )
    end

    #add this code
    class Preview < MailView
    def user_email
    user = User.first
    mail = UserMailer.user_email(user)
    mail
    end
    end

    end
    ```

    ### Mount Views in Routes

    Add new routes to ```config/routes.rb```

    ```ruby
    if Rails.env.development?
    mount UserMailer::Preview => 'preview_email'
    end
    ```
    ### Visit the Preview

    Go to ```http://example.com/preview_email``` to review
  7. Randy Burgess revised this gist Sep 24, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    * [RailsGuides: ActionMailer Basics](http://guides.rubyonrails.org/action_mailer_basics.html)
    * [Railscast: ActionMailer in Rails 3](http://railscasts.com/episodes/206-action-mailer-in-rails-3)
    * [Railscast: Sending HTML Email](http://railscasts.com/episodes/312-sending-html-email)

    ## Generate a Mailer Model and View

  8. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -41,36 +41,36 @@ create a layout for emails as ```views/layouts/mailer_default.html.haml```
    %head
    %body
    #wrapper
    #header{ style: "color: blue;" }
    = render :partial => "shared/mailer_header_default"
    #content{ style: "color: green;" }
    = yield
    #footer{ style: "color: red;" }
    = render :partial => "shared/mailer_footer_default"
    =yield
    ```

    create a partial for the email header at ```views/shared/_mailer_header_default.html.haml```

    ```haml
    %p
    This is header content
    #header{ style: "color: blue;" }
    %p
    This is header content
    ```

    create a partial for the email footer at ```views/shared/_mailer_footer_default.html.haml```

    ```haml
    %p
    This is footer content
    #footer{ style: "color: red;" }
    %p
    This is footer content
    ```

    create a view for emails as ```views/user_mailer/user_email.html.haml```

    ```haml
    %p
    This is the content of the User Email
    = render :partial => "shared/mailer_header_default"
    #content{ style: "color: green;" }
    %p
    This is the content of the User Email
    = render :partial => "shared/mailer_footer_default"
    ```

    ## Create Settings for Mailer

    Add to file ```config/settings.yml```
  9. Randy Burgess revised this gist Sep 22, 2012. No changes.
  10. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -71,14 +71,6 @@ create a view for emails as ```views/user_mailer/user_email.html.haml```
    This is the content of the User Email
    ```

    ## Designate ActionMailer to recognize HAML files

    Add to ```config/environments.rb```

    ```ruby
    ActionMailer::Base.register_template_extension('haml')
    ```
    ## Create Settings for Mailer

    Add to file ```config/settings.yml```
  11. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -127,4 +127,12 @@ On local:
    ```
    $ export MAILER_USERNAME=gmail.com --remote stage
    $ export MAILER_PASSWORD=password --remote stage
    ```

    ## Send an Email

    Fire off an email using this line of ruby:

    ```ruby
    UserMailer.user_email(@user).deliver
    ```
  12. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -41,11 +41,11 @@ create a layout for emails as ```views/layouts/mailer_default.html.haml```
    %head
    %body
    #wrapper
    #header
    #header{ style: "color: blue;" }
    = render :partial => "shared/mailer_header_default"
    #content
    #content{ style: "color: green;" }
    = yield
    #footer
    #footer{ style: "color: red;" }
    = render :partial => "shared/mailer_footer_default"
    ```
  13. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ or create a ```mailers/user_mailer.rb```
    class UserMailer < ActionMailer::Base
    layout 'mailer_default'

    def weekly_review(user)
    def user_email(user)
    @user = user
    @url = "http://example.com/login"
    mail(
  14. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ class UserMailer < ActionMailer::Base
    charset: "utf-8",
    content_type: "text/html",
    from: Settings.mailer.from,
    subject: "Group Weekly Review",
    subject: "Email Test",
    to: user.email
    )
    end
  15. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -17,14 +17,16 @@ or create a ```mailers/user_mailer.rb```
    class UserMailer < ActionMailer::Base
    layout 'mailer_default'

    def user_email(user)
    body user: user, url: "http://example.com/login"
    charset "utf-8"
    content_type "text/html"
    from Settings.mailer.from
    recipients user.email
    sent_on Time.now
    subject "User Email Example"
    def weekly_review(user)
    @user = user
    @url = "http://example.com/login"
    mail(
    charset: "utf-8",
    content_type: "text/html",
    from: Settings.mailer.from,
    subject: "Group Weekly Review",
    to: user.email
    )
    end
    end
    ```
  16. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ class UserMailer < ActionMailer::Base
    layout 'mailer_default'

    def user_email(user)
    body { user: user, url: "http://example.com/login" }
    body user: user, url: "http://example.com/login"
    charset "utf-8"
    content_type "text/html"
    from Settings.mailer.from
  17. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -125,6 +125,4 @@ On local:
    ```
    $ export MAILER_USERNAME=gmail.com --remote stage
    $ export MAILER_PASSWORD=password --remote stage
    ```

    $
    ```
  18. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -118,7 +118,7 @@ On heroku:
    ```
    $ heroku config:add MAILER_USERNAME=gmail.com --remote stage
    $ heroku config:add MAILER_PASSWORD=password --remote stage
    ---
    ```

    On local:

  19. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 19 additions and 1 deletion.
    20 changes: 19 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -109,4 +109,22 @@ ActionMailer::Base.smtp_settings = {
    authentication: Settings.mailer.authentication,
    enable_starttls_auto: Settings.mailer.enable_starttls_auto
    }
    ```
    ```

    ## Set Up Environment Variables

    On heroku:

    ```
    $ heroku config:add MAILER_USERNAME=gmail.com --remote stage
    $ heroku config:add MAILER_PASSWORD=password --remote stage
    ---
    On local:
    ```
    $ export MAILER_USERNAME=gmail.com --remote stage
    $ export MAILER_PASSWORD=password --remote stage
    ```
    $
  20. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ##Refs

    * [RailsGuides: ActionMailer Basics](http://guides.rubyonrails.org/action_mailer_basics.html)
    * [Railscast: ActionMailer in Rails 3](http://railscasts.com/episodes/206-action-mailer-in-rails-3}
    * [Railscast: ActionMailer in Rails 3](http://railscasts.com/episodes/206-action-mailer-in-rails-3)

    ## Generate a Mailer Model and View

  21. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    ##Refs

    * [RailsGuides: ActionMailer Basics](http://guides.rubyonrails.org/action_mailer_basics.html)
    * [Railscast: ActionMailer in Rails 3](http://railscasts.com/episodes/206-action-mailer-in-rails-3}

    ## Generate a Mailer Model and View

    In terminal:
  22. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ or create a ```mailers/user_mailer.rb```
    class UserMailer < ActionMailer::Base
    layout 'mailer_default'

    def weekly_review(user)
    def user_email(user)
    body { user: user, url: "http://example.com/login" }
    charset "utf-8"
    content_type "text/html"
    @@ -57,7 +57,7 @@ create a partial for the email footer at ```views/shared/_mailer_footer_default.
    This is footer content
    ```

    create a view for emails as ```views/user_mailer/user_mailer.html.haml```
    create a view for emails as ```views/user_mailer/user_email.html.haml```

    ```haml
    %p
  23. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -9,13 +9,17 @@ rails g mailer UserMailer
    or create a ```mailers/user_mailer.rb```

    ```ruby
    class GroupMailer < ActionMailer::Base
    default from: Settings.mailer.from

    def example_email(user)
    @user = user
    @url = "http://example.com/login"
    mail(to: user.email, subject: "Example Subject")
    class UserMailer < ActionMailer::Base
    layout 'mailer_default'

    def weekly_review(user)
    body { user: user, url: "http://example.com/login" }
    charset "utf-8"
    content_type "text/html"
    from Settings.mailer.from
    recipients user.email
    sent_on Time.now
    subject "User Email Example"
    end
    end
    ```
    @@ -75,7 +79,7 @@ Add to file ```config/settings.yml```
    ```yml

    mailer:
    from: "Randy Burgess <wrburgess@gmail.com>"
    from: "FirstName LastName <username@example.com>"
    address: "smtp.gmail.com"
    port: 587
    domain: "gmail.com"
  24. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 23 additions and 2 deletions.
    25 changes: 23 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -20,25 +20,46 @@ class GroupMailer < ActionMailer::Base
    end
    ```

    ## Create Mailer Templates

    create a layout for emails as ```views/layouts/mailer_default.html.haml```

    ```haml
    !!!
    %html{ lang: "en" }
    %head
    %meta{ content: "text/html", charset: "utf-8", http-equiv: "Content-Type" }/
    %body
    #wrapper
    #header
    = render :partial => "shared/mailer_header_default"
    #content
    = yield
    #footer
    = render :partial => "shared/mailer_footer_default"
    ```

    create a view for emails as ```views/user_mailer/user_mailer.html.haml```
    create a partial for the email header at ```views/shared/_mailer_header_default.html.haml```

    ```haml
    %p
    This is header content
    ```

    create a partial for the email footer at ```views/shared/_mailer_footer_default.html.haml```

    ```haml
    %p
    This is footer content
    ```

    create a view for emails as ```views/user_mailer/user_mailer.html.haml```

    ```haml
    %p
    This is the content of the User Email
    ```

    ## Designate ActionMailer to recognize HAML files

  25. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,23 @@ Add to ```config/environments.rb```
    ```ruby
    ActionMailer::Base.register_template_extension('haml')
    ```
    ## Create Settings for Mailer

    Add to file ```config/settings.yml```

    ```yml

    mailer:
    from: "Randy Burgess <wrburgess@gmail.com>"
    address: "smtp.gmail.com"
    port: 587
    domain: "gmail.com"
    user_name: <%= ENV["MAILER_USERNAME"] %>
    password: <%= ENV["MAILER_PASSWORD"] %>
    authentication: "plain"
    enable_starttls_auto: true

    ```

    ## Initialize Mailer Settings

  26. Randy Burgess revised this gist Sep 22, 2012. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -46,4 +46,20 @@ Add to ```config/environments.rb```

    ```ruby
    ActionMailer::Base.register_template_extension('haml')
    ```

    ## Initialize Mailer Settings

    create a file name ```config/intializers/mailer_settings.rb```

    ```ruby
    ActionMailer::Base.smtp_settings = {
    address: Settings.mailer.address,
    port: Settings.mailer.port,
    domain: Settings.mailer.domain,
    user_name: Settings.mailer.user_name,
    password: Settings.mailer.password,
    authentication: Settings.mailer.authentication,
    enable_starttls_auto: Settings.mailer.enable_starttls_auto
    }
    ```
  27. Randy Burgess created this gist Sep 22, 2012.
    49 changes: 49 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    ## Generate a Mailer Model and View

    In terminal:

    ```
    rails g mailer UserMailer
    ```

    or create a ```mailers/user_mailer.rb```

    ```ruby
    class GroupMailer < ActionMailer::Base
    default from: Settings.mailer.from

    def example_email(user)
    @user = user
    @url = "http://example.com/login"
    mail(to: user.email, subject: "Example Subject")
    end
    end
    ```

    create a layout for emails as ```views/layouts/mailer_default.html.haml```

    ```haml
    !!!
    %html{ lang: "en" }
    %head
    %meta{ content: "text/html", charset: "utf-8", http-equiv: "Content-Type" }/
    %body
    #wrapper
    ```

    create a view for emails as ```views/user_mailer/user_mailer.html.haml```

    ```haml
    ```


    ## Designate ActionMailer to recognize HAML files

    Add to ```config/environments.rb```

    ```ruby
    ActionMailer::Base.register_template_extension('haml')
    ```