当前位置: 首页 > news >正文

PHP发送邮件

一、安装PHPMailer

        进入项目目录下,执行:composer require phpmailer/phpmailer

二、使用

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;require 'vendor/autoload.php';$mail = new PHPMailer(true); 
header("Content-Security-Policy: script-src 'self' 'unsafe-inline' *.baidustatic.com; style-src 'self' 'unsafe-inline';");
header('Content-Type: application/json');// 邮件配置
$mailConfig = ['smtp_host' => 'smtp.163.com','smtp_port' => 465, // 或587'smtp_user' => 'xxx@163.com','smtp_pass' => 'xxx','from'      => 'xxx@163.com','to'        => 'xxx@163.com'
];// 发送邮件try {$mail->SMTPDebug = 2;$mail->isSMTP();$mail->Host = $mailConfig['smtp_host'];$mail->SMTPAuth = true;$mail->Username = $mailConfig['smtp_user'];$mail->Password = $mailConfig['smtp_pass'];$mail->SMTPSecure = 'ssl'; // 改为 ssl$mail->Port = $mailConfig['smtp_port']; // 465$mail->setFrom($mailConfig['from'], 'xxx');$mail->addAddress($mailConfig['to']);$mail->CharSet = 'UTF-8';$mail->Encoding = 'base64';$mail->ContentType = 'text/html; charset=utf-8';$mail->isHTML(true);$mail->Subject = 'xxx';$mail->Body = "<h3>xxx</h3><p>xxx:xxx</p><p>xxx:{$username}</p><p>xxx:{$userPhone}</p><p>xxx:" . date('Y-m-d H:i:s') . "</p>";$mail->send();} catch (Exception $e) {throw new Exception("邮件发送失败: " . $e->getMessage());}exit;

http://www.xdnf.cn/news/13573.html

相关文章:

  • 【刷题Day19】HTTP的各个版本(浅)
  • 记录git stash误删除恢复方法
  • 探索 JavaScript 中的 Promise 高级用法与实战
  • 什么是MMOE?
  • 坐标上海,20~40K的面试强度
  • Android Studio 常见报错
  • 计算机网络——应用层
  • 济南通过首个备案生活服务大模型,打造行业新标杆
  • 【人工智能】Prompt攻击与防范策略总结
  • 2025年03月中国电子学会青少年软件编程(Python)等级考试试卷(三级)答案 + 解析
  • ELF2开发板的ubuntu系统的ax200 wifi配网
  • Vue 3.0 Composition API 与 Vue 2.x Options API 的区别
  • 8.Rust+Axum 数据库集成实战:从 ORM 选型到用户管理系统开发
  • 2025MathorcupC题 音频文件的高质量读写与去噪优化 保姆级教程讲解|模型讲解
  • Docker中镜像、容器、仓库三者之间的关系
  • 第 8 期:条件生成 DDPM:让模型“听话”地画图!
  • Hadoop的三大结构及各自的作用?
  • TDengine Restful 接口API
  • excel解析图片pdf附件不怕
  • ESP8266简单介绍
  • 2025年山东燃气瓶装送气工考试真题练习
  • MCP协议量子加密实践:基于QKD的下一代安全通信(2025深度解析版)
  • 从数字化到智能化,百度 SRE 数智免疫系统的演进和实践
  • MCP(Model Context Protocol 模型上下文协议)科普
  • vue 中formatter
  • 2025-04-18 李沐深度学习3 —— 线性代数
  • yarn的三大组件及各自作用
  • easyexcel使用模板填充excel坑点总结
  • Kotlin协程Semaphore withPermit约束并发任务数量
  • chili3d调试笔记3 加入c++ 大模型对话方法 cmakelists精读