Skip to content

Instantly share code, notes, and snippets.

@vasily802
Created November 9, 2014 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasily802/f26842fd49b328d2d086 to your computer and use it in GitHub Desktop.
Save vasily802/f26842fd49b328d2d086 to your computer and use it in GitHub Desktop.
PHP mail function
<?
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