基于PHP实现邮件实时通知功能
作者:PHP开源社区 发布时间:2024-06-07 15:46:09
标签:PHP,邮件,实时,通知
一、安装环境
PHPMailer 需要 PHP 的 sockets 扩展支持
另外登录 QQ 邮箱 SMTP 服务器则必须通过 SSL 加密的, PHP 还得包含 openssl 的支持
二、下载
地址: https://github.com/PHPMailer/PHPMailer/
三、 邮箱设置
所有的主流邮箱都支持 SMTP 协议,但并非所有邮箱都默认开启
您可以在邮箱的设置里面手动开启
第三方服务在提供了账号和密码之后就可以登录 SMTP 服务器
通过它来控制邮件的中转方式
SMTP 服务器认证密码,需要妥善保管
四、php发送邮件
<?php
// 引入PHPMailer的核心文件
require_once("PHPMailer/class.phpmailer.php");
require_once("PHPMailer/class.smtp.php");
// 实例化PHPMailer核心类
$mail = new PHPMailer();
// 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
$mail->SMTPDebug = 1;
// 使用smtp鉴权方式发送邮件
$mail->isSMTP();
// smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
// 链接qq域名邮箱的服务器地址
$mail->Host = 'smtp.qq.com';
// 设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
// 设置ssl连接smtp服务器的远程服务器端口号
$mail->Port = 465;
// 设置发送的邮件的编码
$mail->CharSet = 'UTF-8';
// 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名
$mail->FromName = '发件人昵称';
// smtp登录的账号 任意邮箱即可
$mail->Username = 'xxxxxxx@163.com';
// smtp登录的密码 使用生成的授权码
$mail->Password = '**********';
// 设置发件人邮箱地址 同登录账号
$mail->From = 'xxxxxxx@qq.com';
// 邮件正文是否为html编码 注意此处是一个方法
$mail->isHTML(true);
// 设置收件人邮箱地址
$mail->addAddress('xxxxxxxxx@qq.com');
// 添加多个收件人 则多次调用方法即可
$mail->addAddress('xxxxxxxxx@163.com');
// 添加该邮件的主题
$mail->Subject = '邮件主题';
// 添加邮件正文
$mail->Body = '<h1>Hello, i am autofelix</h1>';
// 为该邮件添加附件
$mail->addAttachment('./附件.pdf');
// 发送邮件 返回状态
$status = $mail->send();
五、php框架中使用
先使用composer进行安装:composer require phpmailer/phpmailer ^6.5
使用
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class mail
{
public function send()
{
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.example.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'user@example.com'; //SMTP username
$mail->Password = 'secret'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); //Add a recipient
$mail->addAddress('ellen@example.com'); //Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
}
来源:https://mp.weixin.qq.com/s/_FhMugHOQLeQg3LhszIwFw


猜你喜欢
- Python转json时出现中文乱码设置报文头# -*- coding:gbk -*- 连接数据库设置编码mysql = MyS
- 这可能是一个非常简单的问题,但是今天花一点点时间把这个简单的问题在说清晰一点,相信大家对CSS的学习和认识会很有帮助,强化一些概念的东西,对
- 简介看到了一个能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息的模块–psutil模块。这次利用psutil
- 一个asp读取数据库中数据到数组的类,仅供参考!DbPath = "test.mdb"’数据库位置&
- 1、更新包管理 apt-get install update.2、安装 pip3 :apt-get install python3-pip3
- 以下针对Ubuntu系统,Windows系统没有测试过。Ubuntu中默认就安装有Python 2.x和Python 3.x,默认情况下py
- 本文记录了mysql 8.0.22 安装配置图文教程,供大家参考,具体内容如下一、安装(1)、官网下载(2)、安装(前提是之前没安装过mys
- 1、简介APScheduler是一个 Python 定时任务框架,使用起来十分方便。提供了基于日期、固定时间间隔以及 crontab 类型的
- Heroku是一个很棒的平台,它有很多的控件,并且搭建环境相对来说也比较容易。本指南中,我将一步一步指导你在Heroku平台上部署一个简单地
- 前言所谓模糊查询就是不需要用户完整的输入或者说全部输入信息即可提供查询服务,也就是用户可以在边输入的同时边看到提示的信息(其实是查询出来匹配
- Python关于mySQL的连接插件众多,Bottle下也有人专门开发的插件:bottle-mysql具体使用方法见官方,总共感觉其用法限制
- 系列目录:1. 服务器XMLHTTP(Server XMLHTTP in ASP)基础2. 
- 一、引出问题假如有这两张表,它们中的课程可能价格不一样、周期不一样、等等...不一样...,现在有一张价格策略表,怎么就用一张表报保存它们之
- 问题描述当前使用的PyCharm社区版版本号2022.1.2,配置镜像源时,没有manage repositories解决方案:镜像源:清华
- 1.按姓氏笔画排序:Select * From TableName Order By CustomerName Collate Chines
- 写爬虫有一个绕不过去的问题就是验证码,现在验证码分类大概有4种:图像类滑动类点击类语音类今天先来看看图像类,这类验证码大多是数字、字母的组合
- 之前捣鼓树莓派时,要求做一个番茄钟,但最后就只是搞成一个与树莓派没啥关系的py程序,虽然简陋,但就此记录一下自学的成果。程序实现番茄工作法:
- 1。建立数据库表 表名为online 设如下字段 id '用来记录每一个访问都的session.sessionid name
- 系统环境:VC6 + Python-2.5.41、下载Python-2.5.4源码。2、解压,打开D:\Python-2.5.4\PC\VC
- 我们经常见到很多网站留言系统的显示访客的IP地址都是隐藏了一部分,以达到隐蔽访客真实地理位置的功能。如:111.222.333.*,当然在系