Created
November 9, 2014 20:05
PHP mail function
This file contains hidden or 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
<? | |
function sendmail($aim,$from,$subject,$text, $touid = 0, $attachfile=array()) | |
{ | |
if(!$aim) return 0; | |
$originalsubj = $subject; | |
$charset = "utf-8"; | |
$boundary = "--".md5(uniqid(time())); // любая строка, которой не будет ниже в потоке данных. | |
$EOL = "\r\n"; // ограничитель строк, некоторые почтовые сервера требуют \n - подобрать опытным путём | |
$subject="=?".$charset."?B?".base64_encode($subject)."?="; | |
$html ="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"> | |
<HTML><HEAD> | |
<META content=\"MSHTML 6.00.2800.1106\" name=GENERATOR> | |
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$charset."\" /> | |
<STYLE></STYLE> | |
</HEAD> | |
<BODY bgColor=#ffffff> | |
".$text."\n | |
</BODY></HTML>"; | |
$headers = "MIME-Version: 1.0;$EOL"; | |
$headers .= "Content-Type: multipart/mixed; charset=".$charset."; boundary=\"$boundary\"$EOL"; | |
$headers .= "From: ".$from."$EOL"; | |
$headers .= "X-Mailer: PHP$EOL"; | |
$headers .= "Content-Transfer-Encoding: 8bit$EOL"; | |
$headers .= "Mime-Version: 1.0"; | |
$headers .= phpversion(); | |
$body = "--$boundary$EOL"; | |
$body .= "Content-Type: text/html; charset=".$charset."$EOL"; | |
$body .= "Content-Transfer-Encoding: base64$EOL"; | |
$body .= $EOL; // раздел между заголовками и телом html-части | |
$body .= chunk_split(base64_encode($html)); | |
$body .= "$EOL--$boundary$EOL"; | |
foreach($attachfile as $attach) | |
{ | |
$attachname = $attach['name']; | |
$attachpath = $attach['path']; | |
if($attachname && $attachpath && file_exists($attachpath)) | |
{ | |
$fp = fopen($attachpath,"rb"); | |
$file = fread($fp, filesize($attachpath)); | |
fclose($fp); | |
$body .= "Content-Type: application/octet-stream; name=\"$attachname\"$EOL"; | |
$body .= "Content-Transfer-Encoding: base64$EOL"; | |
$body .= "Content-Disposition: attachment; filename=\"$attachname\"$EOL"; | |
$body .= $EOL; // раздел между заголовками и телом прикрепленного файла | |
$body .= chunk_split(base64_encode($file)); | |
$body .= "$EOL--$boundary$EOL"; | |
} | |
} | |
if (mail($aim, $subject,$body, $headers)) $result = 1; | |
else $result = 0; | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment