使用phpmailer發送郵件(以QQ郵箱爲例)

<?php
  include("class/class.phpmailer.php"); //下載phpmailer並include兩個文件
  include("class/class.smtp.php");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();  html

 ?>服務器

 

若出現不能鏈接或者沒法經過驗證,則this

一、在class.phpmailer.php中編碼

363-365行,將smtp小寫改爲大寫spa

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

二、572-579行,將case "smtp"小寫改爲大寫utf-8

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

三、出現錯誤沒法經過驗證時,是由於在811-817中io

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

驗證時調用的是Username和Password,因此設置時將Username與發件人的from設置相同,同時設置爲發件人郵箱便可解決經過。

相關文章
相關標籤/搜索