php send mail script encoding issue on windows - php

I have problem with non english characters with this e-mail script. If I set the script to send e-mail to my gmail account, I have no problem. However, if I set the script to send email to my domain account and if i open the email with windows live mail or with microsoft outlook then the e-mail is not readable. I must to go to encoding menu, and then select the utf-8, and then i can read the email.
If I open the mail on iMac mail client, i have no problem.
My customer see this as big problem and want me to solve it. Anyone can help?
Here is the code:
<?php
// send the form to the specify email
// CONFIG VARS
$subject = "mysite.com | contact form";
$to = "myaccount#somemail.com";
$from = 'another#somemail.com';
//data
$msg = "Name: " .$_POST['namesup'] ."<br>\n";
$msg .= "Email: " .$_POST['emailsup'] ."<br>\n";
$msg .= "Phone: " .$_POST['phonesup'] ."<br>\n";
$msg .= "Message: " .$_POST['yourtextsup'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send
mail($to, $subject, $msg, $headers);
?>
Any help will be deeply appreciated.
Zoran

Checkout this code it will work for me,
<?php
$subject = "mysite.com | contact form";
$to = "myaccount#somemail.com";
$from = 'another#somemail.com';
$msg = '<html>
<head>
</head>
<body>
<p>
Name: ".$_POST['namesup']."<br>
Email: ".$_POST['emailsup']."<br>
Phone: ".$_POST['phonesup']."<br>
Message: ".$_POST['yourtextsup']."<br>
</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "From: <".$from. ">" ;
mail($to,$subject,$msg,$headers);
?>
This line $headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n"; is resolved your issue of like Arabic language or others.

I would suggest making sure your message is valid HTML, including the <html> and <head> tags, and also make sure you include the <meta http-equiv="Content-Type" content="text/html charset=UTF-8" /> tag. So:
//data
$msg = '<html><head><meta http-equiv="Content-Type" content="text/html charset=UTF-8" /></head><body>';
$msg .= "Name: " .$_POST['namesup'] ."<br>\n";
$msg .= "Email: " .$_POST['emailsup'] ."<br>\n";
$msg .= "Phone: " .$_POST['phonesup'] ."<br>\n";
$msg .= "Message: " .$_POST['yourtextsup'] ."<br>\n";
$msg .= '</body></html>';

Try changing your scripts charset to:
<?php
// send the form to the specify email
// CONFIG VARS
$subject = "mysite.com | contact form";
$to = "myaccount#somemail.com";
$from = 'another#somemail.com';
//data
$msg = "Name: " .$_POST['namesup'] ."<br>\n";
$msg .= "Email: " .$_POST['emailsup'] ."<br>\n";
$msg .= "Phone: " .$_POST['phonesup'] ."<br>\n";
$msg .= "Message: " .$_POST['yourtextsup'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: <".$from. ">" ;
//send
mail($to, $subject, $msg, $headers);
?>

Related

php mail returns FALSE while error_get_last() returns NULL

Like the title says, mail returns FALSE to I call error_get_last() and it returns a NULL. The email is never received. The thing is, nearly identical code in another page on this site has no problem sending an HTML email, so the server must be okay.
I searched and found this question "PHP mail() returns false but no error is logged", but the answer doesn't seem to apply in my case.
My php code is:
<?php
$MSG_Info = array(
'', // 0 = Sender's name
'', // 1 = Sender's email address
'', // 2 = Subject of the message
'');// 3 = The message itself
$MSG_SendTo ='XXXX#yahoo.com';
function GetMessageText()
{
global $MSG_Info;
return
"<blockquote>".
"Name: ".$MSG_Info[0]."<br />".
"Email: ".$MSG_Info[1]."<br />".
"Subject: ".$MSG_Info[2]."<br />".
"Message: ".wordwrap($MSG_Info[3], 70, "\r\n")."<br />".
"</blockquote>";
}
function GetHTMLHeader()
{
return
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'.
'<html>'.
'<head>'.
'<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
'<title>Marguerite Gabe Braun</title>'.
'</head>'.
'<body>'.
'<p><font size="+2" color="#0000AA">'.
'<i>A Message from Marguerite’s Web Site</font></i></p>';
}
function SendMessageEmail($Dest)
{
global $MSG_Info, $MSG_SendTo;
$from = $MSG_SendTo;
$subject = $MSG_Info[2];
$message = GetHTMLHeader();
switch ($Dest)
{
case 1:
$to = $MSG_SendTo;
$replyto = $MSG_Info[1];
$message .= "<p>".$MSG_Info[0]." has sent you a message from your web site.</p>\n";
break;
case 2:
$to = $MSG_Info[1];
$replyto = $MSG_SendTo;
$message .= "<p>Here is the message you sent to Marguerite Gabe Braun from her web site.</p>\n";
break;
default:
$to = $MSG_SendTo;
$replyto = $MSG_SendTo;
$message .= "<p>".$MSG_Info[0]." has sent you a message from your web site.</p>\n";
}
$message .= "<p>".GetMessageText()."</p>\n"."</body></html>";
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
$headers .= "From: " . $from . PHP_EOL;
$headers .= "Reply-To: " . $replyto . PHP_EOL;
// Send the email.
error_reporting(E_ALL);
$MSG_Result = mail($to, $subject, $message, $headers);
if ($MSG_Result)
{
$MSG_Error = NULL;
}
else
{
$MSG_Error = error_get_last();
}
return $MSG_Error;
}
?>
I've inserted all kinds of code (not shown here) to verify that I'm not screwing up the email addresses, that mail() is indeed returning FALSE, and error_get_last() is returning NULL, that the correct information is being passed in POST, etc.
Thanks in advance.
Edit.
Brute force debugging, commenting out one line at a time, narrowed it down the the line in the header where I specify "From:". If I comment out that line the mail sends and it says it is from the webmaster at my site.
So I test more and it looks like the word "From:", or any line that starts with that, is removed from my message. So strange. See the code below. The code for $message for the Reply-to lines, the line with three "froms" in it, and the simple $from line show in the message in my email. All others are missing. Or maybe this is clearer. Message lines 2, 5, 6, 7, 8, and 10 show in my message. The others, which all start with "From:", do not. It's the dangest thing.
<?php
$message .= "From: ".$from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "From: "."\r\n";
$message .= "FROM: "."\r\n";
$message .= "From, from, FROM: "."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= $from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "From: ".$from."\r\n";
$message .= "Reply-To: ".$replyto."\r\n";
$message .= "<p>".GetMessageText()."</p>\n"."</body></html>";
// This commented-out code is used on another page and with works correctly.
// $headers = "MIME-Version: 1.0\r\n";
// $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
// $headers .= "From: ".$from."\r\n";
// $headers .= "Reply-To: ".$replyto."\r\n";
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
// $headers .= "From: " . $from . PHP_EOL;
$headers .= "Reply-To: " . $replyto . PHP_EOL;
// Send the email.
$MSG_Result = mail($to, $subject, $message, $headers);
if ($MSG_Result)
{
$MSG_Error = NULL;
}
else
{
$MSG_Error = error_get_last();
if ($MSG_Error == NULL) {$MSG_Error[0] = "Unknown mail() error.";}
}
return $MSG_Error;
}
?>

how to change the from attribute to my email in mail() function

I am trying to send an email to my customers when they place an order on my website. But the from appears like this:
From : n9da2313
I need to exchange it with info#awraq.me
I tried this but didn't work
`
$to = "founder#awraq.me";
$subject = "Your order";
$message = "Your order was placed successfuly";
$headers = "From: info#awraq.me";
$headers .= "\r\nReply-To: info#awraq.me";
$headers .= "\r\nX-Mailer: PHP/".phpversion();
mail($to,$subject,$message,$headers,"-f info#awraq.me");
`
try this-
$to = "somebody#example.com, somebodyelse#example.com";
$subject = "HTML email";
// 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);
If its not working then check if you have done some configuration in php.ini file.

PHP processing form : unknown sender

I created a form with a processing PHP file. Everything works fine. I receive the mail but it's from "Unknow sender" in Gmail. Why please ?
I would like to see in my email box the name and the firstname of the person who fills the form. What's wrong in my code ?
<?php
if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
extract($_POST);
if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_telephone) && !empty($form3_message)) {
$to = 'XXXXXX#gmail.com'; // My real email
$subject = 'Contact from the site';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
$headers .= 'Reply-To:'.$form3_email. "\r\n";
$message = '<html><body>';
$message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$message .= '<table>';
$message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
$message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
$message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
$message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
$message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
$message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo "Form sent";
} else {
echo "Form not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
Replace the $form3_name with $form3_email
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
^^^^^^^^^^^^ //<----- Here
That's a name , not an email address , and that's the reason you get that error.
Also, you need to wrap them in tags <>
The right way..
$headers .= 'From:' .$form3_firstname. " ".'<'.$form3_email.'>'."\r\n";
replace "\r\n" with "\n" and your problem will be solved... and also put return-path in your headers...
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
$headers .= 'From: \''.$form3_firstname.'\' <'.$form3_firstname.'>\r\nReturn-Path: <'.$form3_firstname.'>
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\n";
$headers .= 'Reply-To:'.$form3_email. "\n";
please let me know if you want furtther guidance...
Because you do not supply an emailaddress in your "From:" header. There always needs to be an emailaddress.
Try something like:
$headers .= "From: $form3_firstname $form3_name <$form_email>\r\n";
Mind you, you may have to test and/or escape your form variables; for instance, check that there is no newline in there, otherwise your form might be abused for spamming.

Changing header "From: noreply#domain.com" to other value, sends mail into spam folder

I have this php mail() code on my site, which works fine.
It is an ecard system, my client can fill out the from info (name, email) and to info (name, email), etc and Mail goes straight to Inbox, on google, hotmail, yahoo, etc.
When the receiver gets the email he can use the reply button, and it gets the right info.
The problem is the From: header in my mail form, I want to change this from noreply#example.com to the receiver's info, or any other info. When I do that, the mail goes into SPAM mail.
Here is the code I'm using
<?php
$name = $_REQUEST['name'] ;
$motive = $_REQUEST['email'] ;
$name2 = $_REQUEST['name2'] ;
$email2 = $_REQUEST['email2'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
$message = urldecode(stripslashes($message));
$headers = 'From:' . $name . ' John Q<noreply#example.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= "Reply-To:" .$name. "<" . $motive. ">\n";
$headers.= "Return-Path: My Company<admin#datatopixels.com>\n";
$headers.= "X-Mailer: PHP". phpversion() ."\n";
$messagee = "
<html>
<head>
<title>Title here</title>
</head>
<body>
<center>
<br>
</center>
</body>
</html>
";
mail($to, $subject, $messagee, $headers);
?>
You must to add a needle headers:
Sample code :
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
And I would suggest to read oswalds's

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"?

Categories