実現したいこと
PHPで、HTML形式とテキスト形式を含むマルチパートメールを送信したい。メールを送信することはできて、送信先アドレスで受信もできているのですが、本文が(HTML形式もテキスト形式も)表示されていません。
発生している問題・エラーメッセージ
受信側のGmailで「メッセージのソースを表示」したときに表示される内容(一部省略・改変): To:zzzzz@gmail.com Subject: 件名 X-PHP-Originating-Script: 287217:htmlmail.php Content-Type: multipart/alternative; boundary="--67620647262a33a1abdd516.50878460" Content-Transfer-Encoding: binary MIME-Version: 1.0 Content-type: text/html; charset=UTF-8 From: "送信者の名前" <xxxxx@yyyyy.com> Message-Id: <20220610123330.D17C98202C69@yyyyy.com> Date: Fri, 10 Jun 2022 21:33:30 +0900 (JST) ----67620647262a33a1abdd516.50878460 Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable =E4=BB=A3=E6=9B=BF=E3=83=86=E3=82=AD=E3=82=B9=E3=83=88=E3=81=AE=E6=9C=AC=E6= =96=87 ----67620647262a33a1abdd516.50878460 Content-Type: text/html; charset=UTF-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable HTML=E3=83=A1=E3=83=BC=E3=83=AB=E3=81=AE=E6=9C=AC=E6=96=87 ----67620647262a33a1abdd516.50878460 ----67620647262a33a1abdd516.50878460-- ※boundaryが4回出てきて3つのパートに分かれ、最後のパートが空白なのが気になっています...
該当のソースコード
PHP
1<?php 2$mail_from = 'xxxxx@yyyyy.com'; 3$mail_to = 'zzzzz@gmail.com'; 4$mail_from_name = '送信者の名前'; 5 6$subject = '件名'; 7 8$body_text = '代替テキストの本文'; 9$body_html = '<h1>HTMLメール</h1><p>本文</p>'; 10 11$parameter = "-f ".$mail_from; 12 13$boundary = "--".uniqid(rand(),1); 14 15// ヘッダー情報 16$headers = ''; 17$headers .= 'Content-Type: multipart/alternative; boundary="' . $boundary . '"' . "\n"; 18$headers .= 'Content-Transfer-Encoding: binary' . "\n"; 19$headers .= 'MIME-Version: 1.0' . "\n"; 20$headers .= 'Content-type: text/html; charset=UTF-8' . "\n"; 21$headers .= "From: " . mb_encode_mimeheader($mail_from_name) . "<" . $mail_from . ">" . "\n"; 22 23// メッセージ部分 24$message = ''; 25$message .= '--' . $boundary . "\n"; 26$message .= 'Content-Type: text/plain; charset=UTF-8' . "\n"; 27$message .= 'Content-Disposition: inline' . "\n"; 28$message .= 'Content-Transfer-Encoding: quoted-printable' . "\n"; 29$message .= "\n"; 30$message .= quoted_printable_decode ( $body_text ) . "\n"; 31$message .= "\n"; 32$message .= '--' . $boundary . "\n"; 33$message .= 'Content-Type: text/html; charset=UTF-8' . "\n"; 34$message .= 'Content-Disposition: inline' . "\n"; 35$message .= 'Content-Transfer-Encoding: quoted-printable' . "\n"; 36$message .= "\n"; 37$message .= quoted_printable_decode ( $body_html ) . "\n"; 38$message .= '--' . $boundary; 39 40// 送信する 41if(!mail($mail_to,$subject, $message, $headers, $parameter)){ 42 echo "送信失敗"; 43}else{ 44 echo "送信成功"; 45} 46 47
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/10 15:14