Merb Wiki
Home
All Pages
New Page
Editing Mailers
Textile Enabled
| [[Page Name]] for internal links
h2. Using Merb mailers. In init.rb, include a dependency on 'merb-mailer'. The "Merb::Mailer":http://merbivore.com/documentation/merb-more/0.9.3/merb-mailer/index.html?a=C00000010&name=Mailer docs cover some basic configuration options. As a gotcha, the config option only seems to work right if you stick it in the after_app_loads block, otherwise Merb::Mailer raises a NameError. "Merb::MailController":http://merbivore.com/documentation/merb-more/0.9.3/merb-mailer/index.html?a=C00000008&name=MailController docs have an example of where to put the mailer controller and the views, as well as an example of how to use the send_mail helper. h2. How Merb mailer is organized. Merb mailer has idea of separation of mailer logic from mailer templates to the point mailers in Merb have own tiny MVC architecture. Example of Merb controller: <pre> <code class="ruby"> class UserMailer < Merb::MailController def hello render_mail end end </code> </pre> templates are kept under app/views/mail controller name/template name just like with regular controllers. Content types in template name may be either text or html, like in app/mailers/views/user_mailer/hello.text.erb for the mailer controller above: <pre> Hello <%= params[:name] %> </pre> h2. Send methods. Merb mailer can use SMTP, Sendmail and imitate sendout in test environment. By default Sendmail is used so to use SMTP you have to use config in your application init file. From Merb-mailer documentation: <pre> <code class="ruby"> Merb::Mailer.config = { :host => 'smtp.yourserver.com', :port => '25', :user => 'user', :pass => 'pass', :auth => :plain # :plain, :login, :cram_md5, the default is no auth :domain => "localhost.localdomain" # the HELO domain provided by the client to the server } </code> </pre> Configuration example for Gmail SMTP: <pre><code class="ruby"> Merb::Mailer.config = { :host => 'smtp.gmail.com', :port => '587', :user => 'user@gmail.com', :pass => 'pass', :auth => :plain } </code></pre> * require "smtp_tls":http://www.rubyinside.com/how-to-use-gmails-smtp-server-with-rails-394.html * Use :text option instead of :body <pre><code class="ruby"> m = Merb::Mailer.new :to => 'foo@bar.com', :from => 'bar@foo.com', :subject => 'Welcome to whatever!', :text => partial(:sometemplate) m.deliver! </code></pre> Configuration for Sendmail send method takes path to Sendmail: <pre> <code class="ruby"> Merb::Mailer.config = {:sendmail_path => '/opt/local/bin/sendmail'} </code> </pre> h2. Plain text mail, HTML mail and MailFactory wrapping. Merb::Mailer is a "smart wrapper" around "MailFactory":http://github.com/tmm1/mailfactory/tree. This means you should think 2 or 3 times before operating on @mail.body attribute of mailer instances in your code. To set plain text body use text attribute like this: <pre> <code class="ruby"> Merb::Mailer.new(:to => ..., :from => ..., :subject => ..., :text => "Users with no HTML rendering mail clients will see this").deliver! </code> </pre> and html attribute to set html content: <pre> <code class="ruby"> Merb::Mailer.new(:to => ..., :from => ..., :subject => ..., :html => "Users having HTML rendering mail clients will see this").deliver! </code> </pre> body, to, from and subject fields are proxied to MailFactory when you receive them. Keep this in mind if you wonder why those fields return arrays instead of strings. h2. Testing mailers. First off, make sure you use test sending method so emails won't be sent on each tests run: <pre> <code class="ruby"> Merb::Mailer.delivery_method = :test_send </code> </pre> Useful helper for mailers testing in your controller specs: <pre> <code class="ruby"> send_mail UserMailer, :hello, { :from => "greeter@example.com", :to => @person.email, :subject => "Greetings" }, { :name => @person.name } </code> </pre> Another useful helper to test mailers themselves: <pre> <code class="ruby"> def describe_mail(mailer, template, &block) describe "/#{mailer.to_s.downcase}/#{template}" do before :each do @mailer_class, @template = mailer, template @assigns = {} end def deliver(send_params={}, mail_params={}) mail_params = {:from => "from@example.com", :to => "to@example.com", :subject => "Subject Line"}.merge(mail_params) @mailer_class.new(send_params).dispatch_and_deliver @template.to_sym, mail_params @mail = Merb::Mailer.deliveries.last end instance_eval &block end end </code> </pre> Mailer controller specs may look like this then: <pre> <code class="ruby"> require File.join(File.dirname(__FILE__),'..','spec_helper') describe_mail UserMailer, :hello do it "should say hello" do deliver :name => "Jamie" @mail.text.should == "Hello Jamie" end end </code> </pre> h2. Massive mail sendouts, mail queueing. If you need to send out more than one email, it may become a bottleneck of your action. Classic example is forum notifications on new replies: sending out 100 email in action may take fair number of seconds. In Merb with Mongrel you can use render_then_call method to "release" Mongrel and then do actual send out: <pre> <code class="ruby"> def create # ... rendered_template = render :template => :posted render_then_call(rendered_template) do # do emails sending end end </code> </pre> But because this example above does not work with Thin and Ebb (at least, 0.8.1 and 0.2 versions, respectively) you may consider using "mail queue plugin":http://github.com/michaelklishin/merb_mail_queue/tree that stores email jobs in the database and provides simple processor to do cron scheduled sendouts.
Submit
Recent Activity
Home
was updated
Home
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
Merb Developers available for work
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
Home
was updated