Posted By

jonbaer on 07/06/06


Tagged

email php textmate cakephp


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jkochis


CakePHP - Mail view through widget helper


 / Published in: PHP
 

http://rossoft.wordpress.com

  1. <?php
  2. /**
  3.  * Mail Templates
  4.  * Sends email through a view
  5.  *
  6.  * The templates rendered must have extension .mail
  7.  *
  8.  * Uses:
  9.  * @link http://phpmailer.sourceforge.net/
  10.  * @see MailWidgetHelper
  11.  *
  12.  * @author RosSoft
  13.  * @version 0.1
  14.  * @license MIT
  15.  *
  16.  *
  17.  * Usage example:
  18.  * First, edit this file and change the define(’CONFIG_SMTP…
  19.  * with your ISP config.
  20.  *
  21.  * file views/foo/mail_view.mail
  22.   <m:from name=”Miguel Ros” mail=”[email protected]” />
  23.   <m:to>
  24.   </m:to>
  25.  
  26.   <m:to>
  27.   </m:to>
  28.  
  29.   <m:subject> Hi! That is the subject
  30.   </m:subject>
  31.  
  32.   <m:body_text>
  33.   This is the plain body text
  34.   </m:body_text>
  35.  
  36.   <m:body_html>
  37.   This is the HTML <b>Body</b>
  38.   </m:body_html>
  39.  
  40.   <m:attach path=”/tmp/test1.txt” name=”test_file_1.txt” />
  41.   <m:attach path=”/tmp/test2.txt” />
  42.   File views/foo/mail_view.thtml:
  43.   The mail has been set
  44.   Action in FooController:
  45.   function mail_view()
  46.   {
  47.   $this->view=’Mail’;
  48.   $this->render(); //render the mail_view.mail
  49.  
  50.   $this->view=’View’;
  51.   $this->render(); //now the mail_view.thtml renders
  52.   }
  53.  
  54.  */
  55.  
  56. define(’CONFIG_SMTP_HOST’,'xxxx’);
  57. define(’CONFIG_SMTP_USER’,'yyyy’);
  58. define(’CONFIG_SMTP_PASS’,'zzzz’);
  59.  
  60. //Directory within VENDORS/
  61. define(’PHPMAILER_SUBDIR’,'phpmailer’ . DS);
  62.  
  63. class MailView extends View
  64. {
  65. var $mail=null; //instance of MailAux
  66.  
  67. function render($action = null, $layout = null, $file = null)
  68. {
  69. $this->mail=& new MailAux;
  70. $this->controller->mail=& $this->mail;
  71.  
  72. $file=null;
  73.  
  74. $this->load_helper(’MailWidget’);
  75.  
  76. if ($action==NULL)
  77. {
  78. $action=$this->action;
  79. }
  80. $file=$this->get_filename($action,’.mail’);
  81. parent::render($action,$layout,$file);
  82. return $this->mail->send();
  83. }
  84.  
  85. /**
  86.   * Adds a helper to the helpers array for loading it
  87.   * @param string $helper Name of the helper like ‘Javascript’
  88.   */
  89. function load_helper($helper)
  90. {
  91. if (!in_array($helper,$this->helpers))
  92. {
  93. $this->helpers[]=$helper;
  94. }
  95. }
  96.  
  97. /**
  98.   * Returns the filename associated with the action
  99.   * with the extension “.$ext”
  100.   *
  101.   * @param string $action If null, then current action
  102.   * @param string $ext Extension of the view template (with dot) Example: ‘.xml’
  103.   * @return string
  104.   */
  105. function get_filename($action,$ext)
  106. {
  107. $old_ext=$this->ext;
  108. $this->ext=$ext;
  109. $fn=$this->_getViewFileName($action);
  110. $this->ext=$old_ext;
  111. return $fn;
  112. }
  113.  
  114. }
  115.  
  116. /**
  117.  * This object is the interface between MailView and MailWidgetHelper
  118.  */
  119. class MailAux extends Object
  120. {
  121. var $charset=’utf-8′;
  122.  
  123. var $from_mail=null;
  124. var $from_name='’;
  125. var $to=array();
  126. var $subject='’;
  127. var $body_text=null;
  128. var $body_html=null;
  129. var $attachments=array();
  130.  
  131. /**
  132.   * SMTP Configuration
  133.   */
  134. var $host=CONFIG_SMTP_HOST;
  135. var $username=CONFIG_SMTP_USER;
  136. var $password=CONFIG_SMTP_PASS;
  137.  
  138. /**
  139.   * It contains mailer error message or false if no
  140.   * error has occurred
  141.   */
  142. var $error=false;
  143.  
  144.  
  145. function send()
  146. {
  147. vendor(PHPMAILER_SUBDIR. ‘class.phpmailer’);
  148. $mail = new PHPMailer();
  149. $mail->PluginDir = VENDORS .PHPMAILER_SUBDIR ;
  150. $mail->SetLanguage(’en’,VENDORS .PHPMAILER_SUBDIR . ‘language’ . DS);
  151.  
  152. $mail->CharSet= $this->charset;
  153. $mail->IsSMTP(); // send via SMTP
  154. $mail->Host = $this->host; // SMTP servers
  155. if ($this->username !==null)
  156. {
  157. $mail->SMTPAuth = true; // turn on SMTP authentication
  158. $mail->Username = $this->username; // SMTP username
  159. $mail->Password = $this->password; // SMTP password
  160. }
  161. $mail->From = $this->from_mail;
  162. $mail->FromName = $this->from_name;
  163. foreach ($this->to as $address)
  164. {
  165. $mail->AddAddress($address);
  166. }
  167. $mail->Subject = $this->subject;
  168. if ($this->body_html)
  169. {
  170. $mail->IsHTML(true); // send as HTML
  171. $mail->Body = $this->body_html;
  172. $mail->AltBody = $this->body_text;
  173. }
  174. else
  175. {
  176. $mail->IsHtml(false);
  177. $mail->Body = $this->body_text;
  178. }
  179. //$mail->WordWrap = 50; // set word wrap
  180. foreach ($this->attachments as $attach)
  181. {
  182. $mail->AddAttachment($attach[’path’],$attach[’name’],’base64′,$attach[’type’]);
  183.  
  184. }
  185. if (! $mail->Send())
  186. {
  187. $this->error=$mail->ErrorInfo;
  188. $this->log(’Mail send:’ . $mail->ErrorInfo);
  189. return false;
  190. }
  191. else
  192. {
  193. $this->error=false;
  194. return true;
  195. }
  196.  
  197. }
  198. }
  199. ?>
  200.  
  201. Copy to app/views/helpers/mail_widget.php
  202.  
  203. <?php
  204. /**
  205.  * MailWidgetHelper
  206.  *
  207.  * For usage with MailView
  208.  *
  209.  * @author RosSoft
  210.  * @version 0.1
  211.  * @license MIT
  212.  */
  213.  
  214. class MailWidgetHelper extends WidgetHelper
  215. {
  216. var $tag=array( ‘m:from’,
  217. ‘m:to’,
  218. ‘m:subject’,
  219. ‘m:body_text’,
  220. ‘m:body_html’,
  221. ‘m:attach’);
  222.  
  223. /**
  224.   * m:from (Mail From)
  225.   * $attr[’mail’] Mail
  226.   * $attr[’name’] Name (optional)
  227.   */
  228. function tag_m_from($attr,$inner_html)
  229. {
  230. $this->view->mail->from_name[email protected]$attr[’name’];
  231. $this->view->mail->from_mail[email protected]$attr[’mail’];
  232. }
  233.  
  234. /**
  235.   * m:to (Mail To)
  236.   * $inner_html Destination address
  237.   */
  238. function tag_m_to($attr,$inner_html)
  239. {
  240. $this->view->mail->to[]=$this->_trim($inner_html);
  241. }
  242.  
  243. /**
  244.   * m:subject (Mail Subject)
  245.   * $inner_html The subject
  246.   */
  247. function tag_m_subject($attr,$inner_html)
  248. {
  249. $this->view->mail->subject=$this->_trim($inner_html);
  250. }
  251.  
  252. /**
  253.   * m:body_text Body in plain text
  254.   * $inner_html The body
  255.   */
  256. function tag_m_body_text($attr,$inner_html)
  257. {
  258. $this->view->mail->body_text=$inner_html;
  259. }
  260.  
  261.  
  262. /**
  263.   * m:body_html Body in html text
  264.   * $inner_html The body
  265.   */
  266. function tag_m_body_html($attr,$inner_html)
  267. {
  268. $this->view->mail->body_html=$inner_html;
  269. }
  270.  
  271. /**
  272.   * m:attach Adds an attachment
  273.   * $attr[’path’] The path of the file
  274.   * $attr[’name’] Overrides the attachment name
  275.   * $attr[’type’] MIME type
  276.   */
  277. function tag_m_attach($attr,$inner_html)
  278. {
  279. $path=$attr[’path’];
  280. $name=($attr[’name’])? $attr[’name’] : ‘’;
  281. $type=($attr[’type’])? $attr[’type’] : ‘application/octet-stream’;
  282.  
  283. $this->view->mail->attachments[]=array( ‘path’=>$path,
  284. ‘name’=>$name,
  285. ‘type’=>$type);
  286. }
  287.  
  288. /**
  289.   * Removes the spaces, tabs, newlines from
  290.   * the beginning and ending of the string
  291.   * @param string $string
  292.   * @return string
  293.   */
  294. function _trim($string)
  295. {
  296. preg_match(’/^[\n\t\s]*(.*)[\n\t\s]*$/’,$string,$matches);
  297. return $matches[1];
  298. }
  299.  
  300. }
  301. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: zaman on October 7, 2009

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′;

var $from_mail=null;
var $from_name='’;
var $to=array();
var $subject='’;
var $body_text=null;
var $body_html=null;   
var $attachments=array();

/**
 * 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;
        $this->log(’Mail send:’ . $mail->ErrorInfo);
        return false;           
    }
    else
    {
        $this->error=false;
        return true;
    }

}

} ?>

Copy to app/views/helpers/mail_widget.php

You need to login to post a comment.