I'm sending mails using the PHP mail() function. The mail headers is not working properly.
$charset = mb_detect_encoding($message);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/html; charset='.$charset . "\r\n";
$headers .= 'Reply-To: '.$from . "\r\n";
$headers .= 'X-Mailer: php';
In the above code, the only first line is parsed and the later 4 lines are showing in the message body. "From" was not set.
$charset = mb_detect_encoding($message);
$headers = 'From: '.$from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset='.$charset . "\r\n";
$headers .= 'Reply-To: '.$from . "\r\n";
$headers .= 'X-Mailer: php';
In the above code, "From" and "MIME" lines are parsed correctly, but the later 3 lines are showing in the message body.
GMail is receiving it correctly.
Have you tried to use \n only instead of \r\n?
http://php.net/manual/en/function.mail.php
Note:
If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.
You may use the code below to easy change of end of line in email:
$EEOL = "\n";
$headers = 'From: '.$from . $EEOL;
$headers .= 'MIME-Version: 1.0' . $EEOL;
You should consider using an email class library instead of the regular mail() function in php.
I personally like the free SwiftMailer because of its simplicity and great functions for attachments etc.
Related
My previous header section was.
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'To: <abc#gmail.com>' . '\r\n';
$headers .= 'From: xyz <info#gmail.com>' . '\r\n' . 'Return-Path: info#gmail.com \n' . 'Reply-To: info#gmail.com \n';
And I replaced above with this : Which is working for me.
$headers = "From: xyz<info#gmail.com>\r\n";
$headers .= "Reply-To: xyz<info#gmail.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Now my question is :
1. What the reason that my old code was working fine previously and
2. What is technical difference b/w my old and new header section ?
Note the difference between '\r\n' and "\r\n".
Double quotes are a requirement for line breaks. If you use single quote, they will be treated as actual text and thus won't work properly
At my company, we have had several clients claim that their order confirmation emails are coming through just as the HTML source, not formatted at all. However the majority of our clients are receiving their emails through fine.
I think I may have found a link which explains why only some clients receive the unformatted email, however I'm not sure of the cause. It seems that users with a custom domain name for their email address are experiencing the problems and regular email domains such as #gmail.com or #icloud.com are working fine.
The problem is not related to the client used to view the email as I have been able to replicate the problem with my private domain name and the source code is displayed in all clients.
Any idea what would be the cause of this?
Here are my headers:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
Try following headers for HTML Email...
$from = $from . ' <' . $from . '>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$headers .= 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'] . "\r\n";
Okay okay, I know there are a million of these threads, but I did my best to look through them and could not find one that actually solved the problem for me. Any help is greatly appreciated, as depending on the browser it either hides the whole thing or shows it in plain text.
Here's what I'm using:
$headers = 'From: coupons#madisoncoupons.com\r\n';
$headers .= 'Reply-To: coupons#madisoncoupons.com\r\n';
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=ISO-8859-1';
mail($email,$subject,$content,$headers);
$content starts and ends with html and body tags, with a style tag in the beginning of the body with some CSS in it.
You'll want to use the correct quotes. "\r\n" and '\r\n' are two completely different things.
Read up on how PHP handles strings if you're rusty.
as #tadman said above, you have to modify your headers to the following
$headers = 'From: coupons#madisoncoupons.com' ."\r\n";
$headers .= 'Reply-To: coupons#madisoncoupons.com' . "\r\n";
Use this
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <info#example.com>' . "\r\n";
mail($email,$subject,$content,$headers);
I am new to PHP and send an email using following code
<?php
$to = 'to#xyz.com';
$subject = 'the subject';
$message = '<table dir="rtl"><tr><td>'. "\r\n";
$message .= '<b>This is Bold</b></br> <i> This is Italics</i></br>شطب 14 مرشحا لمجلس الأمة الكويتي'. "\r\n";
$message .= '</td></tr></table>';
//Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
$headers .= 'From: from#xyz.com' . "\r\n";
$headers .= 'Reply-To: replyto#xyz.com' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
ini_set ( "SMTP", "smtp.xyz.com" );
mail($to, $subject, $message, $headers);
?>
I can receive the email and it display the arabic also but it also show the from email address in the body of the email.
And other issue is that arabic is RTL even after mentioning in table dir-"RTL" it still show the message as LTR.
Example of Email Received
from#xyz.com
This is Bold
This is Italics
شطب 14 مرشحا لمجلس الأمة الكويتي
You could try using the alternative of CSS2 style:
style="direction:RTL; unicode-bidi:embed;"
Extra reading: http://www.i18nguy.com/markup/right-to-left.html
I am using php, using mail function:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: admin#domain.com' . "\r\n";
$headers .= 'Reply-To: Admin <admin#domain.com>' . "\r\n";
// Return Path -
$return_path = "bounce#domain.com";
$toUser... (all necessary variables)
if(mail($toUser,$subject,$body,$headers, "-f".$return_path)){
echo "res=sent";
} else {
echo "res=error";
}
I tested few emails like, abXXX#kasbkjasbdka.com, 8hgb87#9ndksjc9ne.com
(etc, all invalid, non-existent email addresses)
Why it dont go to my bounce#domain.com????
You should also be able to set the Return-Path using a header on most servers:
$headers .= "Return-Path: bounce#domain.com\r\n";
You need to add a space after -f. Also, why not use UTF-8 instead of iso-8859-1? Try this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: admin#domain.com' . "\r\n";
$headers .= 'Reply-To: Admin <admin#domain.com>' . "\r\n";
// Return Path
$return_path = 'bounce#domain.com';
/*
$toUser... (all necessary variables)
*/
if(mail($toUser, $subject, $body, $headers, '-f ' . $return_path)) {
echo 'res=sent';
} else {
echo 'res=error';
}
It may be caused by the configuration of your sendmail. The -F parameter is piped through as a command line attribute, so sendmail needs to be configured to support it.
From php.net:
The additional___parameters parameter
can be used to pass additional flags
as command line options to the program
configured to be used when sending
mail, as defined by the sendmail_path
configuration setting. For example,
this can be used to set the envelope
sender address when using sendmail
with the -f sendmail option.
Edit: I just saw that Matthias Bynens' answer is correct, see this entry