tencent cloud

邮件推送

动态与公告
产品动态
关于新增独立 IP 增值服务的公告
产品简介
产品概述
产品功能
产品优势
应用场景
规格明细
购买指南
价格说明
快速入门
邮件配置
邮件发送
信誉度等级说明
控制台指南
邮件配置
邮件发送
数据统计
投递回应
API 文档
History
Introduction
API Category
调用方式
域名验证相关接口
邮件相关接口
模板相关接口
统计相关接口
发信地址相关接口
黑名单相关接口
查询发送状态相关接口
批量任务相关接口
自定义黑名单相关接口
Data Types
Error Codes
SMTP 文档
SMTP 发送邮件指南
SMTP 服务地址
Java 调用示例
Go 调用示例
PHP 调用示例
Python 调用示例
C# 调用示例
发送带附件的邮件
错误码
Webhook 文档
邮件通知事件
发件限制
附件类型
常见问题
入门相关问题
计费相关问题
发送限制相关问题
发送功能相关问题
域名相关问题
模板相关问题
身份验证和配置相关问题
独立 IP 相关问题
递送相关问题
API 使用相关问题
回调相关问题
垃圾箱相关问题
安全性相关问题
域名加速配置相关问题
注意事项
其他问题
联系我们

PHP 调用示例

PDF
聚焦模式
字号
最后更新时间: 2023-12-22 10:28:15

注意事项

1. 推荐用户使用 PHPMailer 包:
如果是新项目并且使用 composer 那么只需在 composer.json 加上 "phpmailer/phpmailer": "^6.5" ,或者执行 composer require phpmailer/phpmailer ,然后使用下面的代码即可。
如果是老项目且没有使用 composer 的,需手动引入 PHPMailer
2. 服务地址和端口请参见 SMTP 服务地址
以下是代码示例:
<?php

use PHPMailer\\PHPMailer\\PHPMailer;
use PHPMailer\\PHPMailer\\SMTP;
use PHPMailer\\PHPMailer\\Exception;
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->SMTPAuth = true; //Enable SMTP authentication
//$mail->AuthType = 'LOGIN';
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'sg-smtp.qcloudmail.com'; //Set the SMTP server to send through
$mail->Username = 'abc@qq.aa.com'; //SMTP username
$mail->Password = '123456'; //SMTP password

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/plain; charset=UTF-8';
$mail->Encoding = PHPMailer::ENCODING_BASE64;
//$mail->Encoding = '8bit';
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

//Recipients
$mail->setFrom('abc@qq.aa.com', 'fromName');
$mail->addAddress('test@test.com', 'toName'); //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('./tmp.txt'); //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}";
}


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈