PHP:在一封电子邮件中发送超过文章

时间:2014-11-09 21:29:28

标签: php email

我有一个名为'an_posts'的表和一个名为'post_title'的表中的列。当我选择超过'post_title'时,我需要向用户发送电子邮件。但是下面的代码只发送一个'post_title'。为什么会发生这种情况以及我必须在代码中添加的内容,以便电子邮件发送将从数据库中检索两个或更多数据?

if($_GET["action"] == 'sendpostnewsletter')
    {
        global $post;
        $post_id = $_POST["post_id"];
        $emailtosender = $_POST["emailtosender"];
        $subjecttitle = $_POST["subjecttitle"];
        $sql = "select * from an_posts where post_type = 'post' and post_status = 'publish' and ID in ($post_id)";
        $formssql = mysql_query($sql);
            while ( $row = mysql_fetch_array($formssql) ) 
            {
                $message = '
                <div align="center"><h3>'.$row['post_title'].'</h3></div>
                <br />
                <div style="text-align:justify; font-size:16px; font-family:Verdana, Geneva, sans-serif; font-weight:bold">
                '.$row['post_content'].'
                </div>';
            }

        $message .= "<hr />";
        $headers = "From: Newsletter <list@domainname.com>" . "\n" ;
        $headers .= 'MIME-Version: 1.0' . "\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\n";
        wp_mail( $emailtosender , $subjecttitle , $message, $headers );
        $result='<div id="message" class="updated fade"><p><strong><h5>done</h5></strong></p></div>';
        echo $result;
    }

1 个答案:

答案 0 :(得分:0)

您正在覆盖每个循环的消息内容。取而代之的是:

//initialize outside the loop
$message='';
while ( $row = mysql_fetch_array($formssql) ) 
{
    //then append - notice the dot .=
    $message .= '
    ...