письмо с аттачем
Много уже написано на эту тему, но все-равно я не могу разобраться с одним вопросом! Как отправить письмо с телом html, чтобы в тело были вставлены картинки?
Вот как делаю:
Код:
<html>
<body>
<?php
// çàãîëîâêè
$headers = "From: <xxx@mail.ru>\n";
$headers .= "Reply-To: <xxx@mail.ru>\n";
$headers .= "MIME-Version: 1.0\n";
// â îäíó ñòðî÷êó
$headers .= "Content-Type: multipart/mixed; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: Programm<xxx@mail.ru>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n";
$headers .= "Return-Path: <xxx@mail.ru>\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
// html
$message .= "----=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: text/html;\n charset=\"windows-1251\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
// çäåñÿ
$message .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
$message .= "<HTML><BODY>\n";
$message .= "Ïðèâåò! Ëîâè ôàéë!";
$message .= "<IMG src=\"cid:some_attach\">";
$message .= "</BODY></HTML>\n";
$message .= "\n";
// êîíåö
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "\n";
$file = fopen('JPG.jpg','rb');
$data = fread($file,filesize('JPG.jpg '));
fclose($file);
$FileContent=chunk_split(base64_encode($data));
// äîáàâëÿåì àòà÷ìåíòû
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octetstream; name=\"JPG.jpg\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-ID: <some_attach>\n";
$message .= "\n";
$message .= $FileContent;
$message .= "\n";
//êîíåö ñîîáùåíèÿ
$message .= "------=MIME_BOUNDRY_main_message--\n";
$f = mail($var_mail, $subject, $message, $headers);
if ($f) {
?>
<SCRIPT>
alert('Ôàéë îòïðàâëåí.');
</script>
</body>
</html>
<?
}
else {
?>
<SCRIPT>
alert('Ôàéë íå îòïðàâëåí.');
</script>
</body>
</html>
<?
}
?>
<body>
<?php
// çàãîëîâêè
$headers = "From: <xxx@mail.ru>\n";
$headers .= "Reply-To: <xxx@mail.ru>\n";
$headers .= "MIME-Version: 1.0\n";
// â îäíó ñòðî÷êó
$headers .= "Content-Type: multipart/mixed; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Sender: Programm<xxx@mail.ru>\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n";
$headers .= "Return-Path: <xxx@mail.ru>\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
// html
$message .= "----=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: text/html;\n charset=\"windows-1251\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
// çäåñÿ
$message .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
$message .= "<HTML><BODY>\n";
$message .= "Ïðèâåò! Ëîâè ôàéë!";
$message .= "<IMG src=\"cid:some_attach\">";
$message .= "</BODY></HTML>\n";
$message .= "\n";
// êîíåö
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "\n";
$file = fopen('JPG.jpg','rb');
$data = fread($file,filesize('JPG.jpg '));
fclose($file);
$FileContent=chunk_split(base64_encode($data));
// äîáàâëÿåì àòà÷ìåíòû
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octetstream; name=\"JPG.jpg\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-ID: <some_attach>\n";
$message .= "\n";
$message .= $FileContent;
$message .= "\n";
//êîíåö ñîîáùåíèÿ
$message .= "------=MIME_BOUNDRY_main_message--\n";
$f = mail($var_mail, $subject, $message, $headers);
if ($f) {
?>
<SCRIPT>
alert('Ôàéë îòïðàâëåí.');
</script>
</body>
</html>
<?
}
else {
?>
<SCRIPT>
alert('Ôàéë íå îòïðàâëåí.');
</script>
</body>
</html>
<?
}
?>
Загвозка в том, что картинка <IMG src=\"cid:some_attach\"> не находится! Даже указав абсолютный путь, я не могу заставить ее отображаться.
Примного благодарен за помощь.
могу дать проверенную функцию.
function send_mime_mail($tomail, $subject, $message, $files)
{
$headers="From:
Reply-To:
Organization:
X-Priority: 3 (Normal)
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=\"----------6D21133B9A9485\"\n
------------6D21133B9A9485
Content-Type: text/html; charset=Windows-1251
Content-Transfer-Encoding: 8bit
$message\n";
$mime="";
for ($i=0; $i<count($files); $i++)
{
$attachment = file_get_contents($files[$i]);
$fname = pathinfo($files[$i]);
$fname=$fname["basename"];
$fanme1=$files[$i];
if (strpos($fanme1,".doc")<>false) $fname="Подробности сотрудничества.doc";
$mime.="------------6D21133B9A9485
Content-Type: application/gzip;name=\"$fname\"\nContent-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$fname\" \n\n".chunk_split(base64_encode($attachment))."\n\n";
}
$mime.="------------6D21133B9A9485--\n";
$headers=$headers.$mime;
$headers=str_replace("\r","",$headers);
mail($tomail, $subject, "", $headers.$mime);
}