[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkActionMailer.php (summary)

(no description)

Author: Bermi Ferrer
Copyright: Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
License: GNU Lesser General Public License
File Size: 759 lines (28 kb)
Included or required: 3 times
Referenced: 0 times
Includes or requires: 8 files
 AkActionMailer/AkMailComposer.php
 AkActionMailer/AkMailParser.php
 AkBaseModel.php
 AkActionView/AkPhpTemplateHandler.php
 AkActionView/AkHelperLoader.php
 AkActionMailer/AkActionMailerQuoting.php
 AkActionMailer/AkMailMessage.php
 AkActionView.php

Defines 1 class

AkActionMailer:: (40 methods):
  __construct()
  loadSettings()
  setTemplate()
  setMailerName()
  setBody()
  setCc()
  setBcc()
  setCharset()
  setContentType()
  setFrom()
  setHeaders()
  setImplicitPartsOrder()
  setMimeVersion()
  setRecipients()
  setSentOn()
  setSubject()
  addAttachment()
  set()
  getEncoded()
  getMail()
  receive()
  deliverDirectly()
  getRawMessage()
  create()
  deliver()
  performSmtpDelivery()
  performPhpDelivery()
  performTestDelivery()
  renderMessage()
  render()
  getTemplatePath()
  initializeDefaults()
  _initializeTemplateClass()
  getComposer()
  setComposer()
  getMailerName()
  getControllerName()
  urlFor()
  getHelpers()
  _deliverUsingMailDeliveryMethod()


Class: AkActionMailer  - X-Ref

AkActionMailer allows you to send email from your application using a mailer model and views.

= Mailer Models

To use AkActionMailer, you need to create a mailer model.

$ ./script/generate mailer Notifier

The generated model inherits from AkActionMailer. Emails are defined by
creating methods within the model which are then used to set variables to be
used in the mail template, to change options on the mail, or to add attachments.

Examples:

class Notifier extends AkActionMailer
{
function signupNotification($Recipient)
{
$this->setRecipients($Recipient->getEmailAddressWithName());
$this->setFrom("system@example.com");
$this->setSubject("New account information");
$this->setBody(array('account' => $Recipient ));
}
}

Mailer methods have the following configuration methods available.

* <tt>setRecipients</tt> - Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the <tt>To:</tt> header.
* <tt>setSubject</tt> - The subject of your email. Sets the <tt>Subject:</tt> header.
* <tt>setFrom</tt> - Who the email you are sending is from. Sets the <tt>From:</tt> header.
* <tt>setCc</tt> - Takes one or more email addresses. These addresses will receive a carbon copy of your email. Sets the <tt>Cc:</tt> header.
* <tt>setBcc</tt> - Takes one or more email address. These addresses will receive a blind carbon copy of your email. Sets the <tt>Bcc</tt> header.
* <tt>setSentOn</tt> - The date on which the message was sent. If not set, the header wil be set by the delivery agent.
* <tt>setContentType</tt> - Specify the content type of the message. Defaults to <tt>text/plain</tt>.
* <tt>setHeaders</tt> - Specify additional headers to be set for the message, e.g. <tt>$this->setHeaders(array('X-Mail-Count' => 107370));</tt>.

The <tt>setBody</tt> method has special behavior. It takes an array which generates an instance variable
named after each key in the array containing the value that that key points to.

So, for example, <tt>setBody(array("account" => $Recipient));</tt> would result
in an instance variable <tt>$account</tt> with the value of <tt>$Recipient</tt> being accessible in the
view.


= Mailer views

Like AkAkActionController, each mailer class has a corresponding view directory
in which each method of the class looks for a template with its name.
To define a template to be used with a mailing, create an <tt>.tpl</tt> file with the same name as the method
in your mailer model. For example, in the mailer defined above, the template at
<tt>app/views/notifier/signup_notification.tpl</tt> would be used to generate the email.

Variables defined in the model are accessible as instance variables in the view.

Emails by default are sent in plain text, so a sample view for our model example might look like this:

Hi {account.name},
Thanks for joining our service! Please check back often.

You can even use Action View helpers in these views. For example:

You got a new note!
<?=$text_helper->truncate($note->body, 25);?>


= Generating URLs for mailer views

If your view includes URLs from the application, you need to use Ak::urlFor in
the mailing method instead of the view.
Unlike controllers from Action View, the mailer instance doesn't have any
context about the incoming request. That's why you need to jump this little
hoop and supply all the details needed for the URL.

Example:

function signupNotification($Recipient)
{
// This is the same as calling each individual setter
$this->setAttributes(array(
'recipients' => $Recipient->getEmailAddressWithName(),
'from'       => "system@example.com",
'subject'    => "New account information",
'body'       => array(
'account'   => $Recipient,
'home_page' => Ak::urlFor(array('host' => "example.com", 'controller' => "welcome", 'action' => "greeting"))
)
));
}

You can now access @home_page in the template and get http://example.com/welcome/greeting.

= Sending mail

Once a mailer action and template are defined, you can deliver your message or
create it and save it for delivery later:

Notifier::deliver('signup_notification', $David); // sends the email
$Message = Notifier::create('signup_notification', $David); // => A PEAR::Mail object
Notifier::deliver($Message);

You never instantiate your mailer class. Rather, your delivery instance
methods are automatically wrapped in class methods that are called statically
The <tt>signup_notification</tt> method defined above is delivered by invoking
<tt>$Notifier =& new Notifier(); $Notifier->signupNotification(); $Notifier->deliver();</tt>.


= HTML email

To send mail as HTML, make sure your view (the <tt>.tpl</tt> file) generates HTML and
set the content type to html.

class ApplicationMailer extends AkActionMailer
{
function signupNotification($Recipient)
{
$this->setAttributes(array(
'recipients' => $Recipient->getEmailAddressWithName(),
'from'       => "system@example.com",
'subject'    => "New account information",
'body'       => array('account'   => $Recipient),
'content_type' => text/html" //    Here's where the magic happens
));
}
}


= Multipart email

You can explicitly specify multipart messages:

class ApplicationMailer extends AkActionMailer
{
function signupNotification($Recipient)
{
$this->setAttributes(array(
'recipients' => $Recipient->getEmailAddressWithName(),
'from'       => "system@example.com",
'subject'    => "New account information"
));

$this->addPart(array(
'content_type' => "text/html",
'body' => $this->renderMessage('signup-as-html', 'account' => $recipient)));

$this->addPart("text/plain", array(
'transfer_encoding' = "base64",
'body' => $this->renderMessage('signup-as-plain', 'account' => $recipient)));
}
}

Multipart messages can also be used implicitly because AkActionMailer will automatically
detect and use multipart templates, where each template is named after the name of the action, followed
by the content type. Each such detected template will be added as separate part to the message.

For example, if the following templates existed:
* signup_notification.text.plain.tpl
* signup_notification.text.html.tpl

Each would be rendered and added as a separate part to the message,
with the corresponding content type. The same body array is passed to
each template.


= Attachments

Attachments can be added by using the +addAttachment+ method.

Example:

class ApplicationMailer extends AkActionMailer
{
// attachments
function signupNotification($Recipient)
{
$this->setAttributes(array(
'recipients' => $Recipient->getEmailAddressWithName(),
'from'       => "system@example.com",
'subject'    => "New account information"
));

$this->addAttachment(array(
'content_type' => 'image/jpeg',
'body' => Ak::file_get_contents("an-image.jpg")));

$this->addAttachment('application/pdf', generate_your_pdf_here());
}
}


= Configuration options

These options are specified on the class level, as class attriibutes
<tt>$AkActionMailerInstance->templateRoot = "/my/templates";</tt>

* <tt>templateRoot</tt> - template root determines the base from which template references will be made.

* <tt>server_settings</tt> -  Allows detailed configuration of the server:
* <tt>address</tt> Allows you to use a remote mail server. Just change it
from its default "localhost" setting.
* <tt>port</tt> On the off chance that your mail server doesn't run on port 25, you can change it.
* <tt>domain</tt> If you need to specify a HELO domain, you can do it here.
* <tt>user_name</tt> If your mail server requires authentication, set the username in this setting.
* <tt>password</tt> If your mail server requires authentication, set the password in this setting.
* <tt>authentication</tt> If your mail server requires authentication, you need to specify the authentication type here.
Options are: plain, login, cram_md5

* <tt>delivery_method</tt> - Defines a delivery method. Possible values are 'php' (default), 'smtp', and 'test'.

* <tt>perform_deliveries</tt> - Determines whether AkActionMailer::deliver(*) methods are actually carried out. By default they are,
but this can be turned off to help functional testing.

* <tt>deliveries</tt> - Keeps an array of all the emails sent out through the Action Mailer with delivery_method 'test'. Most useful
for unit and functional testing.

* <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
pick a different charset from inside a method with <tt>$this->charset</tt>.
* <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
can also pick a different content type from inside a method with <tt>$this->content_type</tt>.
* <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to "1.0". You
can also pick a different value from inside a method with <tt>$this->mime_version</tt>.
* <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
array("multipart/alternative", "text/html", "text/enriched", "text/plain"). Items that appear first in the array have higher priority in the mail client
and appear last in the mime encoded message. You can also pick a different order from inside a method with
<tt>$this->implicit_parts_order</tt>.
__construct($Driver = null)   X-Ref
No description

loadSettings()   X-Ref
No description

setTemplate($template_name)   X-Ref
Specify the template name to use for current message. This is the "base"
template name, without the extension or directory, and may be used to
have multiple mailer methods share the same template.


setMailerName($mailerName)   X-Ref
Override the mailer name, which defaults to an inflected version of the
mailer's class name. If you want to use a template in a non-standard
location, you can use this to specify that location.


setBody($body)   X-Ref
Define the body of the message. This is either an array (in which case it
specifies the variables to pass to the template when it is rendered),
or a string, in which case it specifies the actual text of the message.


setCc($cc)   X-Ref
Specify the CC addresses for the message.


setBcc($bcc)   X-Ref
Specify the BCC addresses for the message.


setCharset($charset)   X-Ref
Specify the charset to use for the message. This defaults to the
+default_charset+ specified for AkActionMailer.


setContentType($content_type)   X-Ref
Specify the content type for the message. This defaults to <tt>text/plain</tt>
in most cases, but can be automatically set in some situations.


setFrom($from)   X-Ref
Specify the from address for the message.


setHeaders($headers)   X-Ref
Specify additional headers to be added to the message.


setImplicitPartsOrder($implicit_parts_order)   X-Ref
Specify the order in which parts should be sorted, based on content-type.
This defaults to the value for the +default_implicit_parts_order+.


setMimeVersion($mime_version)   X-Ref
Defaults to "1.0", but may be explicitly given if needed.


setRecipients($recipients)   X-Ref
The recipient addresses for the message, either as a string (for a single
address) or an array (for multiple addresses).


setSentOn($date)   X-Ref
The date on which the message was sent. If not set (the default), the
header will be set by the delivery agent.


setSubject($subject)   X-Ref
Specify the subject of the message.


addAttachment()   X-Ref
Add an attachment to the message.

Example:

class ApplicationMailer extends AkActionMailer
{
// attachments
function signupNotification($Recipient)
{
$this->setAttributes(array(
'recipients' => $Recipient->getEmailAddressWithName(),
'from'       => "system@example.com",
'subject'    => "New account information"
));

$this->addAttachment(array(
'content_type' => 'image/jpeg',
'body' => Ak::file_get_contents("an-image.jpg")));

$this->addAttachment('application/pdf', generate_your_pdf_here());
}
}



set($attributes = array()   X-Ref
Generic setter

Calling $this->set(array('body'=>'Hello World', 'subject' => 'First subject'));
is the same as calling $this->setBody('Hello World'); and $this->setSubject('First Subject');

This simplifies creating mail objects from datasources.

If the method does not exists the parameter will be added to the body.

getEncoded()   X-Ref
Gets a well formed mail in plain text


getMail()   X-Ref
The mail object instance referenced by this mailer.


receive($raw_mail)   X-Ref
Receives a raw email, parses it into an email object, decodes it,
instantiates a new mailer, and passes the email object to the mailer
object's #receive method. If you want your mailer to be able to
process incoming messages, you'll need to implement a #receive
method that accepts the email object as a parameter and then call
the AkActionMailer::recieve method using "parent::recieve($Message);"

class MyMailer extends AkActionMailer{
function receive($Message){
parent::receive($Message);
...
}
}

deliverDirectly(&$Message)   X-Ref
Deliver the given mail object directly. This can be used to deliver
a preconstructed mail object, like:

$email =& $MyMailer->createSomeMail($parameters);
$email->setHeader("frobnicate");
MyMailer::deliver($email);

getRawMessage()   X-Ref
No description

create($method_name, $parameters, $content_type = '')   X-Ref
Initialize the mailer via the given +method_name+. The body will be
rendered and a new AkMailMessage object created.


deliver($method_name, $parameters = null, $Message = null)   X-Ref
Delivers an AkMailMessage object. By default, it delivers the cached mail
object (from the AkActionMailer::create method). If no cached mail object exists, and
no alternate has been given as the parameter, this will fail.


performSmtpDelivery(&$Message, $settings = array()   X-Ref
No description

performPhpDelivery(&$Message, $settings = array()   X-Ref
No description

performTestDelivery(&$Message)   X-Ref
No description

renderMessage($method_name, $body, $options = array()   X-Ref
No description

render($options = array()   X-Ref
No description

getTemplatePath()   X-Ref
No description

initializeDefaults($method_name)   X-Ref
Set up the default values for the various instance variables of this
mailer. Subclasses may override this method to provide different
defaults.


_initializeTemplateClass($assigns)   X-Ref
No description

getComposer()   X-Ref
No description

setComposer($Composer = null)   X-Ref
No description

getMailerName()   X-Ref
Alias for getModelName


getControllerName()   X-Ref
Workarround for limited support of helpers on ActionMailer Views


urlFor()   X-Ref
This is the url_for version for helpers and emails.

As we do not have the context of a host being requested, we need to know
the base_url like http://example.com in oder to add it to the generated URL

getHelpers()   X-Ref
Creates an instance of each available helper and links it into into current mailer.

Mailer helpers work as Controller helpers but without the Request context

_deliverUsingMailDeliveryMethod($method, &$Message, $options)   X-Ref
No description



Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6