搜索
首页php教程php手册完整邮件发送类
完整邮件发送类Jun 13, 2016 am 11:23 AM
classportpublicsmtptvarvariables发送完整邮件

完整邮件发送类
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* Private Variables */
var $sock;

/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
 $this->debug = FALSE;
 $this->smtp_port = $smtp_port;
 $this->relay_host = $relay_host;
 $this->time_out = 30; //is used in fsockopen()
 $this->auth = $auth;//auth
 $this->user = $user;
 $this->pass = $pass;
 $this->host_name = "localhost"; //is used in HELO command
 $this->log_file = "";
 $this->sock = FALSE;
}

/* Main Function */
function sendmail($to, $from ,$fromname, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = ""){
 $mail_from = $this->get_address($this->strip_comment($from));
 $body = ereg_replace("(^|(rn))(.)", "1.3", $body);
 $header .= "MIME-Version:1.0rn";
 if($mailtype=="HTML"){
  $header .= "Content-Type:text/html;charset=utf-8rn";
 }
 $header .= "To: ".$to."rn";
 if ($cc != ""){
  $header .= "Cc: ".$cc."rn";
 }
 $header .= "From: $fromnamern";
 $header .= "Subject: ".$subject."rn";
 $header .= $additional_headers;
 $header .= "Date: ".date("r")."rn";
 $header .= "X-Mailer:By Redhat (PHP/".php教程version().")rn";
 list($msec, $sec) = explode(" ", microtime());
 $header .= "Message-ID: rn";
 $TO = explode(",", $this->strip_comment($to));

 if ($cc != ""){
  $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
 }
 if ($bcc != ""){
  $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
 }
 $sent = TRUE;
 foreach ($TO as $rcpt_to){
  $rcpt_to = $this->get_address($rcpt_to);
  if (!$this->smtp_sockopen($rcpt_to)){
   $this->log_write("Error: Cannot send email to ".$rcpt_to."n");
   $sent = FALSE;
   continue;
  }
  if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)){
   $this->log_write("E-mail has been sent to n");
  }else{
   $this->log_write("Error: Cannot send email to n");
   $sent = FALSE;
  }
  fclose($this->sock);
  $this->log_write("Disconnected from remote hostn");
 }
 return $sent;
}

 /* Private Functions */
 function smtp_send($helo, $from, $to, $header, $body = ""){
  if (!$this->smtp_putcmd("HELO", $helo)){
   return $this->smtp_error("sending HELO command");
  }
  
  #auth
  if($this->auth){
   if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))){
    return $this->smtp_error("sending HELO command");
   }
   if (!$this->smtp_putcmd("", base64_encode($this->pass))){
    return $this->smtp_error("sending HELO command");
   }
  }
  if (!$this->smtp_putcmd("MAIL", "FROM:")){
   return $this->smtp_error("sending MAIL FROM command");
  }
  if (!$this->smtp_putcmd("RCPT", "TO:")){
   return $this->smtp_error("sending RCPT TO command");
  }
  if (!$this->smtp_putcmd("DATA")){
   return $this->smtp_error("sending DATA command");
  }
  if (!$this->smtp_message($header, $body)){
   return $this->smtp_error("sending message");
  }
  if (!$this->smtp_eom()){
   return $this->smtp_error("sending . [EOM]");
  }
  if (!$this->smtp_putcmd("QUIT")){
   return $this->smtp_error("sending QUIT command");
  }
  return TRUE;
 }

 function smtp_sockopen($address){
  if ($this->relay_host == ""){
   return $this->smtp_sockopen_mx($address);
  }
  else{
   return $this->smtp_sockopen_relay();
  }
 }

 function smtp_sockopen_relay(){
  $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."n");
  $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  if (!($this->sock && $this->smtp_ok())){
   $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."n");
   $this->log_write("Error: ".$errstr." (".$errno.")n");
   return FALSE;
  }
  $this->log_write("Connected to relay host ".$this->relay_host."n");
  return TRUE;;
 }
 
 function smtp_sockopen_mx($address){
  $domain = ereg_replace("^.+@([^@]+)$", "1", $address);
  if (!@getmxrr($domain, $MXHOSTS)){
   $this->log_write("Error: Cannot resolve MX "".$domain.""n");
   return FALSE;
  }
  foreach ($MXHOSTS as $host){
   $this->log_write("Trying to ".$host.":".$this->smtp_port."n");
   $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
   if (!($this->sock && $this->smtp_ok())){
    $this->log_write("Warning: Cannot connect to mx host ".$host."n");
    $this->log_write("Error: ".$errstr." (".$errno.")n");
    continue;
   }
   $this->log_write("Connected to mx host ".$host."n");
   return TRUE;
  }
  $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
  return FALSE;
 }

 function smtp_message($header, $body){
  fputs($this->sock, $header."rn".$body);
  $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
  return TRUE;
 }
 
 function smtp_eom(){
  fputs($this->sock, "rn.rn");
  $this->smtp_debug(". [EOM]n");
  return $this->smtp_ok();
 }

 function smtp_ok(){
  $response = str_replace("rn", "", fgets($this->sock, 512));
  $this->smtp_debug($response."n");
  if (!ereg("^[23]", $response)){
   fputs($this->sock, "QUITrn");
   fgets($this->sock, 512);
   $this->log_write("Error: Remote host returned "".$response.""n");
   return FALSE;
  }
  return TRUE;
 }

 function smtp_putcmd($cmd, $arg = ""){
  if($arg != ""){
   if($cmd==""){
    $cmd = $arg;
   }
   else{
    $cmd = $cmd." ".$arg;
   }
  }
  fputs($this->sock, $cmd."rn");
  $this->smtp_debug("> ".$cmd."n");
  return $this->smtp_ok();
 }

 function smtp_error($string){
  $this->log_write("Error: Error occurred while ".$string.".n");
  return FALSE;
 }

 function log_write($message){
  $this->smtp_debug($message);
  if ($this->log_file == ""){
   return TRUE;
  }
  $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))){
   $this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""n");
   return FALSE;;
  }
  flock($fp, LOCK_EX);
  fputs($fp, $message);
  fclose($fp);
  return TRUE;
 }

 function strip_comment($address){//去除"()"
  $comment = "([^()]*)";
  while (ereg($comment, $address)){
   $address = ereg_replace($comment, "", $address);
  }
  return $address;
 }

 function get_address($address){
  $address = ereg_replace("([trn])+", "", $address);//t 制表符 r 回车符 n  换行符  +匹配前面的子表达式一次或多次
  $address = ereg_replace("^.*.*$", "1", $address);
  return $address;
 }

 function smtp_debug($message){
  if ($this->debug){
   echo $message;
  }
 }
}


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP异步发送邮件:避免长时间等待邮件发送完成。PHP异步发送邮件:避免长时间等待邮件发送完成。Sep 19, 2023 am 09:10 AM

PHP异步发送邮件:避免长时间等待邮件发送完成。导言:在Web开发中,发送邮件是常见的功能之一。但是,由于邮件发送需要与服务器进行通信,往往会导致用户在等待邮件发送完成的过程中出现长时间的等待。为了解决这个问题,我们可以使用PHP异步发送邮件的方式来优化用户体验。本文将介绍如何通过具体的代码示例实现PHP异步发送邮件,并避免长时间的等待。一、理解异步发送邮件

告别 Windows 11 中的远程邮件槽协议告别 Windows 11 中的远程邮件槽协议Apr 14, 2023 pm 10:28 PM

我们最近一直在谈论微软计划添加到其最新操作系统Windows11中的许多功能。但是,不要以为微软只会添加什么也不收回。事实上,这家软件巨头开始删除相当多的旧功能。在宣布计划在Windows12发布之前停用MSDT功能后,雷德蒙德开发人员带来了更多的坏消息。我们实际上是在谈论远程邮件槽旧版工具。当我们说您实际上想知道这一点时,请相信我们。Microsoft已开始在内部版本25314中弃用此功能我们相信您还记得,就在几天前,微软在其新的金丝雀频道发布了内部版本25314。上述版本包含许多新功能

如何修复 Outlook 电子邮件卡在发件箱问题如何修复 Outlook 电子邮件卡在发件箱问题May 01, 2023 am 10:01 AM

最近,许多用户报告了Outlook邮件卡在发件箱中的问题。即使多次尝试发送电子邮件,问题也没有得到解决。当您看到此问题并检查您的发件箱文件夹时,该消息将卡在那里。电子邮件卡在Outlook发件箱中的可能原因是:电子邮件中的附件超过了大小限制,这会减慢发送过程。邮件服务器的Outlook帐户身份验证问题Outlook或邮件服务器脱机Outlook中的发送/接收设置不正确。其他一些软件正在使用Outlook数据文件。防病毒软件会扫描传出的电子邮件。如果这个问题一直困扰着您并且您无法发送电子邮

PHP邮件追踪功能:了解用户对邮件的行为和反馈。PHP邮件追踪功能:了解用户对邮件的行为和反馈。Sep 19, 2023 am 08:51 AM

PHP邮件追踪功能:了解用户对邮件的行为和反馈在现代社会中,电子邮件已经成为人们日常生活和工作中必不可少的一部分。对于企业来说,发送邮件是与客户进行沟通、推广产品或服务的重要方式之一。然而,一封邮件被发送出去后,我们如何知道它是否被收到、被读取,或者用户对邮件内容有何反应?这时,邮件追踪功能就显得尤为重要了。邮件追踪功能可以帮助我们了解用户对邮件的行为和反馈

公共预览版即将推出,其中包括 Windows 11 和 Windows 10 的最新 Outlook 应用。公共预览版即将推出,其中包括 Windows 11 和 Windows 10 的最新 Outlook 应用。May 09, 2023 am 08:07 AM

作为更新Windows11原生应用程序的一部分,微软计划发布新的Outlook。该应用程序是从头开始制作的,现在正在为预览版做准备,这可能会在微软的Windows11混合活动期间宣布。该项目被称为“ProjectMonarch”,这个新的Outlook已经开发了一年多。这是网络应用程序的重新启动,旨在统一所有现有的Windows电子邮件客户端,例如邮件和日历以及桌面版Outlook。通过OutlookOne,微软希望帮助用户跨不同的桌面平台管理他们的电子邮件。有很多方法可以访问

PHP和PHPMAILER:如何实现邮件发送的自动过滤功能?PHP和PHPMAILER:如何实现邮件发送的自动过滤功能?Jul 21, 2023 am 09:25 AM

PHP和PHPMAILER:如何实现邮件发送的自动过滤功能?在现代社会中,电子邮件已成为人们交流的重要方式之一。然而,随着电子邮件的流行和广泛使用,垃圾邮件的数量也呈现出爆炸式增长的趋势。垃圾邮件不仅会浪费用户的时间和网络资源,还可能带来病毒和钓鱼行为。因此,在开发邮件发送功能时,加入自动过滤垃圾邮件的功能变得至关重要。本文将介绍如何使用PHP和PHPMai

如何修复 Windows 11 的邮件应用程序无法正常工作如何修复 Windows 11 的邮件应用程序无法正常工作May 23, 2023 pm 09:41 PM

为什么我的Windows11邮件应用程序无法运行?邮件应用无法在Windows11中运行的潜在原因有很多。当该应用根本无法启动时,可能是因为系统文件损坏。或者应用程序本身可能已过时或以某种方式损坏。Windows11包括可以解决此类问题的工具和修复选项。Windows11Mail应用程序不发送电子邮件可能是由于许多同步问题。例如,某些第三方防病毒软件和防火墙可能会阻止应用程序同步电子邮件和日历。此类安全实用程序还可能导致WindowsMail应用程序不下载附件。由于某

修复:Windows 11、10 中邮件和日历应用程序的错误代码 0x80070490修复:Windows 11、10 中邮件和日历应用程序的错误代码 0x80070490Apr 13, 2023 pm 09:13 PM

一些 Windows 用户在尝试将 Gmail 或任何其他电子邮件帐户添加到 Windows PC 上的邮件应用程序时报告了错误消息“出现问题,我们很抱歉,但我们无法做到这一点”以及错误代码0x80070490 在屏幕上。即使经过多次尝试,客户也无法将任何电子邮件帐户添加到他们的邮件应用程序中。用户非常不满意,并且不确定如何从这里转移。在邮件应用程序中添加电子邮件帐户时出现此错误的可能原因可能是系统数据文件损坏、邮件应用程序的一些内部问题、过时的邮件应用程序等。在分析了上述可能导致此错误的原因后

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。