Jump to content

PHP Sendmail not sending HTML


Go to solution Solved by davidannis,

Recommended Posts

Hi Guys,

 

I am trying to create a script which send a customer an invoice when they place an order. I have written the following code and this works when sent to an email address which is opened using gmail or the iPhone email client, but Microsoft Outlook screams a great big no at me, and just shows me the html in plain text. Any thoughts anyone?

 

I have included the code below:

 

<?php
include("includes/conn.php");
$id=1;
$random_hash = md5(date('r', time()));
$subject="Red Van Tool Sales - Invoice: ";
$headers = "From: sales@redvantoolsales.co.uk" . "\r\n";
$headers  .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
/*$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;

$user=isset($_COOKIE['user']) ? $_COOKIE['user'] : null;
$sql="select * from user where user_email='$user'";
$sql_res=mysqli_query($conn, $sql) or die(mysqli_error($conn));
while($array=mysqli_fetch_array($sql_res))
{
$uid=$array['user_id'];
}
*/
$sql="select * from orders 
inner join payment_options on orders.payment_options_id=payment_options.payment_options_id
inner join postage_options on orders.delivery_id=postage_options.postage_options_id
inner join user_address on orders.delivery_address_id=user_address.user_address_id
inner join user on orders.user_id=user.user_id
where orders.orders_id='$id'";
$sql_res=mysqli_query($conn, $sql) or die(mysqli_error($conn));
$count=mysqli_num_rows($sql_res);
if($count==0)
{
//header("Location:noauth.php");
echo "fail";
} else {
	while($array=mysqli_fetch_array($sql_res))
	{
	$order_id=$array['orders_id'];
	$subject .= $order_id;
	$order_date=date("d/m/Y", $array['orders_date']);
	$orders_total=number_format($array['orders_total'],2);
	$order_subtotal=number_format($orders_total/1.2,2);
	$vat=number_format($orders_total-$order_subtotal,2);
	$shipping_total=number_format($array['shipping_total'],2);
	$payment_fee=number_format($array['payment_fee'],2);
	$payment_options_name=$array['payment_options_name'];
	$postage_options_name=$array['postage_option_name'];
	$user_forename=$array['user_forename'];
	$user_surname=$array['user_surname'];
	$address_address=$array['address_address'];
	$address_city=$array['address_city'];
	$address_postcode=$array['address_postcode'];
	$shipping_time=$array['postage_option_shipping_time'];
	$shipping_time = $shipping_time*86400;
	$expected_arrival=$array['orders_date']+$shipping_time;
	$expected_arrival=date("d/m/Y", $expected_arrival);
	$emailto = $array['user_email'];
	}

}
ob_start(); //Turn on output buffering
?>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="http://www.redvantoolsales.co.uk/invoice.css" media="print">
<link rel="stylesheet" type="text/css" href="http://www.redvantoolsales.co.uk/invoice.css" media="screen">
</head>

<body>
<div id="cont">
	<div id="container">
    	<div id="topbar">
            <img class="logo" src="http://www.redvantoolsales.co.uk/images/logo.png">
            <div id="companyaddress">Red Van Tools<br>Address<br>City<br>Postcode<br>VAT: 000000000<br>sales@redvantoolsales.co.uk<br>01633 000000<br>www.redvantoolsales.co.uk</div>
        </div>
        
        <div id="nextbar">
            <div id="customeraddress"><?php echo $user_forename . " " . $user_surname; ?><br><?php echo $address_address; ?><br><?php echo $address_city; ?><br><?php echo $address_postcode; ?></div>
            <div id="invoicedetail">INVOICE <?php echo $order_id; ?><br><?php echo $order_date; ?></div>
            
        </div>  
        
        <div id="orderdetailbar">
        	<table>
            <tr><th width="333">Payment Method</th><th width="333">Order Date</th><th width="333">Estimated Delivery Date</th></tr>
            <tr><td><?php echo $payment_options_name; ?></td><td><?php echo $order_date; ?></td><td><?php echo $expected_arrival; ?></td></tr>
            </table>
        </div> 
        
        <div id="orderitembar">
        	<table>
            <tr><th width="120">Quantity</th><th width="500">Details</th><th width="120">Unit Price (£)</th><th width="120">VAT</th><th width="120">Net Subtotal (£)</th></tr>
             
			<?php
			$sql="select * from order_item
			inner join products on order_item.product_id=products.product_id
			inner join price on products.product_id=price.product_id
			where orders_id='$id'";
			$sql_res=mysqli_query($conn, $sql) or die(mysqli_error($conn));
			while($array=mysqli_fetch_array($sql_res))
			{
			$total = number_format($array['product_price']*$array['product_qty'],2);
			echo "<tr><td>" . $array['product_qty'] . "</td><td>" . $array['product_name'] . "</td><td>" . number_format($array['product_price'],2) . "</td><td>20%</td><td align=\"right\">" . $total . "</td></tr>";
			}
            ?>
            </table>
        </div> 
        
        <div id="totals">
        	<table>
            <tr><td>Net Total</td><td><?php echo $order_subtotal; ?></td></tr>
            <tr><td>VAT</td><td><?php echo $vat; ?></td></tr>
            <tr><td><b>GBP Total</b></td><td><b>£<?php echo $orders_total; ?></b></td></tr>
        	</table>
    	
        </div>
    
    </div>

</div>
</body>
</html>
<?php
$message1 = ob_get_clean();
mail( $emailto, $subject, $message1, $headers );
?>	

Thanks guys!

Link to comment
https://forums.phpfreaks.com/topic/275499-php-sendmail-not-sending-html/
Share on other sites

  • Solution

If you are getting frustrated that your email is being sent as plain text instead of HTML, typically one of your headers showing up in the email (even if other scripts on the same server seem to work fine!!!) then structure your headers like this:

 

<?php

$headers = 'From: You <you@example.com>' . "\n";

$headers .= 'MIME-Version: 1.0' . "\n";

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

?>

 

Notice that the From is before the MIME and Content and only Content ends with "\r\n", the other are just "\n"

 

Not sure how it is possible for other scripts on the same server, same domain to work fine as shown in the very top instructions, and others need this crap... I spent a very frustrating couple hours figuring this out, hope you can avoid doing the same.

I found this on the php mail page.  http://php.net/manual/en/function.mail.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.