我目前正在寻找一种使用 WordPress 网站发送电子邮件通知的方法。例如,当用户访问页面 A 时,会在后台发送电子邮件通知。
我在 WordPress 环境中的 Web 开发方面没有太多经验,所以有人可以在这里给我指点吗?我应该从哪里开始?
谢谢。
编辑:
我已经尝试了 mail() 和 wp_mail() 函数,但它们似乎都不适合我。当我访问该页面时,没有发送任何电子邮件。我还检查了该页面的模板,这只是默认模板。也许我正在编辑错误的文件?
编辑2:
我猜主机提供商可能还没有启用邮件功能。
这是一段非常基本的 php 代码,用于发送 html 电子邮件。
<?php
if(is_page(123))
{
$fromName = 'Auto email notification system';
$subject = 'Confirmed';
/* Mail Address */
$toAddr = 'me@domain.com';
$bccAddr = 'bccperson@domain.com';
$fromAddr = 'no-reply@domain.com';
/* End Mail Address */
/* Mail Body */
$msg = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
my messages about confirmation...
</body>
</html>
';
$msg = wordwrap($msg, 70);
/* End Mail Body */
/* Mail Headers Setup */
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "From: ".$fromName." <".$fromAddr.">";
$headers[] = "Bcc: <".$bccAddr.">";
/* End Mail Headers Setup */
mail($toAddr, $subject, $msg, implode("\r\n", $headers));
}
?>
我将上面的代码放在 header.php 文件的底部,它对我有用。
谢谢大家的建议和帮助。
嘿@woodykiddy 为页面创建一个模板并将此代码放入页面中。每次加载页面时,此条件都会返回 true。
// Example using the array form of $headers
// assumes $to, $subject, $message have already been defined earlier...
$headers[] = 'From: Me Myself <me@example.net>';
$headers[] = 'Cc: Aravind B Codex <abc@wordpress.org>';
$headers[] = 'Cc: iluvwp@wordpress.org'; // note you can just use a simple email address
<?php
if(is_page()) :
wp_mail( $to, $subject, $message, $headers );
endif;
?>
http://codex.wordpress.org/Function_Reference/wp_mail
希望这会帮助你。
创建模板很容易。创建一个新页面 my-template.php 将此代码放在顶部。
<?php
/*
Template Name: My New Template
*/
?>
但这取决于你的主题。我为你编辑了二十个。它会给你一个创建模板的想法。
<?php
/*
Template Name: My New Template
*/
get_header();
$headers[] = 'From: Me Myself <me@example.net>';
$headers[] = 'Cc: Aravind B Codex <abc@wordpress.org>';
$headers[] = 'Cc: iluvwp@wordpress.org'; // note you can just use a simple email address
?>
<?php
if(is_page()) :
wp_mail( $to, $subject, $message, $headers );
endif;
?>
<div id="container">
<div id="content" role="main">
<?php
/* Run the loop to output the page.
* If you want to overload this in a child theme then include a file
* called loop-page.php and that will be used instead.
*/
get_template_part( 'loop', 'page' );
?>
</div><!-- #content -->
</div><!-- #container -->
将电子邮件代码放入其中。将其保存在模板目录中。
转到管理面板并添加/编辑页面。在页面的右侧有一个选项(模板)。您的模板将在下拉菜单中可见。选择模板并保存页面就是这样。