Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php使用smtp发送邮件的实现代码

php使用smtp发送邮件的实现代码

发布时间:2017-10-09   编辑:www.jquerycn.cn
为大家举一个php使用smtp发送邮件的代码,简单实用,有需要的朋友,可以参考下。
完整代码如下。
<?php
/**
 * smtp邮件发送函数
 * site www.jbxue.com
 使用方法 sendmail('发送地址', '邮件主题', '邮件内容')
*/

$bfconfig = Array (
 'sitename' => '脚本学堂', //这里填写网站名称
 );

$mail = Array (
 'state' => 1,
 'server' => 'smtp.abc.com',
 'port' => 25,
 'auth' => 1,
 'username' => 'admin@abc.com',
 'password' => '123456',
 'charset' => 'gbk',
 'mailfrom' => 'admin@abc.com'
 );

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("/(^|(\r\n))(\.)/", "\1.\3", $mail_message)));

 $headers .= "";
 $headers .= "MIME-Version:1.0\r\n";
 $headers .= "Content-type:text/html\r\n";
 $headers .= "Content-Transfer-Encoding: base64\r\n";
 $headers .= "From: ".$bfconfig['sitename']."<".$mail['mailfrom'].">\r\n";
 $headers .= "Date: ".date("r")."\r\n";
 list($msec, $sec) = explode(" ", microtime());
 $headers .= "Message-ID: <".date("YmdHis", $sec).".".($msec * 1000000).".".$mail['mailfrom'].">\r\n";

 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')." befen\r\n");
 $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 LOGIN\r\n");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 334) {
   exit($lastmessage);
  }

  fputs($fp, base64_encode($mail['username'])."\r\n");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 334) {
   exit("AUTH LOGIN - ".$lastmessage);
  }

  fputs($fp, base64_encode($mail['password'])."\r\n");
  $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).">\r\n");
 $lastmessage = fgets($fp, 512);
 if(substr($lastmessage, 0, 3) != 250) {
  fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
  $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).">\r\n");
   $lastmessage = fgets($fp, 512);
   if(substr($lastmessage, 0, 3) != 250) {
    fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
    $lastmessage = fgets($fp, 512);
    exit("RCPT TO - ".$lastmessage);
   }
  }
 }

 fputs($fp, "DATA\r\n");
 $lastmessage = fgets($fp, 512);
 if(substr($lastmessage, 0, 3) != 354) {
  exit("DATA - ".$lastmessage);
 }

 fputs($fp, $headers);
 fputs($fp, "To: ".$mail_to."\r\n");
 fputs($fp, "Subject: $mail_subject\r\n");
 fputs($fp, "\r\n\r\n");
 fputs($fp, "$mail_message\r\n.\r\n");
 $lastmessage = fgets($fp, 512);
 if(substr($lastmessage, 0, 3) != 250) {
  exit("END - ".$lastmessage);
 }

 fputs($fp, "QUIT\r\n");
}
?>

您可能感兴趣的文章:
php 邮件发送类(smtp方式或mail函数方式)
php smtp发送邮件的函数
php中通过curl smtp发送邮件的例子
php使用Pear的NetMail发送smtp邮件
使用pear:Net_SMTP类发送邮件的例子
linux下php配置smtp发送邮件的方法
php写的smtp邮件发送类

您可能感兴趣的文章:
使用pear:Net_SMTP类发送邮件的例子
php中通过curl smtp发送邮件的例子
php使用Pear的NetMail发送smtp邮件
phpmailer实现的简单openvpn用户认证的代码
《Perl编程24学时教程》笔记第22课 CGI发送电子邮件
PHPMailer批量发送邮件的实例代码
使用phpmailer发送邮件的例子
php使用phpMailer发送邮件的例子
有关phpmailer的用法
php Socket发送邮件的代码(支持发信认证)

关键词: smtp  发送邮件   
[关闭]