Created
June 17, 2014 14:16
-
-
Save iredun/46f0ce244327453fc155 to your computer and use it in GitHub Desktop.
PHP функция для отправки сообщения на почту с вложением файла
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Параметры | |
//$mail_to-email адресата | |
//$thema-тема письма | |
//$html-текст письма в html формате | |
//$patch - путь до файла который отправляем | |
//Можно использовать и при отправки без вложения | |
function send_mail($mail_to, $thema, $html, $path){ | |
if ($path) { | |
$fp = fopen($path,"rb"); | |
if (!$fp) | |
{ print "Cannot open file"; | |
exit(); | |
} | |
$file = fread($fp, filesize($path)); | |
fclose($fp); | |
} | |
$name=$path;// в этой переменной надо сформировать имя файла (без всякого пути) | |
$EOL = "\r\n"; // ограничитель строк, некоторые почтовые сервера требуют \n - подобрать опытным путём | |
$boundary = "--".md5(uniqid(time())); // любая строка, которой не будет ниже в потоке данных. | |
$headers = "MIME-Version: 1.0;$EOL"; | |
$headers .= "Content-Type: multipart/mixed; charset=utf-8; boundary=\"$boundary\"$EOL"; | |
$headers .= "From: =?utf-8?b?" . base64_encode("СЭД СтГАУ") . "?= <sed@stgau.ru>$EOL"; | |
$headers .= "X-Mailer: PHP/" . phpversion(); | |
$multipart = "--$boundary$EOL"; | |
$multipart .= "Content-Type: text/html$EOL"; | |
$multipart .= "Content-Transfer-Encoding: base64$EOL"; | |
$multipart .= $EOL; // раздел между заголовками и телом html-части | |
$multipart .= chunk_split(base64_encode($html)); | |
if ($path) { | |
$multipart .= "$EOL--$boundary$EOL"; | |
$multipart .= "Content-Type: application/octet-stream; name=\"$name\"$EOL"; | |
$multipart .= "Content-Transfer-Encoding: base64$EOL"; | |
$multipart .= "Content-Disposition: attachment; filename=\"$name\"$EOL"; | |
$multipart .= $EOL; // раздел между заголовками и телом прикрепленного файла | |
$multipart .= chunk_split(base64_encode($file)); | |
} | |
$multipart .= "$EOL--$boundary--$EOL"; | |
if(!mail($mail_to, $thema, $multipart, $headers)) | |
{return False; //если не письмо не отправлено | |
} | |
else { //// если письмо отправлено | |
return True; | |
} | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment