CakePHP-通过widget helper查看邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CakePHP-通过widget helper查看邮件相关的知识,希望对你有一定的参考价值。

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(’/^[ s]*(.*)[ s]*$/’,$string,$matches);
  297. return $matches[1];
  298. }
  299.  
  300. }
  301. ?>

以上是关于CakePHP-通过widget helper查看邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Cakephp4 中的 Helper 获取 $this->request->getAttribute('csrfToken')?

cakephp form helper(select)optgroup嵌套选项数组问题

CakePHP HTML Helper添加前缀URL

CakePHP用分页处理评论

NoClassDefFoundError: android.support.v7.widget.helper.ItemTouchHelper$3

zqt_helper 轻松开发Qt5 Widgets应用