Viewed   235 times

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' ."rn";
    $headers .= 'To: '.$to ."rn";
    $headers .= 'From: '.$from . "rn";
    $headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "rn";
    $headers .= 'Content-Transfer-Encoding: 8bit'."rn";

    mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}

Any suggestions on alternative ways to achieve this goal?

 Answers

3

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 "xD8xA8xD8xB1xD9x8AxD8xAF" in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1YnX/".

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.)

Sunday, December 4, 2022
2

Also set your page language to utf8 eg:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and then see it if worked. If that still doesn't work, go and check this out, it is complete solution for the arabic language using PHP:

http://www.ar-php.org/en_index_php_arabic.html

You may want to check out this too:

http://www.phpclasses.org/browse/package/2875.html

Tuesday, November 22, 2022
1

Thanks @Mike for helping me correct the answer. So As per the question and OP's OS it seems that you(OP) are encountering a variant of this error.

And as discussed on that link, you can use the following code to detect the file name encoding

...    

$final_url = $URLX."/".$BookName."/".str_replace(' ', '', $eachChapiter[$x]);

mb_detect_encoding($final_url, 'UTF-8', true)) ? utf8_decode($final_url) : $final_url;
$content = file_get_contents($final_url).".txt");

Remember that this code will create problems(again discussed on that link) on a linux server. So if you are using Linux production environment then you can apply the solution suggested there.

Thursday, October 13, 2022
1

Try #2... How about using a Exchange Pickup Folder instead? They are a faster way to send emails through Exchange because it just creates the email and drops it in the folder, no waiting to connect to the server or waiting for a reply. Plus I think it skips the whole relay issue.

Configure youur SmtpClient like so:

SmtpClient srv = new SmtpClient("exchsrv2007", 25) {
    DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory,
    PickupDirectoryLocation = "\exchsrv2007PickupFolder"
}
...
Wednesday, September 14, 2022
 
50

Generally speaking, you can't. Spammers would really love for this to be possible, and this is the reason why there are lots of systems in place to avoid this.

If you have a legitimate reason for sending emails on behalf of someone else, there are two solutions:

  • Send the emails through their mail server.
  • Ask them to configure their anti-spam settings (SPF, DKIM, DMARC, whatever) to allow your mail server to send emails using their SMTP domain in the sender address.

Both solutions require their willing cooperation, but the technical details depend on which mail server and anti-spam settings they are using.

Wednesday, September 14, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :