如何在PHP

时间:2016-10-07 14:26:57

标签: php wordpress phpmailer

我正在使用WP邮件发送电子邮件。我有2个不同版本的hte电子邮件,我希望根据特定值发送。下面的代码是自我解释。

function implement_ajax_apartmentsearchemail() {



    if ( ($_POST['email']) ) {
        //get the correct page ID
        $to = ($_POST['email']);
        $name = ($_POST['name']);
        $subject = 'Our Recommendations.';
        $postidstring = ($_POST['postidstring']);   
        $reseller = ($_POST['reseller']);   

        if ($reseller == '') {
            $message = emailcontentscp($postidstring, $name);
        } else {
            $message = emailcontentreseller($postidstring, $reseller, $name);
        }            

        $subject = 'Our Recommendations';
        $headers = "Content-type: text/html;charset=UTF-8\n";
        $headers .= "X-Priority: 3\n";
        $headers .= "X-MSMail-Priority: Normal\n";
        $headers .= "X-Mailer: php\n";
        $headers .= "From: Serviced City Pads  <bookings@citypadsmail.com>\n";    
        wp_mail( $to, $subject, $message, $headers);   

        die();
    }


}   

add_action('wp_ajax_apartmentsearchemail', 'implement_ajax_apartmentsearchemail');
add_action('wp_ajax_nopriv_apartmentsearchemail', 'implement_ajax_apartmentsearchemail');

我正在尝试检查$ reseller变量是否为true,如果是,则运行经销商邮件模板,如果没有,则运行正常的SCP邮件模板。

我的条件适用于我在控制台中返回正确的电子邮件模板但是,当我转储$ message变量时,它什么都没有回来。

我的功能在不同的文件中如下布局。

function emailcontentscp($postidstring, $name) {
 //wp query to get some posts in a nice looking layout

 //loop start
     <div id="post-entry">some content</div>
 //loop end resetpostdata();

 //there is no return() here.  
}

1 个答案:

答案 0 :(得分:0)

为了获得函数的返回值并将其赋值给$message,函数需要return。而不是输出 HTML,将其存储在变量return中:

function emailcontentscp($postidstring, $name) {
    $template = '<div ...>';

    foreach (...) {
        $template .= '<li ...>';
    }

    return $template;
}