`

phpmailer【二】class.phpmailer.php源代码说明(部分)

    博客分类:
  • php
阅读更多

  phpmailer中的部分代码说明

  验证邮件的格式

 

 /** 
   * 验证邮件地址
   * Conforms approximately to RFC2822
   * @link http://www.hexillion.com/samples/#Regex Original pattern found here
   * @param string $address The email address to check
   * @return boolean
   * @static
   * @access public
   */
  public static function ValidateAddress($address) {
	 //存在filter_var方法使用filter_var方法直接验证,否则正则表达式验证
    if (function_exists('filter_var')) { //Introduced in PHP 5.2
      if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
        return false;
      } else {
        return true;
      }
    } else {
      return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
    }
  }

 

  设置者发送者邮件发送名称

/**
 * 设置发送者的邮件和发送者名称
 * @param string $address  发送者邮件
 * @param string $name 发送者名称
 * @param string $auto 是否为自动回复
 * @return boolean
 */
  public function SetFrom($address, $name = '',$auto=1) {
    $address = trim($address);
    $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
     //验证发送者的邮件地址
    if (!self::ValidateAddress($address)) {
      $this->SetError($this->Lang('invalid_address').': '. $address);
      if ($this->exceptions) {
        throw new phpmailerException($this->Lang('invalid_address').': '.$address);
      }
      echo $this->Lang('invalid_address').': '.$address;
      return false;
    }
	//设置发送和发送者的用户名称
    $this->From = $address;
    $this->FromName = $name;
	//设置自动回复,将回复人设置为发送人
    if ($auto) {
      if (empty($this->ReplyTo)) {
        $this->AddAnAddress('ReplyTo', $address, $name);
      }
      if (empty($this->Sender)) {
        $this->Sender = $address;
      }
    }
    return true;
  }

 发送邮件

 

  /**
   * Creates message and assigns Mailer. If the message is
   * not sent successfully then it returns false.  Use the ErrorInfo
   * variable to view description of the error.
   * @return bool
   */
  public function Send() {
    try {
	  //接受人,抄送人,密送人之和小于1抛出异常
      if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
        throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
      }

      // Set whether the message is multipart/alternative
      if(!empty($this->AltBody)) {
        $this->ContentType = 'multipart/alternative';
      }
	  //将错误信息清空
      $this->error_count = 0; // reset errors
	  //设置消息类型
      $this->SetMessageType();
	  //创建头
      $header = $this->CreateHeader();
	  //创建实体
      $body = $this->CreateBody();
	  //实体为空抛出异常
      if (empty($this->Body)) {
        throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
      }

      // digitally sign with DKIM if enabled
      if ($this->DKIM_domain && $this->DKIM_private) {
        $header_dkim = $this->DKIM_Add($header,$this->Subject,$body);
        $header = str_replace("\r\n","\n",$header_dkim) . $header;
      }

      // 选择邮件发方式
      switch($this->Mailer) {
        case 'sendmail':
          return $this->SendmailSend($header, $body);
       //采用smtp格式发送邮件
        case 'smtp':
          return $this->SmtpSend($header, $body);
        default:
          return $this->MailSend($header, $body);
      }

    } catch (phpmailerException $e) {
      $this->SetError($e->getMessage());
      if ($this->exceptions) {
        throw $e;
      }
      echo $e->getMessage()."\n";
      return false;
    }
  }

 创建邮件头

  /**
   * 组装信件的头
   * @access public
   * @return string The assembled header
   */
  public function CreateHeader() {
    $result = '';

    // Set the boundaries
    $uniq_id = md5(uniqid(time()));
    $this->boundary[1] = 'b1_' . $uniq_id;
    $this->boundary[2] = 'b2_' . $uniq_id;
	//创建邮件的发送日期
    $result .= $this->HeaderLine('Date', self::RFCDate());
	//创建邮件的回复地址
    if($this->Sender == '') {
      $result .= $this->HeaderLine('Return-Path', trim($this->From));
    } else {
      $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
    }

    // To be created automatically by mail()
    if($this->Mailer != 'mail') {
      if ($this->SingleTo === true) {
        foreach($this->to as $t) {
          $this->SingleToArray[] = $this->AddrFormat($t);
        }
      } else {
        if(count($this->to) > 0) {
          $result .= $this->AddrAppend('To', $this->to);
        } elseif (count($this->cc) == 0) {
          $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
        }
      }
    }
	 //追加发件人的信息
    $from = array();
    $from[0][0] = trim($this->From);
    $from[0][1] = $this->FromName;
    $result .= $this->AddrAppend('From', $from);

    // sendmail and mail() extract Cc from the header before sending
	//追加抄送者信息
    if(count($this->cc) > 0) {
      $result .= $this->AddrAppend('Cc', $this->cc);
    }
	
    // sendmail and mail() extract Bcc from the header before sending
	//追加密送者信息
    if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
      $result .= $this->AddrAppend('Bcc', $this->bcc);
    }
	//追加回复者的信息
    if(count($this->ReplyTo) > 0) {
      $result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
    }

    // mail() sets the subject itself
	//不是mail形式时,追加主题
    if($this->Mailer != 'mail') {
      $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
    }
	//追加消息ID 
    if($this->MessageID != '') {
      $result .= $this->HeaderLine('Message-ID',$this->MessageID);
    } else {
      $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
    }
	//追加自定义的邮件紧急程度
    $result .= $this->HeaderLine('X-Priority', $this->Priority);
	//追加自定义使用哪个版本的phpmailer发送
	//X-Mailer是代理发信的客户端
    $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (phpmailer.sourceforge.net)');

    if($this->ConfirmReadingTo != '') {
      $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
    }

    // Add custom headers
	//追加自定义的头信息
    for($index = 0; $index < count($this->CustomHeader); $index++) {
      $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
    }
	//设置MIME版本号
    if (!$this->sign_key_file) {
      $result .= $this->HeaderLine('MIME-Version', '1.0');
	  //设置文件的类型
      $result .= $this->GetMailMIME();
    }

    return $result;
  }

  /**
   * Returns the message MIME.
   * @access public
   * @return string
   */
  public function GetMailMIME() {
    $result = '';
    switch($this->message_type) {
	  //邮件类型为plain
      case 'plain':
		 //设置编码格式
        $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
		//设置MIME文件类型及字符集
        $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
        break;
	  //邮件包含附件
      case 'attachments':
      case 'alt_attachments':
		 //如果有嵌入式图片,设置MIME文件类型为multipart/related
	     //不存在嵌入式图片,设置MIME文件类型为multipart/mixed
        if($this->InlineImageExists()){
          $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
        } else {
          $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
          $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
        }
        break;
      case 'alt':
        $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
        $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
        break;
    }

    if($this->Mailer != 'mail') {
	  //追加两个换行
      $result .= $this->LE.$this->LE;
    }

    return $result;
  }

 

php生成的发送邮件头的格式

Date: Sat, 3 Jul 2010 12:38:51 +0000 
Return-Path: *****@126.com 
To: John Doe 
From: First Last 
Reply-to: First Last 
Subject: =?gbk?B?suLK1A==?= 
Message-ID: <8336f09c2d8910f62a6525c1ed92863c@127.0.0.1> 
X-Priority: 3 
X-Mailer: PHPMailer 5.1 (phpmailer.sourceforge.net) 
MIME-Version: 1.0 
Content-Type: multipart/alternative; boundary="b1_8336f09c2d8910f62a6525c1ed92863c" 

 

明天再贴其他部分

 

分享到:
评论

相关推荐

    class.phpmailer.php

    class.phpmailer.php类

    php中用phpmailer发送邮件.pdf

    php中用phpmailer发送邮件.pdf

    免积分下载 PHPMailer-master.zip

    用于在PHP后端使用邮件发送功能,具体实现方法可以参考菜鸟教程,可以直接拿来用:https://www.runoob.com/w3cnote/php-phpmailer.html phpMailer 是一个非常强大的 ph p发送邮件类,可以设定发送邮件地址、回复地址...

    php-Mailer邮件类.zip

    本款邮件发送功能用了国外一个开源码的邮件类。 ...在这里我只指定为GB2312因为这样Outlook能正常显示邮件主题,我尝试过... 改为:require_once("phpmailer/class.phpmailer.php"); 否则的话会产生类的重定义.

    phpmailer.tar.gz

    用于PHP结合QQ邮箱发送邮件的类库(已修改好命名空间等 适用于ThinkPHP等框架 如有问题欢迎指正)

    PHPMailer-master.zip

    开源的PHP邮件类。2015.04最新版

    PHPMailer-6.2.0.zip

    在此版本中,PHPMailer获得了官方的PHP 8兼容性;早期版本可在PHP 8预发行版中使用,但测试套件却不能。@jrfnl完成了所需的大量工作(它还恢复了在较旧的PHP版本上运行的测试)–非常感谢! PHP 8.0兼容性 从PHP CS...

    PHPMailer-5.2.8.zip

    这是PHPMailer-5.2.8 .请大家下载,这个里面文件比较全,谢谢

    phpmailer Class

    phpmailer Class.phpmailer.php

    PHPMailer-6.6.4.zip php收发邮件类库

    PHP Mailer, php收发邮件email类库

    phpmailer.rar

    phpmailer 邮件发送的模板,只包含class.phpmailer.php、class.smtp.php两个文件

    PHPMailer_5.2.4.zip

    PHPMailer is a Full Featured Email Transfer Class for PHP

    PHPMailer_5.2.4.tgz

    PHPMailer_5.2.4.tgz

    有关phpmailer的详细介绍及使用方法

    http://phpmailer.sourceforge.net/第二,确认你的服务器系统已经支持socket ,通过phpinfo();查看是否支持sockets(socket 是属于PHP扩展部分),如果显现为“enabled”,那就是支持了。第三,把文件解压到你的web...

    phpmailer_new.zip

    支持php5.3.3以下版本的phpmailer,下载后直接引用就可使用!我也是在做项目时遇到后进行了修改,亲自测试可用。

    PHPMailer邮件类

    第三步:把文件解压到你的web服务器目录下,调用类就可以了,说明:首先包含 class.phpmailer.php,然后创建对象,设置参数,调用成员函数。具体请见下面的示例代码: &lt;?php require(...

    php邮件发送程序:PHPMailer-master.zip

    亲测好用。推荐下载。php发送邮件是经常用到的一个功能。这里采用PHPMailer-master专家化的程序,非常好地解决了这个问题。

    PHPMailer-6.3.0.zip

    启用与SMTP相同的机制:set SMTPDebug&gt; 0使mail()和sendmail传输与SMTP一样设置信封发件人,即使用From设置为,仅回退到sendmail_from php.ini设置 如果未设置从。 如果未显式设置发件人并且未配置php.ini,这...

Global site tag (gtag.js) - Google Analytics