edward144 / php-mailer-failover
一个PHPMailer类,允许配置多个冗余的SMTP服务。
v1.0.0
2024-04-13 15:27 UTC
Requires
- php: >=7.2
- html2text/html2text: ^4.3
- phpmailer/phpmailer: ^6.9
README
一个PHPMailer类,允许配置多个冗余的SMTP服务。每个服务将依次尝试,如果失败则使用下一个。如果所有服务都失败,邮件将被记录到本地文件以供调试,并且邮件不会丢失。
安装
此包现在在Packagist上,因此您可以直接运行 composer require edward144/php-mailer-failover。
创建一个composer.json文件或修改现有的文件。添加此GitHub仓库并要求最新版本。
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Edward144/PHPMailer-Failover.git"
}
],
"require": {
"edward144/php-mailer-failover": "^1.0"
},
"minimum-stability": "stable"
}
运行 composer install
配置
必须定义一些常量以使类运行。这些常量的示例可以在 tests/config.example.php 中找到。这些应该存储在安全的位置,因为需要SMTP凭据。
-
MAILER_FAILOVER_SMTP- 要使用的SMTP服务数组,将按顺序尝试。'service_name' => [ 'host' => 'smtp.service.com', 'port' => 587, 'username' => 'username', 'password' => 'apikey', 'encryption' => 'tls' //If omitted then PHPMailer::ENCRYPTION_SMTPS will be used ], ... -
MAILER_FAILOVER_DEBUG_LEVEL- PHPMailer定义的整数调试级别,默认为2- 0 - 调试关闭
- 1 - 客户端到服务器消息
- 2 - 如1加服务器到客户端消息
- 3 - 如2加连接状态
- 4 - 所有消息
-
MAILER_FAILOVER_DEBUG_LOCATION- 存储调试日志和失败邮件的绝对路径,包括尾随斜杠 -
MAILER_FAILOVER_FROM- 默认发送邮件的电子邮件地址 -
MAILER_FAILOVER_FROM_FRIENDLY- 默认的友好名称,邮件将从该名称发送
用法
包含类,要求 vendor/autoload.php 并包含您定义的常量。
然后您可以添加和利用各种方法来配置您的邮件。
Use Edward144\PHPMailer-Failover\Mailer
require __DIR__ . '/path/to/vendor/autoload.php';
include __DIR__ . '/path/to/config.php';
$mail = new Mailer();
$mail->addTo('john.smith@example.com'); //Add a single address without a friendly name
$mail->addTo('john.smith@example.com', 'John Smith'); //Add a single address with a friendly name
$mail->addTo([
'john.smith@example.com',
'jane.doe@example.com'
]); //Add an array of addresses without friendly names
$mail->addTo([
'John Smith' => 'john.smith@example.com',
'Jane Doe' => 'jane.doe@example.com'
]); //Add an array of addresses with friendly names
$mail->addTo([
'John Smith' => 'john.smith@example.com',
'jane.doe@example.com'
]); //A mix of emails with and without friendlies can be supplied
$mail->addCc($address, $friendly); //Add a CC address, this can be used the same ways as addTo
$mail->addBcc($address, $friendly); //Add a BCC address, this can also be used the same ways as addTo
$mail->subject('This is my subject);
$mail->body('<p>This is my HTML message!</p>, true); //HTML body, a plaintext version is generated by default, pass a second false parameter to omit this
$mail->altBody('This is my plaintext message!'); //Plaintext body, this should be set before the HTML body, unless the automatic plaintext generation has been disabled.
$mail->from('another.from@example.com', 'Optional Friendly Name'); //Override the default from address and friendly name
$mail->addAttachment('absolute/path/to/attachment.txt'); //Add a single attachment
$mail->addAttachment('absolute/path/to/attachment.txt', 'custom-attachment.txt'); //Add a single attachment with a custom filename
$mail->addAttachment([
'path/to/attachment-1.txt',
'path/to/attachment-2.pdf'
]); //An array of attachments without custom filenames
$mail->addAttachment([
'text-attachment.txt' => 'path/to/attachment-1.txt',
'pdf-attachment.pdf' => 'path/to/attachment-2.pdf'
]); //An array of attachments with custom filenames
$mail->addAttachment([
'text-attachment.txt' => 'path/to/attachment-1.txt',
'path/to/attachment-2.pdf'
]); //A mix of attachments with and without custom filenames can be supplied
$mail->send(); //Send the mail
//true will be returned if a service succeeded
//false if all services failed but the message was logged locally
//An error message will be returned in other cases (an incorrect parameter was supplied, the log file failed to open)