一个轻量级、高效的PHP SMTP邮件发送类,支持SSL/TLS加密,符合RFC标准,可用于各大邮箱服务商。
演示网址
https://tool.bitefu.net/smtpmailer/
功能特点
- 符合RFC5322、RFC2047、RFC822邮件标准
- 兼容主流邮箱服务商(QQ邮箱、163邮箱、Gmail、阿里企业邮等)
API请求示例
$url='https://tool.bitefu.net/smtpmailer/';
$param=array(
'host'=>'smtp.163.com',
//'port'=>456,
'username'=>'发送账号',
'password'=>'发送账号的密码或者授权码',
'from'=>'发送邮箱',
'to'=>'接收邮箱',
'subject'=>'邮件标题',
'message'=>'邮件内容<b>hello word</b>',
'is_html'=>1,
);
$res = curlget($url,$param,'POST');
$res = json_decode($res,1);
var_export($res);
/**
* CURL发送HTTP请求
* @param string $url 请求URL
* @param array $params 请求参数
* @param string $method 请求方法GET/POST
* @return array $data 响应数据
*/
functioncurlget($url, $params='', $method = 'GET'){
$opts = array(
CURLOPT_TIMEOUT => 10,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
);
switch (strtoupper($method)) {
case'GET':
$opts[CURLOPT_URL] = $params ? $url.'?'.http_build_query($params) : $url;
break;
case'POST':
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = http_build_query($params);
break;
}
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
安装方法
直接下载SMTPMailer.class.php文件到您的项目中,然后引入即可使用:
include("SMTPMailer.class.php");
本地使用方法
基本用法
<?php
include("SMTPMailer.class.php");
try {
// 创建SMTPMailer实例
$mailer = new SMTPMailer(
'smtp.example.com', // SMTP服务器地址
465, // SMTP服务器端口
'your-email@example.com', // 用户名/邮箱
'your-password', // 密码
SMTPMailer::ENCRYPT_SSL // 加密类型:ENCRYPT_NONE, ENCRYPT_SSL, ENCRYPT_TLS
);
// 设置为HTML格式邮件(可选)
$mailer->is_html = true;
// 发送邮件
$result = $mailer->send(
['name' => '发件人名称', 'email' => 'your-email@example.com'], // 发件人
'recipient@example.com', // 收件人,可以是字符串或数组
'邮件主题', // 邮件主题
'<p>这是一封<strong>HTML格式</strong>的邮件</p>'// 邮件内容
);
if ($result) {
echo"邮件发送成功!";
} else {
echo"邮件发送失败!";
}
} catch (Exception $e) {
echo"发送失败: " . $e->getMessage();
}
高级用法
发送给多个收件人
$recipients = [
'user1@example.com',
'user2@example.com',
'user3@example.com'
];
$mailer->send(
['name' => '发件人名称', 'email' => 'your-email@example.com'],
$recipients,
'邮件主题',
'邮件内容'
);
添加附件
$mailer->send(
['name' => '发件人名称', 'email' => 'your-email@example.com'],
'recipient@example.com',
'邮件主题',
'邮件内容',
[], // 自定义头信息(可选)
['/path/to/file1.pdf', '/path/to/file2.zip'] // 附件路径数组
);
开启调试模式
$mailer->setDebug(true); // 开启调试模式,错误信息会记录到error_log
常见邮箱SMTP配置
QQ邮箱
$mailer = new SMTPMailer(
'smtp.qq.com',
465,
'your-qq-email@qq.com',
'your-authorization-code', // 授权码,不是QQ密码
SMTPMailer::ENCRYPT_SSL
);
163邮箱
$mailer = new SMTPMailer(
'smtp.163.com',
465,
'your-email@163.com',
'your-authorization-code', // 授权码,不是登录密码
SMTPMailer::ENCRYPT_SSL
);
Gmail
$mailer = new SMTPMailer(
'smtp.gmail.com',
587,
'your-email@gmail.com',
'your-password',
SMTPMailer::ENCRYPT_TLS
);
阿里企业邮箱
$mailer = new SMTPMailer(
'smtp.qiye.aliyun.com',
465,
'your-email@your-domain.com',
'your-password',
SMTPMailer::ENCRYPT_SSL
);
API参数说明
API返回格式
{
"code": 200, // 200成功,201失败
"msg": "邮件发送成功"// 成功或失败的消息
}
注意事项
- 使用QQ邮箱、163邮箱等需要在邮箱设置中开启SMTP服务并获取授权码
- 发送邮件时,建议使用带发件人名称的格式,以符合RFC标准
- 如果遇到发送失败,可以开启调试模式查看详细错误信息
- 发送到QQ邮箱时,必须确保From头格式正确,否则会被拒收
许可证
MIT License