skip to Main Content

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' . "rn";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";  

    $headers .= "From: [email protected] rn" .  
           "Reply-To: [email protected] rn" .  
           "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' . "rn";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";  

    $headers .= "From: [email protected] rn" .  
           "Reply-To: [email protected] rn" .  
           "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('[email protected]', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
    }

}
}

4

Answers


  1. You can try like this

    Hope it should work for you…

     <?php
        $headers=array(
            'MIME-Version: 1.0' . "rn",
            'From: [email protected]',
            'Content-Type:text/html',
            'Reply-To: [email protected]'
        );
        $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("rn",$headers))) {
                mail('[email protected]', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
            }
    
    
        }
    
    Login or Signup to reply.
  2. your $headers is merging with the loop, try this. I am sure it will work:

    foreach ($dstToProduct as $dsid => $productIndices) {
        $email = $newDropships[$dsid]['email'];
        $subject = "A new order has been placed";
    
        $headers = ""; 
    
        $headers .= 'MIME-Version: 1.0' . "rn";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";  
    
        $headers .= "From: [email protected] rn" .  
               "Reply-To: [email protected] rn" .  
               "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('[email protected]', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
        }
    
    }
    }
    
    Login or Signup to reply.
  3. If are you thinking that the phpversion() rise the problem, try something like this:

        $phpV =  phpversion();
        $subject = "A new order has been placed";
        $headers .= 'MIME-Version: 1.0' . "rn";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";  
        $headers .= "From: [email protected] rn" .  
                    "Reply-To: [email protected] rn" .  
                    "X-Mailer: PHP/" .$phpV; 
    

    And generally, you don’t need to do this every time, the variables that is not specific user depends – you can declare once. something like this:

    $subject = "A new order has been placed";
    $headers .= 'MIME-Version: 1.0' . "rn";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";  
    
    $headers .= "From: [email protected] rn" .  
           "Reply-To: [email protected] rn" .  
           "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('[email protected]', 'Error sending product', 'The following order was not sent: ' . $order_id);
        }
    
    Login or Signup to reply.
  4. Try this :

    $subject = "A new order has been placed";
    $headers = 'MIME-Version: 1.0' . "rn";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";  
    $headers .= 'From: Info <[email protected]>' . "rn";
    $headers .= 'Reply-To: Info <[email protected]>' . "rn";
    $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('[email protected]', 'Error sending product', 'The following order was not sent:&nbsp;' . $order_id);
    }
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search