用smtp协议php发送email函数(发邮件)_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > 用smtp协议php发送email函数(发邮件)

用smtp协议php发送email函数(发邮件)

 2012/6/18 16:26:56  ling凌yue月  程序员俱乐部  我要评论(0)
  • 摘要:网上找发送email的函数,基本没找到,有发邮件的类,自己试着写了一个smtp的,发送邮件倒是可以了,不过还不知道怎么发附件,先记录一下,再研究,慢慢更新。测试了163的smtp,可以发送。functionsendEmail($host,$port=25,$check=0,$username='',$password='',$fromemail,$toemail,$fromName='',$toName='',$subject='',$message='',$additional=''
  • 标签:PHP 函数 邮件 协议

? ?网上找发送email的函数,基本没找到,有发邮件的类,自己试着写了一个smtp的,发送邮件倒是可以了,不过还不知道怎么发附件,先记录一下,再研究,慢慢更新。

? ?测试了163的smtp,可以发送。

?

?

function sendEmail($host, $port=25, $check=0, $username='', $password='', $fromemail, $toemail, $fromName=''
	, $toName='', $subject='', $message='', $additional='', $charset='utf-8')
{
	$CRLF = "\r\n";
	$fromName = trim($fromName) == '' ? $fromemail : $fromName;
	$toName = trim($toName) == '' ? $toemail : $toName;
	$send_from = "=?$charset?B?".base64_encode($fromName)."?= <$fromemail>";
	$send_to = "=?$charset?B?".base64_encode($toName)."?= <$toemail>";
	$send_subject = "=?$charset?B?".base64_encode(str_replace(array("\r","\n"), array('',' '),$subject)).'?=';
	$send_message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", 
		str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message)))))));
	$additional = "To: {$send_to}{$CRLF}From: {$send_from}{$CRLF}MIME-Version: 1.0{$CRLF}Content-type: text/html; charset=$charset{$CRLF}{$additional}Content-Transfer-Encoding: base64{$CRLF}";
	$fp = fsockopen($host, $port, $errno, $errstr);
	if (!$fp)
	{
		return 'SCE'; //smtp connect error
	}
	if (strncmp(fgets($fp, 512), '220', 3) != 0)
	{
		return 'SCE'; //smtp connect error
	}
	fwrite($fp, "EHLO $fromName{$CRLF}");
	if ($check)
	{
		while ($rt=strtolower(fgets($fp, 512)))
		{
			if (strpos($rt, "-") !== 3 || empty($rt))
			{
				break;
			}
			elseif (strpos($rt, "2") !== 0)
			{
				return "AE";//auth error
			}
		}
		fwrite($fp, "AUTH LOGIN{$CRLF}");
		if (strncmp(fgets($fp, 512), '334', 3) != 0)
		{
			return "ALE";//auth login error
		}
		fwrite($fp, base64_encode($username).$CRLF);
		if (strncmp(fgets($fp,512), '334', 3) != 0)
		{
			return "ALUE";//auth login username error
		}
		fwrite($fp, base64_encode($password).$CRLF);
		if (strncmp(fgets($fp, 512), '235', 3) != 0)
		{
			return 'ALPE';//auth login password error
		}

	}
	$from = preg_replace("/.*\<(.+?)\>.*/", "\\1", $fromemail);
	fwrite($fp, "MAIL FROM: <$from>$CRLF");
	if (strncmp(fgets($fp, 512), '250', 3) != 0)
	{
		return 'EFE'; //email from error
	}
	fwrite($fp, "RCPT TO: <$toemail>$CRLF");
	if (strncmp(fgets($fp,512), '250', 3) != 0)
	{
		return 'ETE';//email toemail error
	}
	fwrite($fp, "DATA$CRLF");
	if (strncmp(fgets($fp, 512), '354', 3) != 0)
	{
		return 'EDE';//email data error
	}
	$msg  = "Date: ".Date("r").$CRLF;
	$msg .= "Subject: $send_subject".$CRLF;
	$msg .= $additional.$CRLF;
	$msg .= $send_message.$CRLF.".".$CRLF;
	fwrite($fp, $msg);
	$lastmessage = fgets($fp, 512);
	if (substr($lastmessage, 0, 3) != 250)
	{
		return 'ESE';//email send error
	}
	fwrite($fp, "QUIT\r\n");
	fclose($fp);
	return 'OK';
}
?

?

?

?

发表评论
用户名: 匿名