Source for file MailSender.php

Documentation is available at MailSender.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  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: 7e21c58cf840a6fa6c3f50e0fc21d7d85da3d7e4 $
  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.     public function __construct($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.             $env_datetime _et('%Y/%m/%d %H:%M:%S');
  104.             $renderer->setProp("env_datetime"strftime($env_datetime));
  105.             $renderer->setProp("env_useragent"$_SERVER["HTTP_USER_AGENT"]);
  106.             $renderer->setProp("env_remoteaddr"$_SERVER["REMOTE_ADDR"]);
  107.  
  108.             // デフォルトマクロ設定
  109.             $macro $this->_setDefaultMacro($macro);
  110.  
  111.             // ユーザ定義情報設定
  112.             if (is_array($macro)) {
  113.                 foreach ($macro as $key => $value{
  114.                     $renderer->setProp($key$value);
  115.                 }
  116.             }
  117.             if (isset($this->def[$template])) {
  118.                 $template $this->def[$template];
  119.             }
  120.             $mail $renderer->perform(sprintf('%s/%s'$this->mail_dir$template)true);
  121.             if (Ethna::isError($mail)) {
  122.                 return $mail;
  123.             }
  124.         }
  125.         if ($to === null{
  126.             return $mail;
  127.         }
  128.  
  129.         // メール内容をヘッダと本文に分離
  130.         $mail str_replace("\r\n""\n"$mail);
  131.         list($header$body$this->_parse($mail);
  132.  
  133.         // 添付ファイル (multipart)
  134.         if ($attach !== null{
  135.             $attach = isset($attach[0]$attach array($attach);
  136.             $boundary Ethna_Util::getRandom()
  137.             $body "This is a multi-part message in MIME format.\n\n" .
  138.                 "--$boundary\n.
  139.                 "Content-Type: text/plain; charset=iso-2022-jp\n" .
  140.                 "Content-Transfer-Encoding: 7bit\n\n" .
  141.                 "$body\n";
  142.             foreach ($attach as $part{
  143.                 if (isset($part['content']=== false
  144.                     && isset($part['filename']&& is_readable($part['filename'])) {
  145.                     $part['content'file_get_contents($part['filename']);
  146.                     $part['filename'basename($part['filename']);
  147.                 }
  148.                 if (isset($part['content']=== false{
  149.                     continue;
  150.                 }
  151.                 if (isset($part['content-type']=== false{
  152.                     $part['content-type''application/octet-stream';
  153.                 }
  154.                 if (isset($part['name']=== false{
  155.                     $part['name'$part['filename'];
  156.                 }
  157.                 if (isset($part['filename']=== false{
  158.                     $part['filename'$part['name'];
  159.                 }
  160.                 $part['name'preg_replace('/([^\x00-\x7f]+)/e',
  161.                     "Ethna_Util::encode_MIME('$1')"$part['name'])// XXX: rfc2231
  162.                 $part['filename'preg_replace('/([^\x00-\x7f]+)/e',
  163.                     "Ethna_Util::encode_MIME('$1')"$part['filename']);
  164.  
  165.                 $body .=
  166.                     "--$boundary\n.
  167.                     "Content-Type: " $part['content-type'";\n" .
  168.                         "\tname=\"" $part['name'"\"\n" .
  169.                     "Content-Transfer-Encoding: base64\n" 
  170.                     "Content-Disposition: attachment;\n" .
  171.                         "\tfilename=\"" $part['filename'"\"\n\n";
  172.                 $body .= chunk_split(base64_encode($part['content']));
  173.             }
  174.             $body .= "--$boundary--";
  175.         }
  176.  
  177.         // ヘッダ
  178.         if (isset($header['mime-version']=== false{
  179.             $header['mime-version'array('Mime-Version''1.0');
  180.         }
  181.         if (isset($header['subject']=== false{
  182.             $header['subject'array('Subject''no subject in original');
  183.         }
  184.         if (isset($header['content-type']=== false{
  185.             $header['content-type'array(
  186.                 'Content-Type',
  187.                 $attach === null 'text/plain; charset=iso-2022-jp'
  188.                                  : "multipart/mixed; \n\tboundary=\"$boundary\"",
  189.             );
  190.         }
  191.  
  192.         $header_line "";
  193.         foreach ($header as $key => $value{
  194.             if ($key == 'subject'{
  195.                 // should be added by mail()
  196.                 continue;
  197.             }
  198.             if ($header_line != ""{
  199.                 $header_line .= "\n";
  200.             }
  201.             $header_line .= $value[0": " $value[1];
  202.         }
  203.  
  204.         // 改行コードを CRLF に
  205.         if (strtoupper(substr(PHP_OS03)) == 'WIN'{
  206.             $body str_replace("\n""\r\n"$body);
  207.         }
  208.         $wa_config 'mail_func_workaround';
  209.         if ($this->config->get($wa_config== false
  210.          && isset($this->options[$wa_config]== false{
  211.             $header_line str_replace("\n""\r\n"$header_line);
  212.         }
  213.  
  214.         // 送信
  215.         foreach (to_array($toas $rcpt{
  216.             if (is_string($this->option)) {
  217.                 mail($rcpt$header['subject'][1]$body$header_line$this->option);
  218.             else {
  219.                 mail($rcpt$header['subject'][1]$body$header_line);
  220.             }
  221.         }
  222.     }
  223.  
  224.     /**
  225.      *  アプリケーション固有のマクロを設定する
  226.      *
  227.      *  @access protected
  228.      *  @param  array   $macro  ユーザ定義マクロ
  229.      *  @return array   アプリケーション固有処理済みマクロ
  230.      */
  231.     function _setDefaultMacro($macro)
  232.     {
  233.         return $macro;
  234.     }
  235.  
  236.     /**
  237.      *  テンプレートメールのヘッダ情報を取得する
  238.      *
  239.      *  @access private
  240.      *  @param  string  $mail   メールテンプレート
  241.      *  @return array   ヘッダ, 本文
  242.      */
  243.     function _parse($mail)
  244.     {
  245.         list($header_line$bodypreg_split('/\r?\n\r?\n/'$mail2);
  246.         $header_line .= "\n";
  247.  
  248.         $header_lines explode("\n"$header_line);
  249.         $header array();
  250.         foreach ($header_lines as $h{
  251.             if (strstr($h':'== false{
  252.                 continue;
  253.             }
  254.             list($key$valuepreg_split('/\s*:\s*/'$h2);
  255.             $i strtolower($key);
  256.             $header[$iarray();
  257.             $header[$i][$key;
  258.             $header[$i][preg_replace('/([^\x00-\x7f]+)/e'"Ethna_Util::encode_MIME('$1')"$value);
  259.         }
  260.  
  261.         $body mb_convert_encoding($body"ISO-2022-JP");
  262.  
  263.         return array($header$body);
  264.     }
  265.  
  266.     /**
  267.      *  メールフォーマット用レンダラオブジェクト取得する
  268.      *
  269.      *  @access public
  270.      *  @return object  Ethna_Renderer  レンダラオブジェクト
  271.      */
  272.     function getRenderer()
  273.     {
  274.         $_ret_object $this->getTemplateEngine();
  275.         return $_ret_object;
  276.     }
  277.  
  278.     /**
  279.      *  メールフォーマット用レンダラオブジェクト取得する
  280.      *
  281.      *  @access public
  282.      *  @return object  Ethna_Renderer  レンダラオブジェクト
  283.      */
  284.     function getTemplateEngine()
  285.     {
  286.         $c $this->backend->getController();
  287.         $renderer $c->getRenderer();
  288.         return $renderer;
  289.     }
  290. }
  291. // }}}

Documentation generated on Fri, 11 Nov 2011 03:58:34 +0900 by phpDocumentor 1.4.3