Asked  7 Months ago    Answers:  5   Viewed   41 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?



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

Wednesday, March 31, 2021
answered 7 Months ago

After trying various ways, i found following code working with almost all email providers

$to['email'] = "recipients email address";      
$to['name'] = "name";   
$subject = "email subject";
$str = "<p>Hello, World</p>";
$mail = new PHPMailer;
$mail->SMTPAuth = true;
$mail->Host = 'Specify main and backup server here';
$mail->Port = 465;
$mail->Username = '';
$mail->Password = 'email account password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'From Email Address';
$mail->FromName = "Any Name";
$mail->AddReplyTo('', 'any name'); 
$mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->WordWrap = 50;    
$mail->Subject = $subject;
$mail->Body    = $str;
if(!$mail->Send()) {
$err = 'Message could not be sent.';
$err .= 'Mailer Error: ' . $mail->ErrorInfo;                        


variable values needs to be changed accordingly. Hope these helps people having issues with PHPmailer

Saturday, May 29, 2021
answered 5 Months ago

I think your problem is related with n and QMAIL, the mail function documentation state this.

Note: If messages are not received, try using a LF (n) only. Some poor quality Unix mail transfer agents 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.

so you if you header are separated by rn you could probably replace those by single n.

This bug report also provide a solution using a script.

Saturday, May 29, 2021
answered 5 Months ago

Alright, this is kind of disappointing I'm sure, but you fundamentally cannot do this with 100% reliability.

Windows assumes that the active window is the one getting keyboard input. The proper way to fake keyboard input is with SendInput, and you'll notice that it sends messages to the active window only.

That being said, you can SendMessage WM_KEYUP, WM_CHAR, and WM_KEYDOWN messages and (depending on the WndProc receiving them) maybe get away with it. But remember, its going to break under some circumstances, period.

Saturday, June 5, 2021
answered 5 Months ago

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"
Thursday, September 23, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :