PHP邮件$ header break功能

时间:2013-11-25 06:59:51

标签: php email content-type oscommerce

我有一个函数可以发送多封电子邮件,具体取决于在oscommerce中购买的产品数量。它很好用,直到我添加函数的php mail()部分的标题。你可以看到,我的标题是:

    $headers .= 'MIME-Version: 1.0' . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $headers .= "From: info@email.com \r\n" .  
           "Reply-To: info@email.com \r\n" .  
           "X-Mailer: PHP/" . phpversion();

但是当我声明标题(我需要以html格式发送电子邮件)时,只会发送第一封电子邮件。我可以不多次发送标题吗?任何建议将不胜感激!

功能代码段:

foreach ($dstToProduct as $dsid => $productIndices) {
    $email = $newDropships[$dsid]['email'];
    $subject = "A new order has been placed";
    $headers .= 'MIME-Version: 1.0' . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $headers .= "From: info@email.com \r\n" .  
           "Reply-To: info@email.com \r\n" .  
           "X-Mailer: PHP/" . phpversion();




    // Build message text
    $date = date('m/d/Y');

    $text = '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';

    foreach ($productIndices as $productIndex) {

            $text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';

    }

        $text .= '</table>';


    if (!mail($email, $subject, $text, $headers)) {
        mail('info@email.com', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
    }

}
}

4 个答案:

答案 0 :(得分:2)

试试这个:

$subject = "A new order has been placed";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
$headers .= 'From: Info <info@email.com>' . "\r\n";
$headers .= 'Reply-To: Info <info@email.com>' . "\r\n";
$headers .=  "X-Mailer: PHP/".phpversion();   
$date = date('m/d/Y');
foreach ($dstToProduct as $dsid => $productIndices) 
{
 $email = $newDropships[$dsid]['email'];
 $text = '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';

  foreach ($productIndices as $productIndex) {

  $text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';

  }
 $text .= '</table>';
 if (!mail($email, $subject, $text, $headers)) {
    mail('info@email.com', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
}

}

答案 1 :(得分:1)

您可以尝试这样

希望它能为你效劳......

 <?php
    $headers=array(
        'MIME-Version: 1.0' . "\r\n",
        'From: info@email.com',
        'Content-Type:text/html',
        'Reply-To: info@email.com'
    );
    $subject = "A new order has been placed";

    foreach ($dstToProduct as $dsid => $productIndices) {
        $email = $newDropships[$dsid]['email'];


    // Build message text
        $date = date('m/d/Y');

        $text = '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';

        foreach ($productIndices as $productIndex) {

        $text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';

        }

        $text .= '</table>';   
        $body = $text;


    if (!mail($email,$subject,$body,implode("\r\n",$headers))) {
            mail('info@email.com', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
        }


    }

答案 2 :(得分:1)

如果您认为phpversion()会引发问题,请尝试以下方法:

    $phpV =  phpversion();
    $subject = "A new order has been placed";
    $headers .= 'MIME-Version: 1.0' . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
    $headers .= "From: info@email.com \r\n" .  
                "Reply-To: info@email.com \r\n" .  
                "X-Mailer: PHP/" .$phpV; 

通常,您不需要每次都这样做,非特定用户所依赖的变量 - 您可以声明一次。像这样的东西:

$subject = "A new order has been placed";
$headers .= 'MIME-Version: 1.0' . "\r\n";  
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

$headers .= "From: info@email.com \r\n" .  
       "Reply-To: info@email.com \r\n" .  
       "X-Mailer: PHP/" . phpversion();

$date = date('m/d/Y');

$text = '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';
foreach ($productIndices as $productIndex) {
    $text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' 
    . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">'
    . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' 
    . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">'
    . $products_array[$productIndex]["price"] . '</td></tr>';
}
$text .= '</table>';

foreach ($dstToProduct as $dsid => $productIndices) {
    $email = $newDropships[$dsid]['email'];
    if (!mail($email, $subject, $text, $headers)) {
        mail('info@email.com', 'Error sending product', 'The following order was not sent: ' . $order_id);
    }

答案 3 :(得分:0)

你的$标头正在与循环合并,试试这个。我相信它会起作用:

foreach ($dstToProduct as $dsid => $productIndices) {
    $email = $newDropships[$dsid]['email'];
    $subject = "A new order has been placed";

    $headers = ""; 

    $headers .= 'MIME-Version: 1.0' . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $headers .= "From: info@email.com \r\n" .  
           "Reply-To: info@email.com \r\n" .  
           "X-Mailer: PHP/" . phpversion();




    // Build message text
    $date = date('m/d/Y');

    $text = '<table cellpadding="3" style="margin-top: 20px;"><tr style="background-color: #6d7d59; color: #ffffff; font-weight: bold; font-size: 12px;"><td style="width: 240px; vertical-align:text-top;">Product Name</td><td style="width: 120px; vertical-align:text-top;">Model Number</td><td style="width: 80px; vertical-align:text-top;">Quantity</td><td style="width: 80px; vertical-align:text-top;">Price</td></tr>';

    foreach ($productIndices as $productIndex) {

            $text .= '<tr style="background-color: #f0f0f0; color: #513311; font-size: 12px;"><td style="vertical-align:text-top;">' . $products_array[$productIndex]["text"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["model"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["qty"] . '</td><td style="vertical-align:text-top;">' . $products_array[$productIndex]["price"] . '</td></tr>';

    }

        $text .= '</table>';


    if (!mail($email, $subject, $text, $headers)) {
        mail('info@email.com', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
    }

}
}