ionvv/wp-mail-helper

用于通过 WordPress 发送自定义邮件的 PHP 包。

1.0.0 2020-04-14 15:32 UTC

This package is auto-updated.

Last update: 2024-09-15 02:01:15 UTC


README

WP Mail Helper 是一个帮助 WordPress 开发者发送自定义邮件的包。

入门

最低要求和依赖

WP Mail Helper 需要

  • PHP 7+
  • WordPress - 最新版本
  • 已安装 Composer

安装

通过 composer 安装

composer require ionvv/wp-mail-helper

使用

基本用法

$email_controller = new \WpMailHelper\Email();
$email_controller->sendEmail(
    'recipient@domain.tld', // $to
    'sender@domain.tld',    // $from
    'Email subject',        // $subject
    'Email content'         // $message
);

高级用法

$email_controller->sendEmail(
    'recipient@domain.tld', // $to
    'sender@domain.tld',    // $from
    'Email subject',        // $subject
    'Email content',        // $message
    'template',             // $message_type
    [],                     // $headers
    'john.doe@domain.tld',  // $cc
    'john.doe@domain.tld',  // $bcc
    [WP_CONTENT_DIR . '/uploads/dummy-file.zip']
);

使用帖子作为邮件内容

$email_controller = new \WpMailHelper\Email();
$email_controller->sendEmail(
    'recipient@domain.tld', // $to
    'sender@domain.tld',    // $from
    'Email subject',        // $subject
    '$post->ID',            // $message
    'post'                  // $message_type
);

使用自定义模板作为邮件内容

$email_controller = new \WpMailHelper\Email();
$email_controller->sendEmail(
    'recipient@domain.tld',                               // $to
    'sender@domain.tld',                                  // $from
    'Email subject',                                      // $subject
    get_stylesheet_directory() . '/emails/template.html', // $message
    'template'                                            // $message_type
);

在邮件内容中使用自定义变量

有时您可能需要在邮件中程序化地替换一些变量。您可以在邮件主题和内容中使用 '短码'。查看以下示例,了解如何使用它们

$email_subject = 'Welcome to our website, {{FIRST_NAME}}';
$email_content = 'Dear {{FIRST_NAME}}, thanks for joining our website. You have been registered with the following email address: {{EMAIL_ADDRESS}}';

$email_controller = new \WpMailHelper\Email();
$email_controller->setVars(
    [
        'FIRST_NAME' => 'Ion',
        'EMAIL_ADDRESS' => 'ion@domain.tld'
    ],
    [
        'FIRST_NAME' => 'Ion'
    ]
);
$email_controller->sendEmail(
    'ion@domain.tld',       // $to
    'sender@domain.tld',    // $from
    $email_subject,         // $subject
    $email_content          // $message
);

用户将收到以下主题的邮件

Welcome to our website, Ion

和内容

Dear Ion, thanks for joining our website. You have been registered with the following email address: ion@domain.tld

变更日志

1.0.0

  • 初始发布

许可证

WP Mail Helper 代码在 MIT 许可下授权。