PHPのソケット接続を用いてのマルチパートメールを送信したい
PHPのSocket接続でメールを送信する機能を作成したいです。
内容としては
HTMLメールとplain textメールを受信側で切り替える実装にしたいです。
HTMLメールでは画像も表示できればとお思い実装しましたがうまくいきません。
ご回答いただけたら大変助かります。
下記がメールのヘッダー部分で、
受信側のメールにはGmailを使っているのでHTMLが表示されるはずなのですが...
メールBodyはHTMLタグがそのまま表示されてしまう状況です。
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-type: multipart/alternative;boundary= f13d209adaf2fc2d6323d31b9e6f1789
--f13d209adaf2fc2d6323d31b9e6f1789
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>テスト<strong>配信</strong>です。</p>
<p>k.inoue.test 宛</p>
--f13d209adaf2fc2d6323d31b9e6f1789
Content-type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html><body><p>テスト<strong>配信</strong>です。</p>
<p>k.inoue.test 宛</p>
</body></html>
--f13d209adaf2fc2d6323d31b9e6f1789--
下記がコードになります。
DBから宛先や本文を取り出し変数に格納しています。
$crlf = "\r\n";
$receive = array();
$message = str_replace("\n",$crlf,stripslashes($message));
$subject = '=?iso-2022-jp?B?' . base64_encode(mb_convert_encoding($subject,'ISO-2022-JP','UTF-8')) . '?=';
// $message = mb_convert_encoding($message,'ISO-2022-JP','UTF-8');
$from = '=?iso-2022-jp?B?' . base64_encode(mb_convert_encoding($fromName . ' ','ISO-2022-JP','UTF-8')) . '?=' . $crlf . mb_convert_encoding(' <' . $fromEmail . '>','ISO-2022-JP','UTF-8');
if ($localhost == false) {
$server = 'my-ip-address';
} else {
$server = 'localhost';
}
$boundary=md5(uniqid(rand()));
ini_set('sendmail_from',$return_path);
$sock = fsockopen($server,25,$errno,$errstr,60);
if(!$sock) return false;
$receive[] = fgets($sock,1024);
fputs($sock,'HELO ' . $server . $crlf);
$receive[] = fgets($sock,1024);
fputs($sock,'RSET' . $crlf);
$receive[] = fgets($sock,1024);
fputs($sock,'MAIL FROM: ' . $return_path . $crlf);
$receive[] = fgets($sock,1024);
fputs($sock,'RCPT TO: ' . $to . $crlf);
$receive[] = fgets($sock,1024);
fputs($sock,'DATA' . $crlf);
$receive[] = fgets($sock,1024);
fputs($sock,'Date: ' . date('r') . $crlf);
fputs($sock,'Return-Path: <' . $return_path . '>' . $crlf);
fputs($sock,'To: ' . $to . $crlf);
fputs($sock,'From: ' . $from . $crlf);
fputs($sock,'Reply-To: <' . $fromEmail . '>' . $crlf);
fputs($sock,'Subject: ' . $subject . $crlf);
fputs($sock,'Message-Id: <' . md5(uniqid(rand())) . preg_replace('/[^\x00-\x7f]/','',$fromEmail) . '>' . $crlf);
fputs($sock,'MIME-Version: 1.0' . $crlf);
fputs($sock,'Content-Transfer-Encoding: 7bit' . $crlf);
fputs($sock,"Content-type: multipart/alternative;boundary= $boundary" . $crlf);
fputs($sock,"--" . $boundary . $crlf);
fputs($sock,$crlf);
// body start
fputs($sock,'Content-Type: text/plain; charset=UTF-8' . $crlf);
fputs($sock,'Content-Transfer-Encoding: 7bit' . $crlf);
fputs($sock,$crlf);
fputs($sock,$message);
fputs($sock,$crlf);
fputs($sock,"--" . $boundary . $crlf);
fputs($sock,$crlf);
fputs($sock,'Content-type: text/html; charset=UTF-8' . $crlf);
fputs($sock,'Content-Transfer-Encoding: 7bit' . $crlf);
fputs($sock,$crlf);
fputs($sock,'<html><body>'. $message. '</body></html>');
fputs($sock,$crlf);
fputs($sock,"--" . $boundary . "--".$crlf);
fputs($sock,$crlf . '.' . $crlf);
$receive[] = fgets($sock,1024);
fputs($sock,'QUIT' . $crlf);
$receive[] = fgets($sock,1024);
fclose($sock);
エンコードの問題なのか、改行や境界の問題なのかが
見当もつかない状態です。。。
ご回答いただけましたら大変助かります。
よろしくお願い致します。