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






