zoukankan      html  css  js  c++  java
  • 使用phpmailer发送邮件(以QQ邮箱为例)

    <?php
      include("class/class.phpmailer.php"); //下载phpmailer并include两个文件
      include("class/class.smtp.php");

      $mail = new PHPMailer();     //得到一个PHPMailer实例
      $mail->CharSet = "utf-8"; //设置采用utf-8中文编码(内容不会乱码)
      $mail->IsSMTP();                    //设置采用SMTP方式发送邮件
      $mail->Host = "smtp.qq.com";    //设置邮件服务器的地址(若为163邮箱,则是smtp.163.com)
      $mail->Port = 25;                           //设置邮件服务器的端口,默认为25
      $mail->From     = "发件人"; //设置发件人的邮箱地址
      $mail->FromName = "收件人姓名";           //设置发件人的姓名(可随意)
      $mail->SMTPAuth = true;                   //设置SMTP是否需要密码验证,true表示需要
      
      $mail->Username="发件人";    (后面有解释说明为何设置为发件人)
      
      $mail->Password = "发件人邮箱密码";
      $mail->Subject = "你好啊";    //主题
      $mail->AltBody = "text/html";                                // optional, comment out and test
      $mail->Body = "你的邮件的内容";      //内容  
      $mail->IsHTML(true);     
      //$mail->WordWrap = 50;                                 //设置每行的字符数
      $mail->AddReplyTo("回复地址","from");     //设置回复的收件人的地址(from可随意)
      $mail->AddAddress("收件人","to");     //设置收件的地址(to可随意)
           
      echo $mail->Send(); 

     ?>

    若出现不能连接或者无法通过验证,则

    1、在class.phpmailer.php中

    363-365行,将smtp小写改成大写

     public function IsSMTP() {
        $this->Mailer = 'SMTP'; 
      }

    2、572-579行,将case "smtp"小写改成大写

      switch($this->Mailer) {
            case 'sendmail':
              return $this->SendmailSend($header, $body);
            case 'SMTP':
              return $this->SmtpSend($header, $body);
            default:
              return $this->MailSend($header, $body);
          } 

    3、出现错误无法通过验证时,是因为在811-817中

     $connection = true;
              if ($this->SMTPAuth) {
                if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
                  throw new phpmailerException($this->Lang('authenticate'));
                }
              }
            } 

    验证时调用的是Username和Password,所以设置时将Username与发件人的from设置相同,同时设置为发件人邮箱即可解决通过。

  • 相关阅读:
    两数之和等于目标值
    Atitit.软件仪表盘(0)--软件的子系统体系说明
    获取 exception 对象的字符串形式(接口服务返回给调用者)
    SELECT LAST_INSERT_ID() 的使用和注意事项
    @RequestParam 注解的使用
    Eclipse中修改SVN用户名和密码方法
    淘淘商城上传图片按钮不显示的问题
    spring集成webSocket实现服务端向前端推送消息
    Mybatis中jdbcType和javaType对应关系
    MySQL数据库中tinyint字段值为1,读取出来为true的问题
  • 原文地址:https://www.cnblogs.com/jdk123456/p/3578617.html
Copyright © 2011-2022 走看看