itinerisltd/wp-phpmailer

WP PHPMailer 提供了一种干净简单的方式来配置 WordPress 内置的 PHPMailer 库,让您可以快速开始通过您选择的本地或云服务发送邮件。

安装次数: 30,013

依赖项: 0

建议者: 0

安全: 0

星标: 61

关注者: 12

分支: 6

公开问题: 6

类型:wordpress-plugin

0.3.0 2022-05-02 17:16 UTC

This package is auto-updated.

Last update: 2024-09-20 00:41:16 UTC


README

CircleCI Packagist Version WordPress Plugin Rating PHP from Packagist WordPress Plugin: Tested WP Version Packagist Downloads WordPress Plugin Downloads GitHub License Hire Itineris Twitter Follow @itineris_ltd Twitter Follow @TangRufus

WP PHPMailer 提供了一种干净简单的方式来配置 WordPress 内置的 PHPMailer 库,让您可以快速通过您选择的本地或云服务发送邮件。

目标

尽管 WordPress 内置了 PHPMailer 库,允许您通过您选择的本地或云服务发送邮件,但不同的云服务需要不同的配置。更糟糕的是,大多数服务提供多种设置方法。例如:哪个 SendGrid SMTP 端口提供最高级别的安全性,255872525465

WP PHPMailer 使用 WordPress 内置的 PHPMailer 库

  • 因此,您可以将更新内置库的责任从插件作者转移到 WordPress 核心团队和贡献者
    • 截至编写时,官方 SendGrid 插件的 供应商文件夹 已经有 2.5 年没有更新了

WP PHPMailer 相信 约定优于配置,我们为每个服务选择最佳配置

  • 这样您就不必浪费时间阅读文档
  • 这样您就不必确定要使用哪个端口和协议
  • 这样您就不会错过任何安全配置,例如:SMTPAuthSMTPSecure
    • 官方 Mailgun 插件 不同,没有“使用安全 SMTP”选项,因为没有人应该使用不安全的选项
  • 因此,您只需要提供最基本的信息
    • 以 SendGrid 为例,只需要 SendGrid API 密钥(仅带有“邮件发送”权限)即可

WP PHPMailer 认为,插件应该 "只做一件事,做好这件事"

用法

wp-config.php 中选择一个驱动程序并定义其所需的常量。

Mailhog

define('WP_PHPMAILER_DRIVER', 'mailhog');

Mailtrap

define('WP_PHPMAILER_DRIVER', 'mailtrap');

define('MAILTRAP_USERNAME', 'your-mailtrap-username');
define('MAILTRAP_PASSWORD', 'your-mailtrap-password');

SendGrid

define('WP_PHPMAILER_DRIVER', 'sendgrid');

define('SENDGRID_API_KEY', 'your-sendgrid-api-key');

// Optional. Useful if you have email authentication configurated.
define('SENDGRID_FROM_ADDRESS', 'you@example.test');
define('SENDGRID_FROM_NAME', 'John Doe');
define('SENDGRID_FROM_AUTO', true);

自定义驱动

步骤 1. 定义您的驱动

class MyCustomDriver implements DriverInterface
{
    public static function makeConfig(ConstantRepository $constantRepo): ConfigInterface
    {
        $config = new Config();

        $config->set('auth', true);
        $config->set('host', 'smtp.custom.test');
        $config->set('port', 587);
        $config->set('protocol', 'tls');

        $config->set(
            'username',
            $constantRepo->getRequired('MY_CUSTOM_USERNAME')
        );

        $config->set(
            'password',
            $constantRepo->getRequired('MY_CUSTOM_PASSWORD')
        );

        $config->set(
            'fromAddress',
            $constantRepo->get('MY_CUSTOM_FROM_ADDRESS')
        );
        $config->set(
            'fromName',
            $constantRepo->get('MY_CUSTOM_FROM_NAME')
        );
        $config->set(
            'fromAuto',
            $constantRepo->get('MY_CUSTOM_FROM_AUTO')
        );

        return $config;
    }
}

步骤 2. 注册您的驱动

add_filter('wp_phpmailer_drivers', function (array $drivers): array {
    $drivers['my-custom-driver'] = MyCustomDriver::class;

    return $drivers;
});

步骤 3. 定义常量

// wp-config.php

define('WP_PHPMAILER_DRIVER', 'my-custom-driver');

define('MY_CUSTOM_USERNAME', 'xxx');
define('MY_CUSTOM_PASSWORD', 'xxx');

// Optional.
define('MY_CUSTOM_FROM_ADDRESS', 'xxx');
define('MY_CUSTOM_FROM_NAME', 'xxx');
define('MY_CUSTOM_FROM_AUTO', true);

过滤器

wp_phpmailer_driver

$driver = (string) apply_filters('wp_phpmailer_driver', string $wpPhpmailerDriver))

过滤 WP_PHPMAILER_DRIVER 常量。

参数

  • $wpPhpmailerDriver
    • (字符串) WP_PHPMAILER_DRIVER 常量的值

wp_phpmailer_drivers

$drivers = (array) apply_filters('wp_phpmailer_drivers', array $drivers)

过滤可用的驱动程序数组。

参数

  • $drivers
    • (数组) 可用驱动程序数组

示例

add_filter('wp_phpmailer_drivers', function (array $drivers): array {
   $drivers['my-custom-driver'] = MyCustomDriver::class;

   return $drivers;
});

wp_phpmailer_config_mappings

$mappings = (array) apply_filters('wp_phpmailer_config_mappings', array $mapings)

过滤白名单中的 PHPMailer 配置(属性名称)数组。'fromAddress', 'fromName', 'fromAuto' 是特殊的。不要在映射中添加它们!

参数

  • $mapings
    • (数组) 白名单中的 PHPMailer 配置(属性名称)

示例

add_filter('wp_phpmailer_config_mappings', function (array $mappings): array {
    $mappings['xxx'] = 'yyy';

    return $mappings;
});

// The above filter results in:
add_action( 'phpmailer_init', function (PHPMailer $phpmailer) {
    // $this->config comes from `DriverInterface::makeConfig`.
    $phpmailer->xxx = $this->config->get('yyy');
});

最低要求

  • PHP v7.2
  • WordPress v5.5

安装

Composer(推荐)

composer require itinerisltd/wp-phpmailer

wordpress.org(WP CLI)

wp plugin install wp-phpmailer

wordpress.org

https://wordpresstheme.cn/plugins/wp-phpmailer 下载。然后,像通常一样安装 wp-phpmailer.zip

从源码构建(不推荐)

# Make sure you use the same PHP version as remote servers.
# Building inside docker images is recommended.
php -v

# Checkout source code
git clone https://github.com/ItinerisLtd/wp-phpmailer.git
cd wp-phpmailer
git checkout <the-tag-or-the-branch-or-the-commit>

# Build the zip file
composer release:build

然后,像通常一样安装 release/wp-phpmailer.zip

常见错误

NotFoundException - 找不到驱动 'xxx',可接受值有:aaa, bbb, ccc

原因:未找到驱动程序或未定义。

故障排除

  • 确保 PHP 常量 WP_PHPMAILER_DRIVER 是正确的
  • 确保过滤器 wp_phpmailer_driver 正确工作

常见问题解答

设置页面在哪里?

没有设置页面。

所有配置都通过 PHP 常量WordPress 过滤器 完成。

您会添加设置页面吗?

否。

我们看到了与用户输入相关的无数 漏洞,这些漏洞 相关。邮件设置不经常更改,应由开发者配置。因此,WP PHPMailer 决定使用 PHP 常量而不是在 WordPress 数据库中存储选项。

但是,如果您必须这样做,您可以使用 过滤器 来覆盖此行为。

捆绑的 PHPMailer 版本是什么?

此插件重用了 WordPress 核心捆绑的 PHPMailer 类

因此,您必须保持 WordPress 核心最新,以接收安全补丁。

必须使用 SMTP 吗?

否。

虽然您可以创建自己的非 SMTP 驱动程序,但所有默认驱动程序都使用 SMTP。欢迎拉取请求。

您会添加对旧 PHP 版本的支持吗?

永不!此插件仅在 积极支持的 PHP 版本 上工作。

不要在 已停止支持仅安全修复 的 PHP 版本上使用它。

看起来很棒。我可以在哪里找到更多类似的东西?

  • 有关 Itineris 博客的文章
  • 有关 Itineris 的更多项目
  • 有关 Itineris 和 TangRufus 在 wp.org 上的更多插件
  • 关注Twitter上的@itineris_ltd@TangRufus
  • 雇佣Itineris来构建您的下一个出色的网站

我可以在哪里给出 ⭐⭐⭐⭐⭐ 评价?

谢谢!很高兴你喜欢它。让我的老板知道有人在使用这个项目很重要。请考虑

  • wordpress.org上留下5星评价
  • 发表一条提及@itineris_ltd@TangRufus的积极推文
  • ⭐ 星标这个Github仓库
  • 👀 观察Github仓库
  • 写博客文章
  • 提交pull requests
  • 雇佣Itineris

测试

composer test
composer phpstan:analyse
composer style:check

不接受没有测试的pull requests!

反馈

请提供反馈!我们希望尽可能让这个库在尽可能多的项目中变得有用。请提交一个issue并指出你喜欢和不喜欢的地方,或者复制项目并提出建议。没有issue太小。

安全

如果你发现任何安全相关的问题,请通过电子邮件dev@itineris.co.uk联系,而不是使用issue追踪器。

致谢

WP PHPMailer是由Itineris Limited创建的Itineris Limited项目,由Tang Rufus创建。

特别感谢Brandon,他的WP SMTP启发了这个项目。

贡献者名单的完整列表可以在这里找到。

许可证

WP PHPMailerMIT许可证下发布。