Published in: PHP
|
|
|
http://rossoft.wordpress.com
Expand |
Embed | Plain Text
<?php /** * Mail Templates * Sends email through a view * * The templates rendered must have extension .mail * * Uses: * @link http://phpmailer.sourceforge.net/ * @see MailWidgetHelper * * @author RosSoft * @version 0.1 * @license MIT * * * Usage example: * First, edit this file and change the define(âCONFIG_SMTP⌠* with your ISP config. * * file views/foo/mail_view.mail <m:from name=âMiguel Rosâ mail=ârosmiguel@teleline.esâ /> <m:to> rossoft@gmail.com </m:to> <m:to> rosmiguel@teleline.es </m:to> <m:subject> Hi! That is the subject </m:subject> <m:body_text> This is the plain body text </m:body_text> <m:body_html> This is the HTML <b>Body</b> </m:body_html> <m:attach path=â/tmp/test1.txtâ name=âtest_file_1.txtâ /> <m:attach path=â/tmp/test2.txtâ /> File views/foo/mail_view.thtml: The mail has been set Action in FooController: function mail_view() { $this->view=âMailâ; $this->render(); //render the mail_view.mail $this->view=âViewâ; $this->render(); //now the mail_view.thtml renders } */ define(âCONFIG_SMTP_USERâ,'yyyyâ); //Directory within VENDORS/ define(âPHPMAILER_SUBDIRâ,'phpmailerâ . DS); class MailView extends View { var $mail=null; //instance of MailAux function render($action = null, $layout = null, $file = null) { $this->mail=& new MailAux; $this->controller->mail=& $this->mail; $file=null; $this->load_helper(âMailWidgetâ); if ($action==NULL) { $action=$this->action; } parent::render($action,$layout,$file); return $this->mail->send(); } /** * Adds a helper to the helpers array for loading it * @param string $helper Name of the helper like âJavascriptâ */ function load_helper($helper) { { $this->helpers[]=$helper; } } /** * Returns the filename associated with the action * with the extension â.$extâ * * @param string $action If null, then current action * @param string $ext Extension of the view template (with dot) Example: â.xmlâ * @return string */ function get_filename($action,$ext) { $old_ext=$this->ext; $this->ext=$ext; $fn=$this->_getViewFileName($action); $this->ext=$old_ext; return $fn; } } /** * This object is the interface between MailView and MailWidgetHelper */ class MailAux extends Object { var $charset=âutf-8â˛; var $from_mail=null; var $from_name='â; var $to=array(); var $subject='â; var $body_text=null; var $body_html=null; /** * SMTP Configuration */ var $host=CONFIG_SMTP_HOST; var $username=CONFIG_SMTP_USER; var $password=CONFIG_SMTP_PASS; /** * It contains mailer error message or false if no * error has occurred */ var $error=false; function send() { vendor(PHPMAILER_SUBDIR. âclass.phpmailerâ); $mail = new PHPMailer(); $mail->PluginDir = VENDORS .PHPMAILER_SUBDIR ; $mail->SetLanguage(âenâ,VENDORS .PHPMAILER_SUBDIR . âlanguageâ . DS); $mail->CharSet= $this->charset; $mail->IsSMTP(); // send via SMTP $mail->Host = $this->host; // SMTP servers if ($this->username !==null) { $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $this->username; // SMTP username $mail->Password = $this->password; // SMTP password } $mail->From = $this->from_mail; $mail->FromName = $this->from_name; foreach ($this->to as $address) { $mail->AddAddress($address); } $mail->Subject = $this->subject; if ($this->body_html) { $mail->IsHTML(true); // send as HTML $mail->Body = $this->body_html; $mail->AltBody = $this->body_text; } else { $mail->IsHtml(false); $mail->Body = $this->body_text; } //$mail->WordWrap = 50; // set word wrap foreach ($this->attachments as $attach) { $mail->AddAttachment($attach[âpathâ],$attach[ânameâ],âbase64â˛,$attach[âtypeâ]); } if (! $mail->Send()) { $this->error=$mail->ErrorInfo; return false; } else { $this->error=false; return true; } } } ?> Copy to app/views/helpers/mail_widget.php <?php /** * MailWidgetHelper * * For usage with MailView * * @author RosSoft * @version 0.1 * @license MIT */ class MailWidgetHelper extends WidgetHelper { âm:toâ, âm:subjectâ, âm:body_textâ, âm:body_htmlâ, âm:attachâ); /** * m:from (Mail From) * $attr[âmailâ] Mail * $attr[ânameâ] Name (optional) */ function tag_m_from($attr,$inner_html) { $this->view->mail->from_name=@$attr[ânameâ]; } /** * m:to (Mail To) * $inner_html Destination address */ function tag_m_to($attr,$inner_html) { $this->view->mail->to[]=$this->_trim($inner_html); } /** * m:subject (Mail Subject) * $inner_html The subject */ function tag_m_subject($attr,$inner_html) { $this->view->mail->subject=$this->_trim($inner_html); } /** * m:body_text Body in plain text * $inner_html The body */ function tag_m_body_text($attr,$inner_html) { $this->view->mail->body_text=$inner_html; } /** * m:body_html Body in html text * $inner_html The body */ function tag_m_body_html($attr,$inner_html) { $this->view->mail->body_html=$inner_html; } /** * m:attach Adds an attachment * $attr[âpathâ] The path of the file * $attr[ânameâ] Overrides the attachment name * $attr[âtypeâ] MIME type */ function tag_m_attach($attr,$inner_html) { $path=$attr[âpathâ]; $name=($attr[ânameâ])? $attr[ânameâ] : ââ; $type=($attr[âtypeâ])? $attr[âtypeâ] : âapplication/octet-streamâ; ânameâ=>$name, âtypeâ=>$type); } /** * Removes the spaces, tabs, newlines from * the beginning and ending of the string * @param string $string * @return string */ function _trim($string) { return $matches[1]; } } ?>
Comments
Subscribe to comments
You need to login to post a comment.
ext; $this->ext=$ext; $fn=$this->getViewFileName($action); $this->ext=$oldext; return $fn; }
}
/** * This object is the interface between MailView and MailWidgetHelper */ class MailAux extends Object { var $charset=âutf-8â˛;
} ?>
Copy to app/views/helpers/mail_widget.php