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

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.