功能在消息php mail()

时间:2014-06-28 14:12:02

标签: php wordpress email

我正在制作一个电子邮件功能,通过电子邮件发送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';
        }
?>

2 个答案:

答案 0 :(得分:0)

如评论中所述,您需要return来自您的功能。你不能这样做的方式。您可以使用输出缓冲或将其全部放在一个变量中:

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;
}

答案 1 :(得分:-1)

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);
?>