Регистрация Войти
Войти через VK Войти через FB Войти через Google Войти через Яндекс
Войти через VK Войти через FB Войти через Google Войти через Яндекс
Поиск по сайту
Отправка писем с вложенными картинками на PHP
Пример простой php-функции для отправки писем с картинками внутри.
/**
* отправка письма с вложениями
*
* @param string $from
* @param string $to
* @param string $subject
* @param string $text
* @param string $cc
* @param string $arr массив ассоциативных массивов с прикладываемыми файлами
* $arr[] = array(
type = тип mime,
name = имя файла (лучше английское),
content = содержимое файла закодированное base64_encode()
)
* @return unknown
*/
function multipart_mail($from, $to, $subject, $text,$cc=null,$arr=null) {
global $domain;
if($domain=='') $domain='http://'.$_SERVER['HTTP_HOST'];
$headers ="From: $from\r\n";
$headers.="To: $to\r\n";
if (!is_null($cc)) {
$headers.="Cc: $cc\r\n";
}
$headers.="Subject: $subject\r\n";
$headers.="Date: ".date("r")."\r\n";
$headers.="X-Mailer: zm php script\r\n";
$headers.="MIME-Version: 1.0\r\n";
$headers.="Content-Type: multipart/alternative;\r\n";
$baseboundary="------------".strtoupper(md5(uniqid(rand(), true)));
$headers.=" boundary=\"$baseboundary\"\r\n";
$headers.="This is a multi-part message in MIME format.\r\n";
$message="--$baseboundary\r\n";
$message.="Content-Type: text/plain; charset=windows-1251\r\n";
$text_plain=str_replace('<p>',"\r\n",$text);
$text_plain=str_replace('<b>',"",$text_plain);
$text_plain=str_replace('</b>',"",$text_plain);
$text_plain=str_replace('<br>',"\r\n",$text_plain);
$text_plain= preg_replace('/<a(\s+)href="([^"]+)"([^>]+)>([^<]+)/i'," \$4\n\$2",$text_plain);
$message.=strip_tags($text_plain);
//$message.="Content-Transfer-Encoding: 7bit\r\n\r\n";
$message.="\r\n\r\nIts simple text. Switch to HTML view!\r\n\r\n";
$message.="--$baseboundary\r\n";
$newboundary="------------".strtoupper(md5(uniqid(rand(), true)));
$message.="Content-Type: multipart/related;\r\n";
$message.=" boundary=\"$newboundary\"\r\n\r\n\r\n";
$message.="--$newboundary\r\n";
$message.="Content-Type: text/html; charset=windows-1251\r\n";
$message.="Content-Transfer-Encoding: 7bit\r\n\r\n";
$message.=($text)."\r\n\r\n";
preg_match_all('/img(\s+)src="([^"]+)"/i',$text,$m);
if (isset($m[2])) {
$img_f=$m[2];
if (is_array($img_f)) {
foreach ($img_f as $k => $v) {
$img_f[$k]=str_ireplace($domain,$_SERVER['DOCUMENT_ROOT'],$v);
}
}
}
$attachment_files=$img_f;
if (is_array($attachment_files)) {
foreach($attachment_files as $filename) {
$file_content = file_get_contents($filename,true);
$mime_type='image/png';
if(function_exists("mime_content_type")) {
$mime_type=mime_content_type($filename);
}
else {
switch (file_ext($filename)) {
case 'jpg': $mime_type='image/jpeg';break;
case 'gif': $mime_type='image/gif';break;
case 'png': $mime_type='image/png';break;
default:;
}
}
$message=str_replace($domain.'/'.$filename,'cid:'.basename($filename),$message);
$filename=basename($filename);
$message.="--$newboundary\r\n";
$message.="Content-Type: $mime_type;\r\n";
$message.=" name=\"$filename\"\r\n";
$message.="Content-Transfer-Encoding: base64\r\n";
$message.="Content-ID: <$filename>\r\n";
$message.="Content-Disposition: inline;\r\n";
$message.=" filename=\"$filename\"\r\n\r\n";
$message.=chunk_split(base64_encode($file_content));
}
if(!is_null($arr)){
foreach ($arr as $v) {
$mime_type=$v['type'];
$filename=$v['name'];
$message.="--$newboundary\r\n";
$message.="Content-Type: $mime_type;\r\n";
$message.=" name=\"$filename\"\r\n";
$message.="Content-Transfer-Encoding: base64\r\n";
$message.="Content-ID: <$filename>\r\n";
$message.="Content-Disposition: inline;\r\n";
$message.=" filename=\"$filename\"\r\n\r\n";
$message.=chunk_split($v['content']);
}
}
}
$message.="--$newboundary--\r\n\r\n";
$message.="--$baseboundary--\r\n";
return mail($to, $subject, $message , $headers);
}
Смотрите также:
- Универсальная рассылка почты на PHP
- Вставка изображения в письмо на PHP
- Отправка почты по шаблону на PHP
- Удалить все сообщения из почтового ящика
- Настройки SPF и DKIM чтобы письма не попадали в спам
- email рассылка по шаблону на PHP
.
Прокомментировать/Отблагодарить