PHP mail function outlook UTF8 - php

My script:
$message = "
<html>
<head>
<title>Birthday Reminders for August</title>
<style>td,tr{border:solid 1px black;}</style>
<meta charset='UTF-8'>
</head>
<body>
<table>
<tr><td>სახელი</td><td>{$_POST['firstname']}</td></tr>
<tr><td>გვარი</td><td>{$_POST['lastname']}</td></tr>
<tr><td>დაბადების თარიღი</td><td>{$_POST['birthday']}</td></tr>
<tr><td>მოქალაქეობა</td><td>{$_POST['cityzen']}</td></tr>
<tr><td>პირადი ნომერი</td><td>{$_POST['id_number']}</td></tr>
</table>
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
//MUSHA
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; utf-8' . "\r\n";
// Additional headers
$headers .= 'To: Mary <aaa.wwww#gmail.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\n";
// Mail it
mail($to, $subject, $message, $headers);
I've tested it on gmail.com, yahoo.com, outlook.com and everything works great.
but in MS Outlook it has problem of UTF-8.

Try using:
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

try adding
header("Content-type:text/html;charset=utf-8");
to in top of your php file. Also Which version of Outlook you use?

Related

Bit strange behavior of php mail function

By mistake, I added from-address as to-address in mail-function.
It sends mail to both to-address and from-address why? Is it documented anywhere?
$from = 'from_user#gmail.in';
$to = 'to_user#gmail.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$message = json_encode(compact('to', 'from', 'headers'));
// NOTE THE FROM INSTEAD OF TO
mail($from, $subject, $message, $headers);
Further to the comments and to highlight your request for reference. Here is a snippit from the php manual for reference. Notice the first line of additional headers:
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
So you are sending an email both through the mail($to... (which happens to be the $from in your case) but you are also sending the $to in the $headers declaration.

Issue PHP mail header

I have the following issue when using php mail, I'll expose the two ways I do it and wich result I get and expect:
Case 1:
$to = $EmailsPropiosPresupuestos;
$subject = $txtAsuntoCliente;
$message = $emailCliente;
$headers .= "From: noreply\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
The email i sent and the html decoded in the correct way, but my "From" header looks like: "noreply#124512.net" (Strange root).
Case 2:
$to = $EmailsPropiosPresupuestos;
$subject = $txtAsuntoCliente;
$message = $emailCliente;
$headers .= "From: noreply#example.com\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
Now my "From" header is fine, "noreply#example.com", but the mail doesn't decode the html, so I can't view the message in the correct way.
Try to add the MIME version to your header:
$headers .= "MIME-Version: 1.0\n";
For the no-reply I suggest you to add it using the Reply-To:
$headers .= "Reply-To: noreply#example.com\n";
This will allow you to keep a better from email address.
And In my code I use \n on each header line and finish with \r\n.
from: http://de3.php.net/manual/en/function.mail.php
Example #4 Sending HTML email
It is also possible to send HTML email with mail().
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>

PHP/HTML Code Renders as Text in Email: Any Insight?

I've tried many header changes to no avail.
Could it be the email server? Or am I missing something crucial?
I would like to see more than the code rendered as text in the resultant email.
Thanks!
<?php session_start(); ?>
<?php
$body = '';
$body .= '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
</head>
<body>
<h1>Here is a Copy of Your Order</h1>';
$body .= '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
$body .= "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
}
$body .= '</table>';
$body .= "<p>Grand total: \$$total</p>";
$body .='</body></html>';
}
?>
<?php
echo $body;
$to = 'xxxx#gmail.com';
$subject = 'Fill This Order';
$headers = 'From: xxxx#gmail.com' . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
mail($to, $subject, $message, $body, $headers);
?>
here is a tutorial about how to send a mail with html code in it: Sending Nice HTML Email with PHP
work around it, what they got works.
as you can easly see there, the headers sent is:
$to = 'bob#example.com';
$subject = 'Website Change Reqest';
$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";
they diffrence you can see is mostly: \r\n in every end of line.
this could be your problem because without it it can be something like:
$headers .= 'MIME-Version: 1.0' . PHP_EOL . 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
and in headers... this could be: 'MIME-Version: 1.0Content-type: text/html; charset=iso-8859-1
and not work..
i think you should take a look in the link i added and modify your code by it.
EDIT:
i think i found it! you sent mail($to, $subject, $message, $body, $headers);
while PHP mail function uses:
mail(to,subject,message,headers,parameters)
you can see here how to use the mail function: The PHP mail() Function
the thing i think heppend is you sent $message which does not exsits and the headers are not sent as they should.
Have you tried swapping out PHP_EOL for "\r\n"?

Why isn't the html of sent mails being rendered?

My script send the email but it doesn't render the html tags. I'm not sure why not.
$email = $row['email'];
$mail_body = '<html>
<body>
<p>hello,'.$name.'</p>
<p>this is a testing email </p>
<hr />
<p>by server</p>
</body>
</html>';
$subject = "Better website";
$to = "$email";
$headers = "From: mailscript#hotmail.com\r\n";
$headers .= "Content-Type: text/html\r\n";
$mail_result = mail($to, $subject, $mail_body, $headers);
Try setting the mime type as well as shown in the manual for mail()
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
as Jrod already answered you need to add the header
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
but I found that this one is screwing mine up as of late and had to comment it out:
$headers = 'MIME-Version: 1.0' . "\r\n";

Why is the From line not working on my mail function?

I am using a mail function to send html to an email address, but the From name and email address aren't showing up. This is my code:
$name = $_POST['name'];
$mailTo = 'name#email.com';
$subject = 'Message from ' . $_POST['name'];
$message =
'<html>
<head>
<title>HTML email</title>
</head>
<body>
<p><b>Name:</b> ' . $_POST['name'] . '</p>
<p><b>Email:</b> ' . $_POST['email'] . '</p>
<p><b>Message:</b> ' . $_POST['mainmessage'] . '</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Postmaster <some#body.com>';
mail($mailTo, $subject, $message, $headers);
I would expect the email to show up as being from Postmaster at the email address some#body.com, but it is showing up as coming from ideapale#box486.bluehost.com, which is my hosting provider.
What did I not set up correctly?
Chris, try adding the \r\n after the <some#body.com>. I've found that php can be very picky when talking to mail servers.
Edit: just to help a little more, I have this (almost exactly what you have) in one of my working scripts:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$to . "\r\n";
$headers .= 'From: ' .$from. "\r\n";
...where $from = $fromname.' <'.$fromemail.'>'; and $to is just an email address.

Categories