Mail generated using PHP mail function showing only code - php

I have written a code to send mail using PHP mail() function
$message = "
<html>
<head>
<title>TITLE</title>
</head>
<body>
<p>Customer ".$customerName." with id ".$customerId." wishes to place order for the following products that are currently note available in stock</p>
<table>
<tr>
<th align='center'>Product Name</th>
<th align='center'>Product Id</th>
<th align='center'>Product Part Number</th>
<th align='center'>Quantity</th>
</tr>"
.$content.
"
</table>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: '.$customerEmail.'' . "\r\n";
mail($to,$subject,$message,$headers);
The mail is working fine for me, but for the client it appears like this
From:
Sent: None
To: orders#xxxxx.com
Subject: SUBJECT
Content-type:text/html;charset=UTF-8
From: xxxx#xxxx.com
Message-Id: <20140908144611.165ACC36BC#SV08.xxxxxx.com>
Date: Mon, 8 Sep 2014 17:46:11 +0300 (AST)
X-Nonspam: None
X-Antivirus: avast! (VPS xxx-0, 09/09/2014), Inbound message
X-Antivirus-Status: Clean
<html>
<head>
<title>TITLE</title>
</head>
<body>
<p>Customer XXXX with id 3
wishes to place order for the following products that are currently not
available in stock</p>
<table>
<tr>
<th align='center'>Product Name</th>
<th align='center'>Product Id</th>
<th align='center'>Product Part Number</th>
<th align='center'>Quantity</th>
</tr><tr><td
align='center'>SCREW;BHHS;3/8-16 X .75</td><td align='center'>3237<td
align='center'>35059</td><td align='center'>6</td></tr>
</table>
</body>
</html>
Im not understanding why its working for our tests but not for the client. I tested using my gmail account and it shows fine where as the client is using his mail id on his company domain.

Instead of php mail function, I suggest you to use the phpMailer library with google mail smtp credentials. this will help you to send HTML template mails.
Reference URL - PHP mailer example url

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

Remove and tags from your $message. And change headers to
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Related

PHP mail function sending the email, but not the html form data

The HTML consists of simple input fields as listed in the php below, what happens is PHP mail() sends the email but excludes the form data itself. I have spent a lot of time but not sure why this is happening.
and the php to send the mail:
<?php
$to = "user#gmail.com";
$subject = "Question Bank - New Question";
$message = '<html>
<head>
<title>New Question</title>
<style>
table {
border: 1px solid black;
}
table th, tr{
text-align:center;
}
</style>
</head>
<body>
<table cellspacing="20px">
<tr>
<th>Username</th>
<th>Email</th>
<th>Question</th>
<th>Question Hint</th>
<th>Correct Answer</th>
</tr>
<tr>
<td>'.$_POST["username"].'</td>
<td>'.$_POST["email"].'</td>
<td>'.$_POST["question"].'</td>
<td>'.$_POST["questionhint"].'</td>
<td>'.$_POST["answer"].'</td>
</tr>
</table>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: user#company.com' . "\r\n";
$headers .= 'Cc: cc#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

How to send mail using php by insert html into mail content? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
How to send mail using php by insert html into mail content ?
I tried to insert html code inner $message, When i test it's show error
like this Parse error: syntax error, unexpected 'margin' (T_STRING)
How can i do ?
<?PHP
include("connect.php");
$email = "test_mail#hotmail.com";
$to = $email;
$subject = "test subject";
$message = "
<body style="margin: 0; padding: 0;">
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<img src="http://i.stack.imgur.com/Jy9QUm.jpg"/>
</td>
</tr>
<tr>
<td>
test text
</td>
</tr>
</table>
</body>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>
You have an issue with wrapping your $body string try this
<?PHP
include("connect.php");
$email = "test_mail#hotmail.com";
$to = $email;
$subject = "test subject";
$message = "
<body style='margin: 0; padding: 0;'>
<table border='1' cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td>
<img src='http://i.stack.imgur.com/Jy9QUm.jpg'/>
</td>
</tr>
<tr>
<td>
test text
</td>
</tr>
</table>
</body>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>

How to send pdf attachment using PHP [duplicate]

This question already has answers here:
php send e-mail with PDF attachment
(4 answers)
Closed 7 years ago.
Good day! My code is working great if the format is in msword but when i changed it to PDF it becomes corrupted what should i do? Please help me.
$headers = "From:<noreply#example.com.ph>";
$to = 'example#example.com';
$subject = 'Purchase Order';
$message .= 'Please see attached file';
$txt .=" <html> <body>
<p><b> PO Number:</b>
$purchasenumber</p>
<p><b> Style Code:</b> $styleCode</p>
<p><b> Generic Number:</b> $gennum</p>
<p><b> Vendor Name:</b> $vendname</p>
<p><b> Planned Delivery Date:</b>
$pdelivdate</p> <br/> <br/>
<table border=1 style='width:100%' cellpadding='0'>
<thead>
<tr>
<th width='16.7%'>Material Number</th>
<th width='16.7%'>Color</th>
<th width='16.7%'>Size</th>
<th width='16.7%'>Ordered QTY</th>
<th width='16.7%'>Total Cost</th>
<th width='16.7%'>Total SRP</th>
</tr>
</thead>
<tbody>
";
$statement = $db->prepare("SELECT * FROM purchaseorderproductitem where purchaseorderID = :pid");
$statement->execute(array(':pid' => $purchasenumber));
foreach ($statement->fetchAll() as $row)
{ $matnum = $row['materialnumber']; $color = $row['color']; $size = $row['size']; $qty = $row['quantity']; $units = $row['units']; $curcost = $qty * $cost; $cursrp = $qty * $srp; $curcost = number_format($curcost, 2, '.', ''); $cursrp = number_format($cursrp, 2, '.', '');
$txt .="
<tr> <td width='16.7%'>$matnum</td> <td width='16.7%'>$color</td> <td width='16.7%'>$size</td> <td width='16.7%'>$qty $units</td> <td width='16.7%'>$curcost</td> <td width='16.7%'>$cursrp</td> </tr>
";
}
$txt .="
<tr> <td width='16.7%' text-align:'center'>Total</td> <td width='16.7%'> </td> <td width='16.7%'> </td> <td width='16.7%'>$totalqty pcs</td> <td width='16.7%'>$totalcost</td> <td width='16.7%'>$totalsrp </td> </tr>
</body> </table> </html>
";
// Always set content-type when sending HTML email $message = "MIME-Version: 1.0" . "\r\n"; // $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $message .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fileatt_name2 = "PurchaseOrder.pdf";
$semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment $headers .= "\nMIME-Version:
1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $data2 = chunk_split(base64_encode($txt));
$message = "{$mime_boundary}\n" . "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" . "Content-Transfer-Encoding: 7bit\n\n" .
$message .= "{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .
// Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . // {$fileatt_type} " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n";
// Send the message $send = mail($to, $subject, $message, $headers);
Can you please help me to solve this issue? Thanks in advance!
You can use a combination of PHPMailer:
http://phpmailer.worxware.com/
And TCPDF:
http://www.tcpdf.org/
To accomplish this task. I will not cover the processes in detail as code examples would be quite tedious to create however both pieces of software have detailed documentation and examples found here:
https://github.com/Synchro/PHPMailer
And here:
http://www.tcpdf.org/docs.php
Edit:
If you do not want to to use something that just works like PHPMailer then I would ensure that the correct headers are being sent.
One useful trick i have found here too is that if you open said corrupted file in a text editor, you most usually find useful information at the very beginning relating to any errors that may have occurred while processing the output.
Edit:
Just guessing here but your last few lines of code I believe should read as follows:
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "boundary=\"{$mime_boundary}\""; $data2 = chunk_split(base64_encode($txt));
$headers .= "{$mime_boundary}\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n";
// Add file attachment to the message
$headers .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . {$fileatt_type} " name=\"{$fileatt_name2}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name2}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data2 . "\n\n" . "--{$mime_boundary}--\n";
// Send the message
$send = mail($to, $subject, $message, $headers);
You had concatenated the end of one of the statements instead of ending it with a semicolon ;
You were adding the headers to the $message variable, they should rather be in the $headers variable correct?
You had added one of the headers twice.

How to send values from html form to mail in table PHP

This what I have I tried to put it in a table just like:
<table>
<tr><td>$_POST['onderwerp']</td></tr>
</table>
This is what I have it sends the mail but it's to messy:
<?php
$to = 'example#gmail.com';
$subject = 'Vraag via de website';
$message = 'Onderwerp:'. $_POST['onderwerp'].'<br /><br />'.$_POST['vraag'].'<br /><br />'.'Telefoonummer:'. $_POST['tel'].'<br /><br />'.'Email:'. $_POST['email'] ;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:<eexample#gmail.com>' . "\r\n";
$headers .= 'The shop<example#gmail.com>' . "\r\n";
mail($to, $subject, $message, $headers);
header('Location: contact.html');
?>
I just want to send the variables in a table so that I don't have to search through all the text.
<?php
$to = 'user#example.com';
$subject = 'Vraag via de website';
$msg = "<html>
<head>
<title>Title of email</title>
</head>
<body>
<table cellspacing=\"4\" cellpadding=\"4\" border=\"1\" align=\"center\">
<tr>
<td align=\"center\">Onderwerp</td>
<td align=\"center\"> vraag</td>
<td align=\"center\">Telefoonummer</td>
<td align=\"center\">Email</td>
</tr>
<tr>
<td align=\"center\">".$_POST['onderwerp']."</td>
<td align=\"center\">".$_POST['vraag']."</td>
<td align=\"center\">".$_POST['tel']."</td>
<td align=\"center\">".$_POST['email']."</td>
</tr>
</table>
</body>
</html>";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Site Name <me#mysite.com>' . "\r\n";
mail($to, $subject, $msg, $headers);
?>
you could try this using your variables.
$var = 'test';
$var2 = 'test2';
echo '<table border="1">';
echo '<tr><td>' . $var . '</td></tr>';
echo '<tr><td>' . $var . '</td></tr>';
echo '</table>';
this will show the variables in a table, then you can edit the table using css how you want. :)
I suggest that you include swiftmailer.
Swiftmailer makes sure emails get delivered in the Inbox and you can easily include HTML markup in your emails:
Swiftmailer HTML in email
Just download the Swiftmailer Library, include and configure it like this example:
Sending an email in swiftmailer
Let me know if this helps you out!

Formatting my email sent with mail() through PHP, and IIS7

First thing. My code works. I have my SMTP information set up correctly in IIS7's sendmail.ini, and i'm using gmail. What I have is a shopping cart for ordering food.
In my cart.php page, I have the print output pulling in the users choices:
$printoutput .= "<table width='500' border='0' cellspacing='0' cellpadding='0'>
<tr><td> <strong>Order Item # ". ($i+1) ."</strong></td></tr>
<tr><td> ". $product_name . "</td></tr>
<tr><td>$" . $price . ".00</td></tr>
<tr><td>". $displayoptions . "</td></tr></table>
<table height='1%' width='500' border='0' cellspacing='0' cellpadding='0'><tr><td></td></tr></table>";
$i++;
This output looks perfect in a web browser. Later in the code, I use a form to post the $printoutput and name it order. But on my email.php page, I use
session_start();
if(isset($_POST['order'])){
$order = $_POST['order'];
}
and then
mail($email_to, $email_subject, $order, $headers);
well like I said, I can just simply echo $order and it looks perfect in the web browser. But when actually sent as an email, I get an email with ALL of the html mark up with it. I've tried a few things, but nothing is formatting my information the way I want it to. Does anyone know how I can get this to basically format correctly in an email?
I'd like to have my information simply listed like this:
Order Item #
Product Name
Price
Options
But currently the output in the email has a ton of extra spaces, and shows all the HTML.
Any help would be appreciated.
http://www.w3schools.com/Php/func_mail_mail.asp
<?php
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
?>

Categories