> トップページ > 関数 > メール送信
メール送信
$mail関数を利用してメールを送信します。
コード
/**
* $to 宛てにメールを送信する
*
* @param string $from 送信元メールアドレス
* @param string $to 宛先
* @param string $subject 件名
* @param string $message 本文
* @return bool 1;成功 0:失敗
*/
function MailFunction($from, $to, $subject, $message)
{
// Subject部の文字コードを変換
$x_subject = mb_convert_encoding($subject, "JIS", "auto");
$x_subject = base64_enbox3($x_subject);
$x_subject = "=?iso-2022-jp?B?".$x_subject."?=";
// Message部の文字コードを変換
$x_message = htmlspecialchars($message);
$x_message = str_replace("&", "&", $x_message); // html escape
if (get_magic_quotes_gpc()) {
$x_message = stripslashes($x_message);
}
$x_message = str_replace("\r\n", "\r", $x_message);
$x_message = str_replace("\r", "\n", $x_message);
$x_message = mb_convert_encoding($x_message, "JIS", "auto");
// メールヘッダを生成
$gmt = date("Z");
$gmt_abs = abs($gmt);
$gmt_hour = floor($gmt_abs / 3600);
$gmt_min = floor(($gmt_abs - $gmt_hour * 3600) / 60);
if ($gmt >= 0) {
$gmt_flg = "+";
} else {
$gmt_flg = "-";
}
$gmt_rfc = date("D, d M Y H:i:s ").sprintf($gmt_flg."%02d%02d", $gmt_hour, $gmt_min);
$headers = "Date: {$gmt_rfc}\n";
$headers .= "From: {$from}\n";
$headers .= "Subject: {$x_subject}\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "X-Mailer: PHP/".phpversion()."\n";
$headers .= "Content-type: text/plain; charset=ISO-2022-JP\n";
$headers .= "Content-Transfer-Encoding: 7bit";
// メール送信実行
if(mail($to, $x_subject, $x_message, $headers)) {
return true;
}
return false;
}
サンプル
$from = 'from@hoge.com'; $to = 'to@hoge.com'; $subject = '件名'; $body = '本文1行目\n'; $body .= '本文2行目\n'; MailFunction($from, $to, $subject, $body);