Revisions
-
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 42 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 40 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ## 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 ``` -
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -136,10 +136,10 @@ UserMailer.user_email(@user).deliver ### Install gem Add to your Gemfile (:test and :dev) with specific ```Github``` commit to prevent breaking ```ruby group :test, :development do gem 'mail_view', git: 'git://github.com/37signals/mail_view.git', ref: '6a4bc7f01a' end ``` -
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 60 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Randy Burgess revised this gist
Sep 24, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 13 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 =yield ``` create a partial for the email header at ```views/shared/_mailer_header_default.html.haml``` ```haml #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 #footer{ style: "color: red;" } %p This is footer content ``` create a view for emails as ```views/user_mailer/user_email.html.haml``` ```haml = 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``` -
Randy Burgess revised this gist
Sep 22, 2012 . No changes.There are no files selected for viewing
-
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 0 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` ## Create Settings for Mailer Add to file ```config/settings.yml``` -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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{ style: "color: blue;" } = render :partial => "shared/mailer_header_default" #content{ style: "color: green;" } = yield #footer{ style: "color: red;" } = render :partial => "shared/mailer_footer_default" ``` -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 user_email(user) @user = user @url = "http://example.com/login" mail( -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: "Email Test", to: user.email ) end -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 10 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 ``` -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" charset "utf-8" content_type "text/html" from Settings.mailer.from -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 19 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` $ -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) ## Generate a Mailer Model and View -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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_email.html.haml``` ```haml %p -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 12 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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: "FirstName LastName <username@example.com>" address: "smtp.gmail.com" port: 587 domain: "gmail.com" -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 23 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 %body #wrapper #header = render :partial => "shared/mailer_header_default" #content = yield #footer = render :partial => "shared/mailer_footer_default" ``` 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 -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Randy Burgess revised this gist
Sep 22, 2012 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } ``` -
Randy Burgess created this gist
Sep 22, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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') ```