PHP mail() changing some characters to htmlentities? - php

My PHP mail() script changes the ! exclamation mark on some email clients such as hotmail to %21 when the ! is in a tag in the email body.
Here's my script
$to = "myemail#outlook.com";
$subject = "Password Reset";
$body = "Link 1
<br><br>
Without href: http://example.com/#!/page - regular text
";
$headers = "From: no-reply#example.com\r\n";
$headers .= "Reply-To: no-reply#example.com\r\n";
$headers .= "Return-Path: no-reply#example.com\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to,$subject,$body,$headers);
So in the script above, the ! is changed to %21 only when it's a link, the regular text keeps it as /#!/instead of transforming it to /#%21/
How would I go about fixing this issue so it doesn't change to %21?

Some characters are encoded because they are not valid URLs. In your case URL code points are replaced with percent-encoded byte %.

Related

PHP fails to send UTF-8 Messages [duplicate]

This question already has answers here:
How to send UTF-8 email?
(3 answers)
Closed 5 years ago.
I have the following PHP code:
<?php
$to = 'info#neatgr.cf';
$subject = date("d/m/Y");
$message = 'Hey 123 !## αβγ';
$headers = "From: testsite < mail#testsite.com >\n";
$headers .= "Cc: testsite < mail#testsite.com >\n";
$headers .= "X-Sender: testsite < mail#testsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail#testsite.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
mail($to,$subject,$message,$headers);
echo "<script type='text/javascript'>alert('Message sent! We will get back to you soon!');</script>";
echo "<script>window.location.href = 'http://example.com';</script>";
?>
The mail gets sent fine. The problem is that αβγ (Unicode Characters) doesn't get on the mail recipient's end correctly.
This is what the recipient sees: Hey 123 !## αβγ
This is what he should see: Hey 123 !## αβγ
I've looked everywhere and tried everything, changing headers, converting my string to unicode etc. etc., but nothing has worked. Maybe I'm doing something wrong?
Use the UTF-8 charset header, for example:
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
Goes to:
$headers .= "Content-Type: text/html; charset=UTF-8\n";

php mailer script error 5

This is my code and i have done everything to make it work but i am stuck. I can't get this code to sent email out ..
<?php
if(isset($_REQUEST['submit']))
{
$to = "usman_86#rocketmail.com";
$subject = "Find Dealer # flowsleeve";
$headers = "From: Find Dealer ";
$headers .= "Reply-To: ". strip_tags("usman_86#rocketmail.com") . "\r\n";
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$messsage = "
Name : ".$_REQUEST['first_name']."
Email : ".$_REQUEST['email']."
Event Type : ".$_REQUEST['event_type']."
Event location : ".$_REQUEST['event_location']."
Last Name : ".$_REQUEST['last_name']."
Home Phone : ".$_REQUEST['home_phone']."
Event Date : ".$_REQUEST['event_date']."
Referrer : ".$_REQUEST['referrer']."
Message : ".$_REQUEST['message']."";
mail($to,$subject,$message,$headers);
The problem is in header section. You forgot to add . in line 8. Take a look at this and correct it.
$headers = "From: Find Dealer ";
$headers .= "Reply-To: ". strip_tags("usman_86#rocketmail.com") . "\r\n";
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Because of this mistake, header will contain only mime version info, content type and charset.
I tried your code, without the if ( isset .. statement.
Your code works successfully. Please add additional code or errors.
Make sure you also check your spamfolder.

Using mail() to sent UTF8 e-mail

I'm trying to send an e-mail with PHP, including multi-language characters. The whole site (html header, php header) are set to UTF8 aswell as the form charset.
To PHP code I have now is:
$to = "email#email.com";
$subject = "Subject";
$message = "Question is ".$question;
$from = "auto#from.com";
$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-UTF-8' . "\r\n";
$sendmail = #mail($to,$subject,$message,$headers);
I supose I'm wrong somewhere ?
I'm getting '?' characters with Japanese letters for examnple.
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/html; charset=utf-8"."\r\n";
should be all you need.
I may be wrong, but as far as I know iso-UTF-8 is not a valid charset, at least it doesn't look like it is.

Sending a link in an email

Ok before you guys laugh LOLZ --> I'm not sure if this is a GMAIL issue or something I'm doing wrong but im trying to insert an email for an activation link in the body of a message.
$body = "Or click on this link: Activate Your Account "
Ok the problem here is that my GMAIL account shows this: (which BTW is click-able and routes correctly)
Or click on this link: <a href="http://www.url.com/start/page.php?verify=true&id=f22645cff5ecfd4d3c115af5&activate=75845> Activate Your Account </a>
What am I missing here? Is this just a gmail issue? How can i Correct this?
I want it to show: Activate Your Account
Try closing the quote at the end of your URL.
The SO syntax highlighting even showed you the issue.
Wrong Syntax,
error hould have showed you whats happening,
Parse error: syntax error, unexpected T_STRING on line x...
2 . Send HTML mail, the Content-type header must be set to html
I made some changes and tested this, try this working script :
<?php
$body = 'Or click on this link: Activate Your Account ';
// To send HTML mail, the Content-type header must be set
$headers = 'From: webmaster#example.com' . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("to#gmail.com","Subject goes here",$body,$headers);
?>
PHP Mail
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
For sure you're missing a double quote here.
<a href="http://www.example.com/...5845>
This shall be instead written as:
<a href="http://www.example.com/...5845">
If you didn't set the headers, this works for me. Of course you need to set your own variables.
$sender_email = strip_tags($sender_email);
$subject = "=?UTF-8?B?".base64_encode(strip_tags($title))."?=";
$message = nl2br(strip_tags($message));
$headers = "From: ".$sender_email.' <'.$sender_email.'>'."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";
$headers .= "\tformat=flowed;\r\n";
$headers .= "\tcharset=\"utf-8\";\r\n";
$headers .= "\treply-type=original\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "Reply-To: ".$sender_email."\r\n";
$body = __('sendfriend:defaultmessage') . '<br /><br/>'.$link.'<br/><br />'.$message;
$mail = mail($recipients, $subject, $body, $headers);
Make sure to send the email as an HTML email.
$headers = "From: myEmail#example.com\r\n";
$headers .= "Content-Type: text/html\r\n";
mail(...$headers);
Add the $headers variable on the end of the mail function.
define headers in your mail function first
$headers = 'From: abc#example.com' . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

How do I send emails with Arabic content via PHP's mail function?

I'm having a challenge with sending emails with arabic content using PHP's mail function. Let's say I have this simple arabic string:
بريد
I've tried several ways to utilize the headers, but the emails content all still end up with something like: X*X1X(X1Y X/. However, the email subject is correctly encoded if I use arabic characters (thanks to the base64_encode, see function below)
Here's one of the email functions I've tried
function sendSimpleMail($to,$from,$subject,$message) {
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'To: '.$to ."\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'."\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}
Any suggestions on alternative ways to achieve this goal?
Unfortunately, 8bit encoding is not reliable in e-mail. Many mail transport agents will remove the top bit of every byte in the mail body. بريد is "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF" in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1Y\nX/".
The way to get non-ASCII characters into a mail body is to set Content-Transfer-Encoding to either base64 or quoted-printable, and the encode the body with base64_encode or quoted_printable_encode, respectively.
(quoted-printable is better if the mail is largely ASCII as it retains readability in the encoded form and is more efficient for ASCII. If the whole mail is Arabic, base64 would probably be the better choice.)
$boundary = uniqid(rand(), true);
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\n";
$headers .= "This is a MIME encoded message.\n\n";
$headers .= "--$boundary\n" .
"Content-Type: text/plain; charset=UTF-8 \n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($plaintext));
$headers .= "--$boundary\n" .
"Content-Type: text/html; charset=ISO-8859-1\n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($msg));
$headers .= "--$boundary--\n" .
mail($address, $subject, '', $headers);
This one works for me.
Try this
$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
Your code works for me as-is.
Are you sure that $message contains a valid UTF-8 string?

Categories