当前位置:首页>php>一个轻量级、高效的PHP SMTP开源邮件发送类,支持SSL/TLS加密,符合RFC标准,可用于各大邮箱服务商

一个轻量级、高效的PHP SMTP开源邮件发送类,支持SSL/TLS加密,符合RFC标准,可用于各大邮箱服务商

  • 2026-09-05 17:32:56
一个轻量级、高效的PHP SMTP开源邮件发送类,支持SSL/TLS加密,符合RFC标准,可用于各大邮箱服务商

一个轻量级、高效的PHP SMTP邮件发送类,支持SSL/TLS加密,符合RFC标准,可用于各大邮箱服务商。

演示网址

https://tool.bitefu.net/smtpmailer/

功能特点

  • 支持SSL/TLS加密连接
  • 支持HTML格式邮件
  • 支持多收件人发送
  • 支持添加附件
  • 符合RFC5322、RFC2047、RFC822邮件标准
  • 兼容主流邮箱服务商(QQ邮箱、163邮箱、Gmail、阿里企业邮等)
  • 提供API接口,方便远程调用
  • 简洁易用的API接口

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参数说明

参数名
类型
必填
说明
host
string
是
SMTP服务器地址
port
int
否
SMTP服务器端口,默认465
username
string
是
邮箱账号
password
string
是
邮箱密码或授权码
from
string/array
是
发件人邮箱或包含名称和邮箱的数组
to
string/array
是
收件人邮箱,多个收件人用逗号分隔
subject
string
是
邮件主题
message
string
是
邮件内容
is_html
int
否
是否为HTML格式,1为是,0为否,默认0

API返回格式

{
"code": 200,  // 200成功,201失败
"msg": "邮件发送成功"// 成功或失败的消息
}

注意事项

  1. 使用QQ邮箱、163邮箱等需要在邮箱设置中开启SMTP服务并获取授权码
  2. 发送邮件时,建议使用带发件人名称的格式,以符合RFC标准
  3. 如果遇到发送失败,可以开启调试模式查看详细错误信息
  4. 发送到QQ邮箱时,必须确保From头格式正确,否则会被拒收

许可证

MIT License

最新文章

随机文章