Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

phpメール送信例/メモ

More than 3 years have passed since last update.
mail.php
// SETUP
$to[] = 'test@testtest.jp';
$to[] = 'testtest2@yahoo.co.jp';
$from_email= 'sender@address.co.jp';
$from_name = '送信者名';
$subject = 'メールのタイトル';

$body = "

このメールは、自動で送信されるメールです。

資料を送信致します。
添付ファイルをご参考して下さい。

以上です。
";

$attach_file = "/path/path/path/file.dat";

// send the email
foreach( $to as $to_email ) {
sendmail_jpn($to_email, $subject, $body, $from_email,$from_name, $attach_file);
sleep(1);
}

echo "送信完了";

//関数-----------------------------------

function sendmail_jpn($to, $subject, $message, $from_email,$from_name, $filepath)
{
$mime_type = "application/octet-stream";

// 添付ファイルのエンコード
$filename = basename( $filepath );

// マルチパートなので、パートの区切り文字列を指定
$boundary = '----=_Boundary_' . uniqid(rand(1000,9999) . '_') . '_';

// 件名のエンコード
$subject = mb_convert_encoding($subject, 'ISO-2022-JP', 'SJIS');
$subject = mb_encode_mimeheader_ex($subject);

// 本文のエンコード
$message = mb_convert_encoding($message, 'ISO-2022-JP', 'SJIS');

// toをエンコード
// $to = mb_convert_encoding($mail['to']['name'], "SJIS", "SJIS");
$to = "=?ISO-2022-JP?B?" . base64_encode($to) . '?= <' . $to . '>';

// fromをエンコード
// $from = mb_convert_encoding($mail['from']['name'], "SJIS", "SJIS");
$from = "=?ISO-2022-JP?B?" . base64_encode($from_name) . '?= <' . $from_email . '>';

// 添付ファイルのエンコード
$filename = mb_convert_encoding($filename, 'ISO-2022-JP', 'SJIS');
$filename = "=?ISO-2022-JP?B?" . base64_encode($filename) . "?=";

// ヘッダーの指定
$head = "";
$head .= "From: {$from}\n";
$head .= "MIME-Version: 1.0\n";
$head .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\n";
$head .= "Content-Transfer-Encoding: 7bit";

$body = "";

// 本文
$body .= "--{$boundary}\n";
$body .= "Content-Type: text/plain; charset=ISO-2022-JP;" .
"Content-Transfer-Encoding: 7bit\n";
$body .= "\n";
$body .= "{$message}\n";
$body .= "\n";

// 添付ファイルの処理
$body .= "--{$boundary}\n";
$body .= "Content-Type: {$mime_type}; name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment; filename=\"{$filename}\"\n";
$body .= "\n";

$fp = fopen( $filepath, "r" ) or die("Error on mailing. (attachment file cannot open)");
$contents = fread( $fp, filesize($filepath) );
fclose( $fp );
$f_encoded = chunk_split(base64_encode($contents)); //添付ファイルをbase64エンコードする
$body .= "{$f_encoded}\n";
$body .= "\n";

if (mail($to, $subject, $body, $head)) {
echo 'sendmail_jpn : OK.';
} else {
echo 'sendmail_jpn : FAILURE.';
}
}

// mb_encode_mimeheaderのバグ対策用
function mb_encode_mimeheader_ex($text, $split_count = 34) {
$position = 0;
$encorded = '';

while ($position < mb_strlen($text, 'ISO-2022-JP')) {
if ($encorded != '') {
$encorded .= "\r\n ";
}
$output_temp = mb_strimwidth($text, $position, $split_count, '', 'ISO-2022-JP');
$position = $position + mb_strlen($output_temp, 'ISO-2022-JP');
$encorded .= "=?ISO-2022-JP?B?" . base64_encode($output_temp) . "?=";
}

return $encorded;
}
KanaeYou
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away