下面的代碼演示了 PHPMailer 的使用方法,注意 PHPMailer 實例的配置過程。
<?php
require_once 'PHPMailer/class.phpmailer.php';
require_once 'PHPMailer/class.smtp.php';
class QQMailer
{
public static $HOST = 'smtp.qq.com'; // QQ 郵箱的服務器地址
public static $PORT = 465; // smtp 服務器的遠程服務器端口號
public static $SMTP = 'ssl'; // 使用 ssl 加密方式登錄
public static $CHARSET = 'UTF-8'; // 設置發送的郵件的編碼
private static $USERNAME = '123456789@qq.com'; // 授權登錄的賬號
private static $PASSWORD = '****************'; // 授權登錄的密碼
private static $NICKNAME = 'yuanjin'; // 發件人的昵稱
public function __construct($debug = false)
{
$this->mailer = new PHPMailer();
$this->mailer->SMTPDebug = $debug ? 1 : 0;
$this->mailer->isSMTP(); // 使用 SMTP 方式發送郵件
}
public function getMailer()
{
return $this->mailer;
}
private function loadConfig()
{
$this->mailer->SMTPAuth = true; // 開啟 SMTP 認證
$this->mailer->Host = self::$HOST; // SMTP 服務器地址
$this->mailer->Port = self::$PORT; // 遠程服務器端口號
$this->mailer->SMTPSecure = self::$SMTP; // 登錄認證方式
/* Account Settings */
$this->mailer->Username = self::$USERNAME; // SMTP 登錄賬號
$this->mailer->Password = self::$PASSWORD; // SMTP 登錄密碼
$this->mailer->From = self::$USERNAME; // 發件人郵箱地址
$this->mailer->FromName = self::$NICKNAME; // 發件人昵稱(任意內容)
$this->mailer->isHTML(true); // 郵件正文是否為 HTML
$this->mailer->CharSet = self::$CHARSET; // 發送的郵件的編碼
}
public function addFile($path)
{
$this->mailer->addAttachment($path);
}
public function send($email, $title, $content)
{
$this->loadConfig();
$this->mailer->addAddress($email); // 收件人郵箱
$this->mailer->Subject = $title; // 郵件主題
$this->mailer->Body = $content; // 郵件信息
return (bool)$this->mailer->send(); // 發送郵件
}
}
require_once 'QQMailer.php';
$mailer = new QQMailer(true);
$title = '愿得一人心,白首不相離。';
$content = <<< EOF
<p align="center">
皚如山上雪,皎若云間月。<br>
聞君有兩意,故來相決絕。<br>
今日斗酒會,明旦溝水頭。<br>
躞蹀御溝上,溝水東西流。<br>
凄凄復凄凄,嫁娶不須啼。<br>
愿得一人心,白首不相離。<br>
竹竿何裊裊,魚尾何簁簁!<br>
男兒重意氣,何用錢刀為!</p>
EOF;
$mailer->send('123456789@qq.com', $title, $content);
遠近互聯前端小秦整理發布,希望能對學習技術的你有所幫助
遠近互聯專業提供網站建設、APP開發、網站優化、外貿網站SEO、微信運營的品牌整合營銷服務讓客戶通過網絡品牌建立與網絡傳播提高業績。






