如何在wp_mail()中包含PHP变量

时间:2015-04-13 09:56:16

标签: php wordpress

我使用wp_mail()发送包含帖子中所有信息的电子邮件,包括其所有自定义字段。

我已保存帖子,当我点击电子邮件按钮时,会向我的功能发送一个AJAX请求,其中包含get_post_meta()wp_mail()。这应该获得所有已保存的post meta并通过电子邮件发送出去。

但没有快乐 - 这只是发送一封空邮件。我错过了什么吗?

function implement_ajax_operatoremail() {  

/*
*Construct the email
*/

    //The recipients    
    $title = get_the_title( $ID );
    $to = 'myemailaddress';
    $subject = 'Booking Confirmation : ' . $title; 
    $headers = 'From: clientname';

    //the message
    $message = 'here is the ' . $title . 'but it aint working' ;

    //send the email
    wp_mail( $to, $subject, $message, $headers, $attachments );

}

add_action('wp_ajax_operatoremail', 'implement_ajax_operatoremail');
add_action('wp_ajax_nopriv_operatoremail', 'implement_ajax_operatoremail');

3 个答案:

答案 0 :(得分:2)

$title = get_the_title( $ID );
$to = 'myemailaddress';
$subject = 'Booking Confirmation : ' . $title; 
$message  = "Dear ".$user->first_name.",\r\n\r\n";
$message .= "Your Booking is confirmed \r\n\r\n";
$message .= "Thanks!"."\r\n";
$headers = "From: Company Name <info@example.com>"."\r\n";                      
$mail = wp_mail( $to, $subject, $message, $headers );

请用此替换代码。

答案 1 :(得分:1)

这里可能缺少的是$附件?看起来你在函数中使用了变量但是没有分配任何东西吗?

还可以尝试分配类似$ title =&#39; test title&#39 ;;正如其中一条评论所说,你的$ ID来自哪里?

另一件事是标题,请更正您的$ headears以匹配: &#34;来自:&#34;示例用户&#34; &#34;

答案 2 :(得分:0)

虽然这并没有真正回答我的问题。错误是我试图从帖子ID中获取变量,帖子ID没有被发送,所以不足为奇。一旦我将帖子ID发送到我的功能,一切都很好。

谢谢大家。