PHPでHTMLメールを送る方法

PHPでHTMLメールなどメールソースを記述する場合には、mb_send_mail関数は使用できません。
HTMLメールや添付ファイルを付けたメールを送信するには、mail関数またはpopen関数を使用します。

HTMLメールや添付ファイル、携帯メールのデコメールなどを送るときは、自分宛にメールを送信して、受信したファイルのソースを参考にするといいです。
[参考記事] mb_send_mail、mb_encode_mimeheaderの文字化けのまとめ(半角カタカナなど)
[参考記事] mb_send_mailでCCやBCCを指定する 表示名を指定する
[参考記事] mail関数やmb_send_mail関数でReturn-Pathを設定する方法
[参考記事] PCからデコメールを送るときの仕様
[参考記事] mailto本文での改行 ドコモのN、Pで送信に失敗します
[参考記事] mailtoの使い方
[参考記事] 携帯サイトでのmailtoの使い方

RFCの仕様ではメールの改行コードはCR+LFとなります。
RFC2821(SMTP)/RFC1939(POP3)/RFC2822(Internet Message Format)

mail関数を使用する場合

$mail_from      = 'test@example.com';
$mail_to        = 'hoge@example.net';
$mail_from_name = '送信者の名前';

$subject = '件名';

$body_text = '代替テキストの本文';
$body_html = 'HTMLメールの本文';

$parameter = "-f ".$mail_from;

$boundary = "--".uniqid(rand(),1);

// ヘッダー情報
$headers = '';
$headers .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '"' . "\r\n";
$headers .= 'Content-Transfer-Encoding: binary' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: " . mb_encode_mimeheader($mail_from_name) . "<" . $mail_from . ">" . "\r\n";
// 送信者名を指定しない場合は次のよう
// $headers .= "From: " . $mail_from . "\r\n";

// メッセージ部分
$message = '';
$message .= '--' . $boundary . "\r\n";
$message .= 'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$message .= 'Content-Disposition: inline' . "\r\n";
$message .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n";
$message .= "\r\n";
$message .= quoted_printable_decode ( $body_text ) . "\r\n";
$message .= "\r\n";
$message .= '--' . $boundary . "\r\n";
$message .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$message .= 'Content-Disposition: inline' . "\r\n";
$message .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n";
$message .= "\r\n";
$message .= quoted_printable_decode ( $body_html ) . "\r\n";
$message .= '--' . $boundary . "\r\n";

// 送信する
if(!mail($mail_to,$subject, $message, $headers, $parameter)){
  echo "送信失敗";
}else{
  echo "送信成功";
}

popen関数を使用する場合

$mail_from      = 'test@example.com';
$mail_to        = 'hoge@example.net';
$mail_from_name = '送信者の名前';

$subject = '件名';

$body_text = '代替テキストの本文';
$body_html = 'HTMLメールの本文';

$boundary = "--".uniqid(rand(),1);
$subject = mb_encode_mimeheader(mb_convert_encoding($subject, "JIS", "auto"), "JIS");

// ヘッダー情報
$headers = '';
$headers .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '"' . "\r\n";
$headers .= 'Content-Transfer-Encoding: binary' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: " . mb_encode_mimeheader($mail_from_name) . "<" . $mail_from . ">" . "\r\n";
// 送信者名を指定しない場合は次のよう
// $headers .= "From: " . $mail_from . "\r\n";
$headers .= 'To: ' . $mail_to . "\r\n";
$headers .= 'Subject: ' . $subject . "\r\n";

// メッセージ部分
$message = '';
$message .= '--' . $boundary . "\r\n";
$message .= 'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$message .= 'Content-Disposition: inline' . "\r\n";
$message .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n";
$message .= "\r\n";
$message .= quoted_printable_decode ( $body_text ) . "\r\n";
$message .= "\r\n";
$message .= '--' . $boundary . "\r\n";
$message .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$message .= 'Content-Disposition: inline' . "\r\n";
$message .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n";
$message .= "\r\n";
$message .= quoted_printable_decode ( $body_html ) . "\r\n";
$message .= '--' . $boundary . "\r\n";

// 送信する
//sendmailへのプロセスを開く
if(!($mp = @popen("/usr/sbin/sendmail -f $mail_from $mail_to", "w")){
  echo "送信失敗";
  exit;
}

fputs($mp, $headers);
fputs($mp, $message);

//sendmailへのプロセスを開放
pclose($mp);

関連記事

スポンサーリンク

||演算子 文字列の結合

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る