您现在的位置是:群英 > 开发技术 > 编程语言
PHP用PHPMailer实现邮件发送的过程是什么
Admin发表于 2022-09-20 18:06:14513 次浏览
关于“PHP用PHPMailer实现邮件发送的过程是什么”的知识点有一些人不是很理解,对此小编给大家总结了相关内容,文中的内容简单清晰,易于学习与理解,具有一定的参考学习价值,希望能对大家有所帮助,接下来就跟随小编一起学习一下“PHP用PHPMailer实现邮件发送的过程是什么”吧。
导语

〝 古人学问遗无力,少壮功夫老始成 〞

随着企业化的管理越来越规范,各种项目管理系统中,都需要加入到邮件实时通知功能,所以在项目中如何整合发邮件功能,其实是很重要的一点。如果这篇文章能给你带来一点帮助,希望给飞兔小哥哥一键三连,表示支持,谢谢各位小伙伴们。

一、安装环境

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用PHPMailer实现邮件发送的过程是什么”的介绍了,感谢各位的阅读,希望文本对大家有所帮助。如果想要了解更多知识,欢迎关注群英网络,小编每天都会为大家更新不同的知识。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: php
相关信息推荐
2022-01-22 17:57:20 
摘要:SharedPreferences可以存储什么数据?SharedPreferences是Android平台上的一个轻量级的存储量,用来存储少量数据时比较方便,简单。但是,SharedPreferences只能保存简单类型的数据。接下来我们详细的了解一下。
2022-09-15 17:48:15 
摘要:本篇文章给大家带来了关于java的相关知识,其中主要整理了接口与抽象类的相关问题,当需要从几个类中派生出一个类,继承他们所有的属性和方法,JAVA没有多重继承需要用接口实现,下面一起来看一下,希望对大家有帮助。
2022-05-06 17:55:38 
摘要:这篇文章主要为大家详细介绍了C语言实现简单版三子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部