1. 云栖社区>
  2. PHP教程>
  3. 正文

PHP SMTP发送邮件函数

作者:用户 来源:互联网 时间:2017-12-01 20:13:23

php邮件函数smtp发送

PHP SMTP发送邮件函数 - 摘要: 本文讲的是PHP SMTP发送邮件函数, PHP SMTP发送邮件函数 使用方法 sendmail('发送地址', '邮件主题', '邮件内容')代码来源:http://www.befen.net/opensource/php-smtp-mail.html $bfconfig


PHP SMTP发送邮件函数

使用方法 sendmail('发送地址', '邮件主题', '邮件内容')代码来源:http://www.befen.net/opensource/php-smtp-mail.html


$bfconfig = Array (
'sitename' => '网站名称',
);
$mail = Array (
'state' => 1,
'server' => 'smtp.abc.com',
'port' => 25,
'auth' => 1,
'username' => [email protected]',
'password' => '123456',
'charset' => 'gbk',
'mailfrom' => [email protected]'
);
function sendmail($mail_to, $mail_subject, $mail_message) {
global $mail, $bfconfig;
date_default_timezone_set('PRC');
$mail_subject = '=?'.$mail['charset'].'?B?'.base64_encode($mail_subject).'?=';
$mail_message = chunk_split(base64_encode(preg_replace("/(^|(rn))(.)/","1.3", $mail_message)));
$headers .="";
$headers .="MIME-Version:1.0rn";
$headers .="Content-type:text/htmlrn";
$headers .="Content-Transfer-Encoding: base64rn";
$headers .="From:".$bfconfig['sitename']."<".$mail['mailfrom'].">rn";
$headers .="Date:".date("r")."rn";
list($msec, $sec) = explode("", microtime());
$headers .="Message-ID: <".date("YmdHis", $sec).".".($msec * 1000000).".".$mail['mailfrom'].">rn";
if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
exit("CONNECT - Unable to connect to the SMTP server");
}
stream_set_blocking($fp, true);
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != '220') {
exit("CONNECT -".$lastmessage);
}
fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')."befenrn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
exit("HELO/EHLO -".$lastmessage);
}
while(1) {
if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
break;
}
$lastmessage = fgets($fp, 512);
}
if($mail['auth']) {
fputs($fp,"AUTH LOGINrn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
exit($lastmessage);
}
fputs($fp, base64_encode($mail['username'])."rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
exit("AUTH LOGIN -".$lastmessage);
}
fputs($fp, base64_encode($mail['password'])."rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 235) {
exit("AUTH LOGIN -".$lastmessage);
}
$email_from = $mail['mailfrom'];
}
fputs($fp,"MAIL FROM: <".preg_replace("/.*<(.+?)>.*/","/1", $email_from).">rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp,"MAIL FROM: <".preg_replace("/.*<(.+?)>.*/","/1", $email_from).">rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
exit("MAIL FROM -".$lastmessage);
}
}
foreach(explode(',', $mail_to) as $touser) {
$touser = trim($touser);
if($touser) {
fputs($fp,"RCPT TO: <".preg_replace("/.*<(.+?)>.*/","/1", $touser).">rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp,"RCPT TO: <".preg_replace("/.*<(.+?)>.*/","/1", $touser).">rn");
$lastmessage = fgets($fp, 512);
exit("RCPT TO -".$lastmessage);
}
}
}
fputs($fp,"DATArn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 354) {
exit("DATA -".$lastmessage);
}
fputs($fp, $headers);
fputs($fp,"To:".$mail_to."rn");
fputs($fp,"Subject: $mail_subjectrn");
fputs($fp,"rnrn");
fputs($fp,"$mail_messagern.rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
exit("END -".$lastmessage);
}
fputs($fp,"QUITrn");
}

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , 邮件 , 函数 , smtp 发送 ,以便于您获取更多的相关知识。

稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一

6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

开发者常用软件,超百款实用软件一站式提供