首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从WordPress向站点用户发送包含自定义字段的HTML

从WordPress向站点用户发送包含自定义字段的HTML
EN

WordPress Development用户
提问于 2018-06-22 17:12:07
回答 1查看 694关注 0票数 0

我希望发送电子邮件给我的WordPress网站/系统的用户。我想要一些电子邮件,上面写着:

你还有X个学分来完成你的任务。

你的截止日期是Z。

X、Y、Z将是从我在系统中创建的自定义字段和/或sql查询中提取的信息。

我正在寻找一个插件或一些关于如何通过代码手动操作的技巧。我将把这些按需寄出,以及每月等。

谢谢您的指导,如果您需要更多的信息,请告诉我。我试着尽量保持简单。也许wp-mail是最好的选择?

EN

回答 1

WordPress Development用户

发布于 2018-06-22 20:18:32

The Basics

您可以使用WordPress中的任何地方使用wp_mail()发送电子邮件:

代码语言:javascript
复制
wp_mail( $to, $subject, $message, $headers, $attachments )

$to可以是多个收件人的字符串或数组。任何符合RFC 2822的地址都可以工作,所以你可以包括一个真名和一个电子邮件地址。

$subject & $message是字符串,$message可以是字符串。

可以选择,$headers可以是字符串,也可以是附加电子邮件头的数组,而$attachments可以是要附加的文件数组。

HTML电子邮件

如果您对内容使用HTML,那么在发送电子邮件之前,可以向过滤器添加一个简单的函数,以便正确处理mime类型的设置:

代码语言:javascript
复制
function wpse306737_set_html_content_type($content_type){
    return 'text/html';
}

add_filter( 'wp_mail_content_type', 'wpse306737_set_html_content_type' );

发送电子邮件后,最好再次删除此筛选器,这样您就不会影响以后在WP代码运行中发送的任何电子邮件:

代码语言:javascript
复制
remove_filter( 'wp_mail_content_type', 'wpse306737_set_html_content_type' );

From: Address

默认情况下,电子邮件来自wordpress@sitename。您可以使用筛选器来更改以下内容:

代码语言:javascript
复制
add_filter( 'wp_mail_from', 'wpse306737_mail_from' );
add_filter( 'wp_mail_from_name', 'wpse306737_mail_from_name' );

function wpse306737_mail_from() {
    return 'johnsmith@example.com';
}

function wpse306737_mail_from_name() {
    return 'John Smith';
}

如果您愿意,也可以设置标头,

代码语言:javascript
复制
$headers[] = 'From: John Smith ';

但WP过滤器似乎更符合可湿性粉剂的方式。

其他标头

代码语言:javascript
复制
$headers[] = 'Cc: The Shop ';
$headers[] = 'Bcc: Jane Jones ';

构建消息

消息只是一个字符串,因此连接纯文本、HTML和任何您想要的WP数据。如果我们使用post元字段并在循环中,那么:

代码语言:javascript
复制
$message = '';
$custom_fields = get_post_custom();
$message .= 'You have ' . $custom_fields['X'] . ' credits remaining to fulfill ' . $custom_fields['Y'] . '.';
$message .= 'Your deadline is ' . $custom_fields['Z'] . '.';

--关于检索元数据

的一点注记

当您在循环中时,get_post_custom()返回当前post的所有自定义字段。您可以使用post ID调用它,以获取特定post的元数据:例如,get_post_custom(3)

get_post_custom()将返回所有后置元的数组,其中一些可能是序列化的,有些可能是数组,因此您可能会发现更好的方法是使用对get_post_meta()的多次调用,这些调用将允许您通过metakey获取数据,并指定它是单个值还是多个值:get_post_meta( 3, 'total_points', true )将返回一个元字段的值,其中键total_points与ID 3附加在post上。

您没有说明要使用的数据是如何存储的,但是有一些用于检索用户元、术语元和注释元的等效函数。get_user_meta()get_term_meta() & get_comment_meta()都和get_post_meta()一样工作。

事实上,如果您查看源代码,只要有一个数据库表,WP就会很高兴地返回任何元数据,因此您甚至可以使用内置函数,如果您的表有合适的名称,就可以为您自己的数据库表使用WordPress元缓存。我所提到的get_{type}_meta()函数都调用了get_metadata()。调用get_metadata( 'thing', 3 )将检查名为thingmeta的WP db表,并将使用它检索元数据。get_metadata()也有一个过滤器get_{$meta_type}_metadata来短路这个数据库查找,允许你替换任何你喜欢的数据源。

发送邮件

把所有这些放在一起,你就会得到:

代码语言:javascript
复制
add_filter( 'wp_mail_from', 'wpse306737_mail_from' );
add_filter( 'wp_mail_from_name', 'wpse306737_mail_from_name' );

function wpse306737_mail_from() {
    return 'johnsmith@example.com';
}

function wpse306737_mail_from_name() {
    return 'John Smith';
}

$headers = array(); // let's be safe
$headers[] = 'Cc: The Shop ';
$headers[] = 'Bcc: Jane Jones ';

$message = '';
$custom_fields = get_post_custom();
$message .= 'You have ' . $custom_fields['X'] . ' credits remaining to fulfill ' . $custom_fields['Y'] . '.';
$message .= 'Your deadline is ' . $custom_fields['Z'] . '.';

$to = 'Site User ';
$subject = 'Meet your deadline!';

function wpse306737_set_html_content_type($content_type){
    return 'text/html';
}

add_filter( 'wp_mail_content_type', 'set_html_content_type' );

wp_mail( $to, $subject, $message, $headers );

remove_filter( 'wp_mail_content_type', 'set_html_content_type' );

发送大量电子邮件

您可能希望通过有效的SMTP帐户或事务性电子邮件服务发送电子邮件,从而维护服务器的声誉并提高可交付性,以便您可以利用SPF、DKIM和DMARC。

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/306737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档