PHPMailer——发送邮件函数封装

源代码 

<?php
/**
* 邮件发送
* @param $to 接收人
* @param string $subject 邮件标题
* @param string $content 邮件内容(html模板渲染后的内容)
* @throws Exception
* @throws phpmailerException
*/
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
function send_email($to="1234567890@qq.com",$subject='',$content='<h1>Hello World</h1>'){
    // 引入PHPMailer的核心文件
    require_once(dirname(__FILE__)."/PHPMailer/src/PHPMailer.php");
    require_once(dirname(__FILE__)."/PHPMailer/src/SMTP.php");
    // 实例化PHPMailer核心类
    $mail = new PHPMailer();
    // 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 1;
    //调试输出格式
    //$mail->Debugoutput = 'html';
    // 使用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登录的账号 QQ邮箱即可
    $mail->Username = '1234567890@qq.com';
    // smtp登录的密码 使用生成的授权码
    $mail->Password = 'abcdefghijklmn';
    // 设置发件人邮箱地址 同登录账号
    $mail->From = '1234567890@qq.com';
    // 邮件正文是否为html编码 注意此处是一个方法
    $mail->isHTML(true);
    // 设置收件人邮箱地址
    if(is_array($to)){foreach($to as $v){$mail->addAddress($v);}}else{$mail->addAddress($to);}
    $mail->addAddress('1234567890@qq.com');
    // 添加多个收件人 则多次调用方法即可
    //$mail->addAddress('1234567890@163.com');
    // 添加该邮件的主题
    $mail->Subject = $subject;
    // 添加邮件正文
    $email=$content;
    //$mail->Body = '<h1>Hello World</h1>';
    $mail->Body = $email;
    // 为该邮件添加附件
    //$mail->addAttachment('./example.pdf');
    //附加信息,可以省略
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; 
    // 发送邮件 返回状态
    return $status = $mail->send();
}
send_email("1234567890@qq.com");
?>

资源下载

PHPMailer Github: https://github.com/PHPMailer/PHPMailer/

参考文章

https://blog.csdn.net/weixin_43272781/article/details/102932918

https://blog.csdn.net/zhsp1029/article/details/2206483

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starzkg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值