facebook twitter hatena line email

Php/メール添付メモ

提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:16時点における127.0.0.1 (トーク)による版 (ページの作成:「==必須ライブラリ== Mail Mail_Mime Net_SMTP ==ロジック== <?php require_once("Mail.php"); require_once("Mail/mime.php"); //言語設定、内部エンコ...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

必須ライブラリ

Mail

Mail_Mime

Net_SMTP

ロジック

<?php
require_once("Mail.php");
require_once("Mail/mime.php");
//言語設定、内部エンコーディングを指定する
mb_language("japanese");
mb_internal_encoding('SJIS');
$params = array(
  "host" => "localhost",
  "port" => 25,
  "auth" => false
);
$mail = Mail::factory("smtp", $params);
// 配信先メアド(複数,区切り)
$recipients = "test@example.com";
$body = <<<EOD
○添付ファイル
説明・・・
EOD;
$body = mb_convert_encoding($body, "ISO-2022-JP", "sjis");
$mime = new Mail_Mime("\n");
$mime->setTxtBody($body);
$mime->addAttachment("/home/admin/test.csv", "csv/text");
$bodyParam = array(
  "head_charset" => "ISO-2022-JP",
  "text_charset" => "ISO-2022-JP"
);
$body = $mime->get($bodyParam);
$title = "ショップ移行状態リスト";
$title = mb_convert_encoding($title, "ISO-2022-JP", "sjis");
$addHeaders = array(
  "Date" => date("r"),
  "To" => "test@example.com",
  "From" => "test@example.com",
  "Subject" => $title
);
$headers = $mime->headers($addHeaders);
$ret = $mail->send($recipients, $headers, $body);
if (PEAR::isError($ret)) {
 error_log($ret->getMessage());
}

添付なしPear::Mail

// 言語設定、内部エンコーディングを指定する
mb_language("japanese");
mb_internal_encoding('SJIS');

$header['From'] = "test@example.com";
$header['To'] = $this->_spinfo['email'];
$header['Reply-To'] = "test@example.com";

$title = mb_convert_encoding($mail['title'], "ISO-2022-JP", "sjis");
$header['Subject'] = $title;

$address = array($this->_spinfo['email']);

$body = mb_convert_encoding($mail['body'], "ISO-2022-JP", "sjis");

$mail = Mail::factory("sendmail");
$ret = $mail->send($address, $header, $body);
if (PEAR::isError($ret)) {
 error_log($ret->getMessage());
}

添付なしメール(send_mail

mb_language("Ja") ;
mb_internal_encoding("UTF-8") ;
$mailto="test@example.com";
$subject="title";
$content="body";
$mailfrom="From:" .mb_encode_mimeheader("from") ."<test@example.com>";
mb_send_mail($mailto,$subject,$content,$mailfrom);

zend添付ありメール

mb_internal_encoding('UTF-8');
$from = "admin@example.com";
$toAddress = array();
$toAddress[] = "test@example.com";
$toAddress[] = "test2@example.com";
$subject = "タイトル";
$body = <<<EOD
○添付ファイル
・・・
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'));
$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);
$mail->send();