广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >基于PHP实现邮件实时通知功能
  • 687
分享到

基于PHP实现邮件实时通知功能

PHP邮件实时通知PHP邮件通知 2022-11-13 07:11:38 687人浏览 八月长安
摘要

目录一、安装环境二、下载 三、 邮箱设置四、PHP发送邮件五、php框架中使用一、安装环境 PHPMailer 需要 PHP 的 Sockets 扩展支持 另外登录 QQ

一、安装环境

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}";
        }
    }
}

以上就是基于PHP实现邮件实时通知功能的详细内容,更多关于PHP邮件实时通知的资料请关注编程网其它相关文章!

--结束END--

本文标题: 基于PHP实现邮件实时通知功能

本文链接: https://www.lsjlt.com/news/147715.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作