Sending mail from a controller involves three steps:
- Set mail settings in merb_init.rb (Not shown here…see the Mailer docs).
- Create a MailController subclass with actions and templates.
- Call the MailController from another Controller via the send_mail method.
First, create a file in app/mailers that subclasses Merb::MailController. The actions in this controller will do nothing but render mail.
# app/mailers/article_mailer.rb class ArticleMailer < Merb::MailController def notify @user = params[:user] render_mail end end
You also can access the params hash for values passed with the Controller.send_mail method. See also the documentation for render_mail to see all the ways it can be called.
Create a template in a subdirectory of app/mailers/views that corresponds to the controller and action name. Put plain text and ERB tags here:
# app/mailers/views/article_mailer/notify.text.erb Hey, <%= @user.name %>, We're running a sale on dog bones!
Finally, call the Controller.send_mail method from a standard Merb controller.
class Articles < Application def index @user = User.find_by_name('louie') send_mail(ArticleMailer, :notify, { :from => "me@example.com", :to => "louie@example.com", :subject => "Sale on Dog Bones!" }, { :user => @user }) render end end
Note: If you don’t pass a fourth argument to Controller.send_mail, the controller’s params will be sent to the MailController subclass as params. However, you can explicitly send a hash of objects that will populate the params hash instead. In either case, you must set instance variables in the MailController’s actions if you want to use them in the MailController’s views.
The MailController class is very powerful. You can:
- Send multipart email with a single call to render_mail.
- Attach files.
- Render layouts and other templates.
- Use any template engine supported by Merb.
| Public Attributes | |
|---|---|
| base_ |
|
| mailer | |
| params | |
| session | |
| Public Methods | |
|---|---|
| attach | Attaches a file or multiple files to an email. You call this from a method in your MailController (including a before filter). |
| dispatch_ |
A convenience method that creates a blank copy of the MailController and runs dispatch_and_deliver on it. |
| dispatch_ |
take a method name to dispatch to and mail parameters for the MailFactory object. |
| filters_ |
|
| new | You can initialize a MailController with a series of parameters that can be used by methods in the class. You can also pass in a controller object, which will be available to the MailController methods as base_controller. |
| render_ |
Allows you to render various types of things into the text and HTML parts of an email If you include just text, the email will be sent as plain-text. If you include HTML, the email will be sent as a multi-part email. |
| Protected Methods | |
|---|---|
| route | |
| Private Methods | |
|---|---|
| get_ |
This method is here to overwrite the one in the general_controller mixin The method ensures that when a url is generated with a hash, it contains a controller |
<code/>and<pre/>for code samples.