消息php mail()中的函数

消息php mail()中的函数,php,wordpress,email,Php,Wordpress,Email,我正在制作一个电子邮件功能,其中Wordpress的帖子摘要通过电子邮件发送。 但是,消息正文保持空白,该函数似乎不起作用 有什么想法吗 function bodyout() { $ids = $_REQUEST['name']; $wpq = array('post__in'=> $ids, 'post_type' => 'posts', 'taxonomy'=>array('category1','category2'),'showposts' => -1); $pr

我正在制作一个电子邮件功能,其中Wordpress的帖子摘要通过电子邮件发送。 但是,消息正文保持空白,该函数似乎不起作用

有什么想法吗

function bodyout() {
$ids = $_REQUEST['name'];
$wpq = array('post__in'=> $ids, 'post_type' => 'posts', 'taxonomy'=>array('category1','category2'),'showposts' => -1);
$product_posts = new WP_Query ($wpq); ?>
<h2> Dear, </h2>
<p> This is the selection you made</p>
<table width="100%" cellspacing="0px" cellpadding="0px" style="width: 100%;" id="prod_list"><tbody><tr><th style="text-align: left;">name</th>
<th style="text-align: center;">var1</th>
<th style="text-align: center;">var2</th>
<th style="text-align: center;">var3</th>
<th style="text-align: center;">pdf</th>
</tr>
<?php if ($product_posts->have_posts()) : while ($product_posts->have_posts()) : $product_posts->the_post(); ?>
<tr class="data row_1?&gt;">
<td width="214"><?php the_title(); ?></td>
<td  style="text-align: center;"><?php the_field('var1'); ?>    </td>
<td  style="text-align: center;"><?php the_field('var2'); ?></td>
<td  style="text-align: center;"><?php the_field('var3'); ?></td>
<td style="text-align: center;"><?php if(get_field('pdf')) { ?><a onclick="return false" href="<?php the_field('pdf_download'); ?>" title="download">DOWNLOAD</a><?php }else {echo "no pdf"; } ?></td></tr>
<?php endwhile; endif; ?>
</tbody></table>
<?php
}
    //send email
        $from = $admin_email;
        $to = $your_email;
        $subject="Your selection";
        $body = bodyout();
        $headers = "From: $from \r\n";
        $headers .= "Reply-To: $email \r\n";
        mail($to, $subject, $body,$headers);
        if(mail)
        {
        echo 'Email sent';
        }
?>
函数bodyout(){
$ids=$\请求['name'];
$wpq=array('post\u in'=>$id','post\u type'=>'posts','taxonomy'=>array('category1','category2'),'showposts'=>-1);
$product\U posts=新的WP\U查询($wpq);?>
亲爱的,
这是您所做的选择

名称 var1 var2 var3 pdf
如注释中所述,您需要
从函数中返回一些内容。按您的操作方式,您无法返回。您可以使用输出缓冲或将其全部放入如下变量中:

function bodyout() {
    $ids = $_REQUEST['name'];
    $wpq = array('post__in'=> $ids, 'post_type' => 'posts', 'taxonomy'=>array('category1','category2'),'showposts' => -1);
    $product_posts = new WP_Query ($wpq); 

    $retval = ' <h2> Dear, </h2>
                <p> This is the selection you made</p>
                <table width="100%" cellspacing="0px" cellpadding="0px" style="width: 100%;" id="prod_list">
                    <tbody>
                        <tr>
                            <th style="text-align: left;">name</th>
                            <th style="text-align: center;">var1</th>
                            <th style="text-align: center;">var2</th>
                            <th style="text-align: center;">var3</th>
                            <th style="text-align: center;">pdf</th>
                        </tr>';

    if ($product_posts->have_posts()): 
        while ($product_posts->have_posts()): 
            $product_posts->the_post();
            $retval .= '    <tr class="data row_1?&gt;">
                                <td width="214">'.  the_title() . '</td>
                                <td style="text-align: center;">'. the_field('var1') . '</td>
                                <td style="text-align: center;">'. the_field('var2') . '</td>
                                <td style="text-align: center;">'. the_field('var3') . '</td>
                                <td style="text-align: center;">';

            if (get_field('pdf')) { 
                $retval .= '<a onclick="return false" href="'. the_field('pdf_download') .'" title="download">DOWNLOAD</a>';
            } else {
                $retval .= "no pdf"; 
            } 
            $retval .= '    </td>
                        </tr>';
        endwhile; 
    endif;
    $retval .= '    </tbody>
                </table>';
    return $retval;
}
函数bodyout(){
$ids=$\请求['name'];
$wpq=array('post\u in'=>$id','post\u type'=>'posts','taxonomy'=>array('category1','category2'),'showposts'=>-1);
$product\U posts=新的WP\U查询($wpq);
$retval='亲爱的,
这是您所做的选择

名称 var1 var2 var3 pdf '; 如果($product\u posts->have\u posts()): while($product\u posts->have\u posts()): $product_post->the_post(); $retval.=' “.标题() “._字段('var1')。” “._字段('var2')。” “._字段('var3')。” '; if(get_字段('pdf')){ $retval.=''; }否则{ $retval.=“无pdf”; } $retval.=' '; 结束时; endif; $retval.=' '; 返回$retval; }
HTML电子邮件需要比您提供的更多的标题。请注意下面的mime版本和内容类型。请尝试以下格式:

<?php
// multiple recipients
$to  = 'aidan@example.com' ; // recipient

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>


您需要
从该函数返回
某些内容。您这样做的方式有点不可靠,PHP标记插入时没有捕获输出。请参阅PHP手册中的内容,或使用常规变量方法,如引号字符串或
herdoc
。哦,@putvande声明,它必须
返回$something;
>bodyout()
函数。从邮件本身删除整个函数并在body元素中应用php if和while语句是否更容易?