问题
I have a very simple PHP script, something like:
while ($condition){
echo "<a href="thanks.php?id=".$id."> THANKS </a>";
}
Of course, I have a bit more code but it doesn't matter. Once I had created this piece of code, the script sends an email to the user.
THE INBOX
The links are okay in every single line, except the LAST ONE shows the link this way:
tha nks.php?id.....
It adds a space in between the code.
This only happens with hotmail. Gmail, yahoo, and everything else work just fine.
回答1:
I know this is late but there is an alternate solution that worked for me:
Use this line to encode your entire message using base64:
$message = chunk_split(base64_encode($message));
Then, append this your header:
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";
That will tell the mail client that your message is base64 encoded.
回答2:
karancan's solution probably works, but the root cause is what Hobo was saying. The mail
function inserts a line-break every 900 characters (I think). So if you're building up your $message with a bunch of $message .= "more text";
you'll encounter this error once that line is greater than 900 characters long. It's confusing though because the spaces seem to be intermittent, particularly if you're building up an HTML message because sometimes the linebreaks will appear in perfectly benign locations.
The simple solution is to add a \r\n\
to the end of lines.
Interestingly, these forms work:
$message .= "<tr><td>1</td><td>2</td>\r\n";
$message .= '<tr><td>1</td><td>2</td>'."\r\n";
But this does not:
$message .= '<tr><td>1</td><td>2</td>\r\n';
The \r\n
must be surrounded by double quotes, otherwise the characters will just get added to the text instead of creating a carriage return/line break.
回答3:
Was facing the same problem and tried most of these solutions but it did not work for me. It seems when the line seems too long to mail function it adds space around 900 character mark. So the following solution of adding wordwrap worked for me.
mail($to, $subject, wordwrap($message), $headers);
回答4:
I've seen sendmail insert characters when lines are too long. That's not the case here (as other clients are handling it fine), but I wonder if hotmail has a line length limit that you're hitting.
Does it make any difference if you insert a newline in your echo
, ie
while ($condition){
echo "<a href=\"thanks.php?id=".$id."\"> THANKS </a>\n";
}
回答5:
2019 working answer
The SMTP protocol requires a CRLF after each line which contains more than 998 characters. Basically each line of 1000 must contain at its very end the CRLF characters.
The simplest working solution to this issue is to send the email content encoded in base64 then add a header that tells mail clients that the content was encoded in base 64 and then send the email.
Bellow is an working example. I've tested with a mail which had ~ 27000 chars and it worked perfectly. Before this update, it was breaking critical links in my HTML content.
<?php
// used for date() function
date_default_timezone_set('Europe/Bucharest');
$to = 'john.doe@company.com';
$from = 'john.doe@company.com';
$fromName = 'John Doe';
$subject = "Html email test at ".date('H:i:s');
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "Content-Transfer-Encoding: base64" . "\r\n";
// Additional headers
$headers .= 'From: '.$fromName.'<'.$from.'>' . "\r\n";
$headers .= 'Cc: welcome@example.com' . "\r\n";
$headers .= 'Bcc: welcome2@example.com' . "\r\n";
$extremely_long_html_email = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"/>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>
<title>TEST</title>
</head>
<body>
Very long HTML code. I stripped mine due to copyright issues.
</body>
</html>";
// Send email
if(mail($to, $subject, base64_encode($extremely_long_html_email), $headers)){
echo 'Email has sent successfully.';
}else{
echo 'Email sending failed.';
}
?>
TL;DR;
Add extra headers $headers .= "Content-Transfer-Encoding: base64" . "\r\n";
and encode html content before sending it using base64_encode($html_email)
function.
来源:https://stackoverflow.com/questions/9999908/php-mail-function-randomly-adds-a-space-to-message-text