anthonybudd/wp_mail

此包的最新版本(v2.0)没有可用的许可证信息。

v2.0 2018-03-10 17:56 UTC

This package is auto-updated.

Last update: 2024-09-28 22:31:23 UTC


README

WP_Mail 是WordPress中最受欢迎、最简单、功能最强大的动态电子邮件类。该类提供了简单的方法来附加文件、自定义头部和大量的辅助函数。该类仅使用 WordPress 函数 wp_mail() 发送电子邮件,这意味着您现有的所有 SMTP 设置将无需额外的配置或设置即可继续工作。

$email = WP_Mail::init()
    ->to('john.doe@gmail.com')
    ->subject('WP_Mail is great!')
    ->template(get_template_directory() .'/emails/demo.php', [
        'name' => 'Anthony Budd',
        'location' => 'London',
        'skills' => [
           'PHP',
           'AWS',
        ] 
    ])
    ->send();

email.html

<h3>You have a new contact from enquirey!</h3><br>

<p>
  <strong>Name:</strong><?= $name ?>
</p>

<p>
  <strong>email:</strong>
  <a href="mailto:<?= $email ?>"><?= $email ?></a>
</p>

<p>
  <strong>Skills:</strong><br>
  <ul>
    <?php foreach($skills as $skill): ?>
      <li>
        <?= $skill ?>
      </li>
    <?php endforeach;?>
  </ul>
</p>

安装

使用 composer 需要 WP_Mail

$ composer require anthonybudd/WP_Mail

或者

下载 WP_Mail 类并在您的 functions.php 文件顶部 require 它。

    require 'src/WP_Mail.php';

方法

to(), cc(), bcc()

所有这些函数都允许您设置电子邮件的接收者数组或字符串,如下例所示。

    $email = WP_Mail::init()
        ->to([
            'johndoe@gmail.com'
            'mikesmith@gmail.com'
        ])
        ->cc('JackTaylor@gmail.com')

subject()

要设置主题字段,请使用 subject 函数。第一个参数将是电子邮件的主题。

    $email = WP_Mail::init()
        ->subject('This this the subject')

from()

要设置“发送者”头部,有一个有用的辅助函数。

    $email = WP_Mail::init()
        ->from('John Doe <john.doe@ideea.co.uk>')

attach()

类似于 to、cc 和 bcc 方法,attach 方法可以接受字符串或字符串数组。这些字符串必须是绝对文件路径,如果文件不存在,则该方法将引发错误。

    $email = WP_Mail::init()
        ->attach(ABSPATH .'wp-content/uploads/2017/06/file.pdf')

template($templatePath, $variables = [])

template 方法用于设置 HTML 电子邮件模板的路径。第二个参数是一个关联数组,其键将对应于您的 HTML 电子邮件的变量。变量是可选的,对于没有变量的模板也不需要。

    $email = WP_Mail::init()
        ->template(get_template_directory() .'/email.html', [
           'name' => 'Anthony Budd',
           'job'  => 'Developer',
        ])

templateHeader($templatePath, $variables = [])

templateFooter($templatePath, $variables = [])

不言自明

如果您要发送大量电子邮件,beforeTemplate() 和 afterTemplate() 将允许您将模板化的 HTML 添加到您的电子邮件中。

    $email = (new WP_Mail)
        ->beforeTemplate(get_template_directory() .'/email-header.html')
		->afterTemplate(get_template_directory() .'/email-footer.html')
        ->template(get_template_directory() .'/email.html', [
           'name' => 'Anthony Budd',
           'job'  => 'Developer',
        ])

headers()

此方法允许您为电子邮件设置额外的头部。这可以是一个头部数组的字符串或单个字符串头部。

    $email = WP_Mail::init()
        ->headers("From: John Doe <john.doe@ideea.co.uk>")
    $email = WP_Mail::init()
        ->headers([
            "From: John Doe <john.doe@ideea.co.uk>",
            "X-Mailer: PHP/". phpversion(),
            "Reply-To: webmaster@ideea.co.uk",
            "Content-type: text/html; charset=iso-8859-1",
        ])

render()

此方法由 send() 方法调用,结果直接传递给 wp_mail 函数的 $message 参数。这可以用于测试或向管理员显示电子邮件的外观。

当您发送电子邮件时,render() 方法会使用简单的正则表达式来查找和替换使用 mustache 语法表示的变量。最后,该方法使用 WordPress 内置的 wp_mail() 函数发送电子邮件。

    $email = (new WP_Mail)
        ->send()