Search code examples
phpemailcontent-typeoscommerce

Php mail $headers break function


I have a function that sends multiple emails out depending on how many products are purchased within oscommerce. It works great until I add in the headers for the php mail() portion of the function. As you can see bellow, my headers read:

    $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();

But when I declare the headers (which I need in order to send the email as html), only the first email is sent out. Can I not send a header multiple times? Any suggestions would be appreciated!

Function snippet:

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

}
}

Solution

  • Try this :

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

    }