Source for file Ethna_MailSender.php

Documentation is available at Ethna_MailSender.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_MailSender.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: Ethna_MailSender.php 483 2007-10-30 14:56:00Z cocoitiban $
  10.  */
  11.  
  12. /** メールテンプレートタイプ: 直接送信 */
  13. define('MAILSENDER_TYPE_DIRECT'0);
  14.  
  15.  
  16. // {{{ Ethna_MailSender
  17. /**
  18.  *  メール送信クラス
  19.  *
  20.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  21.  *  @access     public
  22.  *  @package    Ethna
  23.  */
  24. {
  25.     /**#@+
  26.      *  @access private
  27.      */
  28.  
  29.     /** @var    array   メールテンプレート定義 */
  30.     var $def array(
  31.     );
  32.  
  33.     /** @var    string  メールテンプレートディレクトリ */
  34.     var $mail_dir 'mail';
  35.  
  36.     /** @var    int     送信メールタイプ */
  37.     var $type;
  38.  
  39.     /** @var    string  送信オプション */
  40.     var $option '';
  41.  
  42.     /** @var    object  Ethna_Backend   backendオブジェクト */
  43.     var $backend;
  44.  
  45.     /** @var    object  Ethna_Config    設定オブジェクト */
  46.     var $config;
  47.  
  48.     /**#@-*/
  49.  
  50.     /**
  51.      *  Ethna_MailSenderクラスのコンストラクタ
  52.      *
  53.      *  @access public
  54.      *  @param  object  Ethna_Backend   &$backend       backendオブジェクト
  55.      */
  56.     function Ethna_MailSender(&$backend)
  57.     {
  58.         $this->backend =$backend;
  59.         $this->config =$this->backend->getConfig();
  60.     }
  61.  
  62.     /**
  63.      *  メールオプションを設定する
  64.      *
  65.      *  @access public
  66.      *  @param  string  $option メール送信オプション
  67.      */
  68.     function setOption($option)
  69.     {
  70.         $this->option $option;
  71.     }
  72.  
  73.     /**
  74.      *  メールを送信する
  75.      *
  76.      *  $attach の指定方法:
  77.      *  - 既存のファイルを添付するとき
  78.      *  <code>
  79.      *  array('filename' => '/tmp/hoge.xls', 'content-type' => 'application/vnd.ms-excel')
  80.      *  </code>
  81.      *  - 文字列に名前を付けて添付するとき
  82.      *  <code>
  83.      *  array('name' => 'foo.txt', 'content' => 'this is foo.')
  84.      *  </code>
  85.      *  'content-type' 省略時は 'application/octet-stream' となる。
  86.      *  複数添付するときは上の配列を添字0から始まるふつうの配列に入れる。
  87.      *
  88.      *  @access public
  89.      *  @param  string  $to         メール送信先アドレス (nullのときは送信せずに内容を return する)
  90.      *  @param  string  $template   メールテンプレート名 or タイプ
  91.      *  @param  array   $macro      テンプレートマクロ or $templateがMAILSENDER_TYPE_DIRECTのときはメール送信内容)
  92.      *  @param  array   $attach     添付ファイル
  93.      */
  94.     function send($to$template$macro$attach null)
  95.     {
  96.         // メール内容を作成
  97.         if ($template === MAILSENDER_TYPE_DIRECT{
  98.             $mail $macro;
  99.         else {
  100.             $renderer =$this->getTemplateEngine();
  101.  
  102.             // 基本情報設定
  103.             $renderer->setProp("env_datetime"strftime('%Y年%m月%d日 %H時%M分%S秒'));
  104.             $renderer->setProp("env_useragent"$_SERVER["HTTP_USER_AGENT"]);
  105.             $renderer->setProp("env_remoteaddr"$_SERVER["REMOTE_ADDR"]);
  106.  
  107.             // デフォルトマクロ設定
  108.             $macro $this->_setDefaultMacro($macro);
  109.  
  110.             // ユーザ定義情報設定
  111.             if (is_array($macro)) {
  112.                 foreach ($macro as $key => $value{
  113.                     $renderer->setProp($key$value);
  114.                 }
  115.             }
  116.             if (isset($this->def[$template])) {
  117.                 $template $this->def[$template];
  118.             }
  119.             $mail $renderer->perform(sprintf('%s/%s'$this->mail_dir$template)true);
  120.         }
  121.         if ($to === null{
  122.             return $mail;
  123.         }
  124.  
  125.         // メール内容をヘッダと本文に分離
  126.         $mail str_replace("\r\n""\n"$mail);
  127.         list($header$body$this->_parse($mail);
  128.  
  129.         // 添付ファイル (multipart)
  130.         if ($attach !== null{
  131.             $attach = isset($attach[0]$attach array($attach);
  132.             $boundary Ethna_Util::getRandom()
  133.             $body "This is a multi-part message in MIME format.\n\n" .
  134.                 "--$boundary\n.
  135.                 "Content-Type: text/plain; charset=iso-2022-jp\n" .
  136.                 "Content-Transfer-Encoding: 7bit\n\n" .
  137.                 "$body\n";
  138.             foreach ($attach as $part{
  139.                 if (isset($part['content']=== false
  140.                     && isset($part['filename']&& is_readable($part['filename'])) {
  141.                     $part['content'file_get_contents($part['filename']);
  142.                     $part['filename'basename($part['filename']);
  143.                 }
  144.                 if (isset($part['content']=== false{
  145.                     continue;
  146.                 }
  147.                 if (isset($part['content-type']=== false{
  148.                     $part['content-type''application/octet-stream';
  149.                 }
  150.                 if (isset($part['name']=== false{
  151.                     $part['name'$part['filename'];
  152.                 }
  153.                 if (isset($part['filename']=== false{
  154.                     $part['filename'$part['name'];
  155.                 }
  156.                 $part['name'preg_replace('/([^\x00-\x7f]+)/e',
  157.                     "Ethna_Util::encode_MIME('$1')"$part['name'])// XXX: rfc2231
  158.                 $part['filename'preg_replace('/([^\x00-\x7f]+)/e',
  159.                     "Ethna_Util::encode_MIME('$1')"$part['filename']);
  160.  
  161.                 $body .=
  162.                     "--$boundary\n.
  163.                     "Content-Type: " $part['content-type'";\n" .
  164.                         "\tname=\"" $part['name'"\"\n" .
  165.                     "Content-Transfer-Encoding: base64\n" 
  166.                     "Content-Disposition: attachment;\n" .
  167.                         "\tfilename=\"" $part['filename'"\"\n\n";
  168.                 $body .= chunk_split(base64_encode($part['content']));
  169.             }
  170.             $body .= "--$boundary--";
  171.         }
  172.  
  173.         // ヘッダ
  174.         if (isset($header['mime-version']=== false{
  175.             $header['mime-version'array('Mime-Version''1.0');
  176.         }
  177.         if (isset($header['subject']=== false{
  178.             $header['subject'array('Subject''no subject in original');
  179.         }
  180.         if (isset($header['content-type']=== false{
  181.             $header['content-type'array(
  182.                 'Content-Type',
  183.                 $attach === null 'text/plain; charset=iso-2022-jp'
  184.                                  : "multipart/mixed; \n\tboundary=\"$boundary\"",
  185.             );
  186.         }
  187.  
  188.         $header_line "";
  189.         foreach ($header as $key => $value{
  190.             if ($key == 'subject'{
  191.                 // should be added by mail()
  192.                 continue;
  193.             }
  194.             if ($header_line != ""{
  195.                 $header_line .= "\n";
  196.             }
  197.             $header_line .= $value[0": " $value[1];
  198.         }
  199.  
  200.         // 改行コードを CRLF に
  201.         if (strtoupper(substr(PHP_OS03)) == 'WIN'{
  202.             $body str_replace("\n""\r\n"$body);
  203.         }
  204.         $header_line str_replace("\n""\r\n"$header_line);
  205.  
  206.         // 送信
  207.         foreach (to_array($toas $rcpt{
  208.             if (is_string($this->option)) {
  209.                 mail($rcpt$header['subject'][1]$body$header_line$this->option);
  210.             else {
  211.                 mail($rcpt$header['subject'][1]$body$header_line);
  212.             }
  213.         }
  214.     }
  215.  
  216.     /**
  217.      *  アプリケーション固有のマクロを設定する
  218.      *
  219.      *  @access protected
  220.      *  @param  array   $macro  ユーザ定義マクロ
  221.      *  @return array   アプリケーション固有処理済みマクロ
  222.      */
  223.     function _setDefaultMacro($macro)
  224.     {
  225.         return $macro;
  226.     }
  227.  
  228.     /**
  229.      *  テンプレートメールのヘッダ情報を取得する
  230.      *
  231.      *  @access private
  232.      *  @param  string  $mail   メールテンプレート
  233.      *  @return array   ヘッダ, 本文
  234.      */
  235.     function _parse($mail)
  236.     {
  237.         list($header_line$bodypreg_split('/\r?\n\r?\n/'$mail2);
  238.         $header_line .= "\n";
  239.  
  240.         $header_lines explode("\n"$header_line);
  241.         $header array();
  242.         foreach ($header_lines as $h{
  243.             if (strstr($h':'== false{
  244.                 continue;
  245.             }
  246.             list($key$valuepreg_split('/\s*:\s*/'$h2);
  247.             $i strtolower($key);
  248.             $header[$iarray();
  249.             $header[$i][$key;
  250.             $header[$i][preg_replace('/([^\x00-\x7f]+)/e'"Ethna_Util::encode_MIME('$1')"$value);
  251.         }
  252.  
  253.         $body mb_convert_encoding($body"ISO-2022-JP");
  254.  
  255.         return array($header$body);
  256.     }
  257.  
  258.     /**
  259.      *  メールフォーマット用レンダラオブジェクト取得する
  260.      *
  261.      *  @access public
  262.      *  @return object  Ethna_Renderer  レンダラオブジェクト
  263.      */
  264.     function &getRenderer()
  265.     {
  266.         $_ret_object =$this->getTemplateEngine();
  267.         return $_ret_object;
  268.     }
  269.  
  270.     /**
  271.      *  メールフォーマット用レンダラオブジェクト取得する
  272.      *
  273.      *  @access public
  274.      *  @return object  Ethna_Renderer  レンダラオブジェクト
  275.      */
  276.     function &getTemplateEngine()
  277.     {
  278.         $c =$this->backend->getController();
  279.         $renderer =$c->getRenderer();
  280.         return $renderer;
  281.     }
  282. }
  283. // }}}
  284. ?>

Documentation generated on Thu, 08 May 2008 00:14:58 +0900 by phpDocumentor 1.4.2