1. 程式人生 > >php 郵件傳送

php 郵件傳送

安裝phpmailer

composer require phpmailer/phpmailer

傳送qq郵件

$nickname = "碳視界";
$mailer = new QQMailer(false,$nickname);

// 新增附件
// $mailer->addFile('20130VL.jpg');


// 郵件標題
$title = "";
// 郵件內容
$html = "";

//接收者郵箱
$email = "[email protected]";

// 傳送QQ郵件
$res = $mailer->send($email, $title, $html);

1.qq傳送郵件類

<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/9/5
 * Time: 15:48
 */

namespace PHPMailer\PHPMailer;


use app\index\model\Config;

class QQMailer
{
    public static $HOST = 'smtp.qq.com'; // QQ 郵箱的伺服器地址
    public static $PORT = 465; // smtp 伺服器的遠端伺服器埠號
    public static $SMTP = 'ssl'; // 使用 ssl 加密方式登入
    public static $CHARSET = 'UTF-8'; // 設定傳送的郵件的編碼

    protected static $USERNAME = '
[email protected]
'; // 授權登入的賬號 protected static $PASSWORD = ''; // 授權登入的密碼 protected static $NICKNAME = ''; // 發件人的暱稱 /** * QQMailer constructor. * @param bool $debug [除錯模式] */ public function __construct($debug = false,$username='[email protected]',$password= 'kplqertxoyejebcd',$nickname='精品簽名設計') { self::$PASSWORD = $password; self::$USERNAME =""; self::$NICKNAME = ""; $this->mailer = new PHPMailer(); $this->mailer->SMTPDebug = $debug ? 1 : 0; $this->mailer->isSMTP(); // 使用 SMTP 方式傳送郵件 } /** * @return PHPMailer */ public function getMailer() { return $this->mailer; } private function loadConfig() { /* Server Settings */ $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; // 發件人暱稱(任意內容) /* Content Setting */ $this->mailer->isHTML(true); // 郵件正文是否為 HTML $this->mailer->CharSet = self::$CHARSET; // 傳送的郵件的編碼 } /** * Add attachment * @param $path [附件路徑] */ public function addFile($path) { $this->mailer->addAttachment($path); } /** * Send Email * @param $email [收件人] * @param $title [主題] * @param $content [正文] * @return bool [傳送狀態] */ 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(); // 傳送郵件 } }