/ Published in: PHP
http://rossoft.wordpress.com
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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=â€[email protected]†/> <m:to> </m:to> <m:to> </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) { $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]; } } ?>