facebook twitter hatena line email

Php/zend framework/zend mail

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ファイル添付ありサンプル

mb_internal_encoding('UTF-8');

$from = 'admin@example.com';

$toAddress = array();
$toAddress[] = 'test@example.com';
$toAddress[] = 'test2@example.com';

$subject = "title1";
$body = <<<EOD
body1
EOD;
$mail = new Zend_Mail('iso-2022-jp');
$mail->setBodyText(mb_convert_encoding($body, 'ISO-2022-JP', 'UTF-8'));
$mail->setDate(date('r'));
$mail->setFrom($from, mb_encode_mimeheader($from, 'UTF-8', 'B'));
foreach ($toAddress as $to) {
    $mail->addTo($to);
}
$mail->setSubject(mb_encode_mimeheader($subject, 'UTF-8', 'B'));

// 添付 start
$fileName = "test.csv";
$dir = APPLICATION_PATH . '/../data/csvs/';
$filePath = $dir . basename($fileName);

$fp = fopen($filePath, "r");
$myImage = fread($fp, filesize($filePath));
fclose($fp);

$attachment = $mail->createAttachment($myImage);
$attachment->type = 'text/csv';
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->filename = basename($fileName);
// 添付 end

$mail->send();

文字コードマッパーメール

/**
 * メール送信カスタムクラス
 * 
 * @ex
 * $mail = new CustomMail();
 * $mail->addTo($to);
 * $mail->setSubject($subject);
 * $mail->setBodyText($body);
 * $mail->send();
 */
class CustomMail extends Zend_Mail
{
    public function __construct()
    {
        mb_internal_encoding('UTF-8');
        
        parent::__construct('iso-2022-jp');
        
        $this->setDate(date('r'));
        
        // 送信元設定
        $from = 'admin@example.com';
        $this->setFrom($from);
        
        // 送信先設定
        $to = 'test@example.com';
        $this->addTo($to);
    }
    // override
    public function setBodyText($value)
    {
        $value = mb_convert_encoding($value, 'ISO-2022-JP', 'UTF-8');
        parent::setBodyText($value);
    }
    // override
    public function setSubject($value)
    {
        $value = mb_encode_mimeheader($value, 'UTF-8', 'B');
        parent::setSubject($value);
    }
    // override
    public function setFrom($value)
    {
        $value = mb_encode_mimeheader($value, 'UTF-8', 'B');
        parent::setFrom($value);
    }
}

参照

http://framework.zend.com/manual/ja/zend.mail.html