Source for file action_mailer.php
Documentation is available at action_mailer.php
* File for ActionMailer class
* @version $Id: action_mailer.php 217 2006-06-07 02:37:36Z john $
* @copyright (c) 2005 John Peterson
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
include_once( "Mail.php" );
include_once( "Mail/mime.php" );
$mail = null, # Mail object to deliver mail
$errors = array(); # array to hold any errors
'host' => 'localhost', # The server to connect.
'port' => 25, # The port to connect.
'persist' => false, # Indicates whether or not the SMTP connection should persist over multiple sends.
'auth' => false, # Whether or not to use SMTP authentication.
'username' => null, # The username to use for SMTP authentication.
'password' => null, # The password to use for SMTP authentication.
'path' => '/usr/sbin/sendmail',
$template = null, # view file to use for body of email.
$template_root = null, # template_root determines the base from which template references will be made.
$deliveries = array(), # if delivery_method is "test" it will not deliver but store emails in this array.
$from = null, # from email address
$default_from = null, # if no from specified use this as the from.
$body = array(), # array set in child class of values for view template.
$preparse_body = array(), # holder for orginal body array from child class
$crlf = "\r\n", # linefeed char
* ActionMailer constructor.
* @todo Document this API
* Override call() to do some magic where you can call create_*() and deliver_*().
* @todo Document this API
function __call($method_name, $parameters) {
# If the method exists, just call it
if(preg_match("/^create_([_a-z]\w*)/", $method_name, $matches)) {
$result = $this->create_mail($matches[1], $parameters);
} elseif(preg_match("/^deliver_([_a-z]\w*)/", $method_name, $matches)) {
$result = $this->create_mail($matches[1], $parameters);
* Set all the necessary email headers
* @todo Document this API
* Format an array of emails into a correct string / validate emails.
* @todo Document this API
$emails = array($emails);
foreach($emails as $email) {
$email_addresses[] = $email;
$this->errors[] = "Invalid ". $type. "email address: ". $email;
$email_addresses = implode(",", $email_addresses);
* Set the text body of the email.
* @todo Document this API
* Set the html body of the email.
* @todo Document this API
* Sets up default class variables for this mailer. Classes extending
* ActionMailer can override these values.
* @todo Document this API
$this->template_path = "{ $this->template_root}/ ".Inflector::underscore(get_class($this));
'text_charset' => $this->
* Sets up and creates the email for deliver().
* @todo Document this API
private function create_mail($method_name, $parameters = array()) {
if(method_exists($this, $method_name)) {
//echo "calling $method_name<br>";
call_user_func_array(array($this, $method_name), $parameters);
$this->headers['Subject'] = ereg_replace("[\n\r]", "", $this->headers['Subject']);
$this->mail = & Mail::factory("mail");
* Load the template view file for the body of the email.
* @todo Document this API
function render_message($method_name, $body = array()) {
if(strstr($method_name, "/")) {
$template = "{ $this->template_root}/{ $method_name}. ". Trax::$views_extension;
$template = "{ $this->template_path}/{ $method_name}. ". Trax::$views_extension;
if(file_exists($template)) {
$result = ob_get_contents();
* Uses ActionControllers render_partial method.
* @todo Document this API
function render_partial($path, $options = array()) {
if(is_array($options['locals']) && is_array($locals)) {
$options['locals'] = array_merge($locals, $options['locals']);
} elseif(is_array($locals)) {
$options['locals'] = $locals;
$ar = new ActionController();
$ar->views_path = $this->template_path;
$ar->render_partial($path, $options);
* Return a text version of the email currently loaded.
* @todo Document this API
function encoded($add_pre_tags = false) {
list(, $text_headers) = $this->mail->prepareHeaders($this->headers);
$email = "<pre>".$email."</pre>";
* Sends the email loaded into this object via create_mail().
* @todo Document this API
function deliver($mail = null) {
$result = $mail->mail->send(null, $mail->headers, $mail->body);
$this->errors[] = $result->getMessage();
* Add an attachment to an email.
* @todo Document this API
function add_attachment($file, $content_type ='application/octet-stream', $file_name = '', $is_file = true, $encoding = 'base64') {
$this->mail_mime->addAttachment($file, $content_type, $file_name, $is_file, $encoding);
* Validates a single email address
* Validates the input $email is in format:
* user@domain.com or "John Smith <user@domain.com>"
* <li>true => Valid email, no errors found.
* <li>false => Email not valid</li>
function validate_email($email) {
if(eregi("^[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z0-9_-]){2,4}$", $email) ||
eregi("^([ '_a-zA-Z0-9]\w*)+<[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z0-9_-]){2,4}>$", $email)) {
* Set a single line for the header of an email
* @param string $header_key
* key for the header line (To:, From:, Subject:, etc)
* @param string $header_value
* value for the $header_key
function set_header_line($header_key, $header_value) {
if($header_key && $header_value) {
$this->headers[$header_key] = $header_value;
* Add or overwrite description of an error to the list of errors
* @param string $error Error message text
* @param string $key Key to associate with the error (in the
* simple case, column name). If omitted, numeric keys will be
* assigned starting with 0. If specified and the key already
* exists in $errors, the old error message will be overwritten
* with the value of $error.
function add_error($error, $key = null) {
* Return description of non-fatal errors
* @param boolean $return_string
* <li>true => Concatenate all error descriptions into a string
* using $seperator between elements and return the
* <li>false => Return the error descriptions as an array</li>
* @param string $seperator String to concatenate between error
* descriptions if $return_string == true
* @return mixed Error description(s), if any
function get_errors($return_string = false, $seperator = "<br>") {
if($return_string && count($this->errors) > 0) {
return implode($seperator, $this->errors);
* Return errors as a string.
* Concatenate all error descriptions into a stringusing
* $seperator between elements and return the string.
* @param string $seperator String to concatenate between error
* @return string Concatenated error description(s), if any
function get_errors_as_string($seperator = "<br>") {
|