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

发布时间:2021-01-24编辑:脚本学堂
为大家举一个php使用smtp发送邮件的代码,简单实用,有需要的朋友,可以参考下。
完整代码如下。
<?php
/**
 * smtp邮件发送函数
 * site www.jb200.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("/(^|(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方式或mail函数方式)
php smtp发送邮件的函数
php中通过curl smtp发送邮件的例子
php使用Pear的NetMail发送smtp邮件
使用pear:Net_SMTP类发送邮件的例子
linux下php配置smtp发送邮件的方法
php写的smtp邮件发送类