How do I send emails with Arabic content via PHP's mail function?
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.)
How do I send emails with arabic html content in php smtp
you can use PHPMailer Class
it is powerful , and open source .
for get examples please read this PHPMailer class examples
example for arabic , persian and etc.
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "yourname@gmail.com";
$mail->Password = "************";
$mail->AddReplyTo('yourname@example.com', 'Your Name');
$mail->AddAddress('username@example.com', 'User Name');
$mail->SetFrom('yourname@example.com', 'Your Name'); //
$mail->Subject = 'موضوع';
$mail->AltBody = 'برنامه شما از این ایمیل پشتیبانی نمی کند، برای دیدن آن، لطفا از برنامه دیگری استفاده نمائید'; // متنی برای کاربرانی که نمی توانند ایمیل را به درستی مشاهده کنند
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/html';
$mail->MsgHTML('<html>
<body>
این یک <font color="#CC0000">تست</font> است!
</body>
</html>'); // متن پیام به صورت html
//$mail->AddAttachment('images/phpmailer.gif'); // ضمیمه کردن فایل
$mail->Send();
echo "پیام با موفقیت ارسال شد\n";
}
catch (phpmailerException $e) {
echo $e->errorMessage();
}
catch (Exception $e) {
echo $e->getMessage();
}
?>
not able to send arabic email with good characters
- you aren't specifying any Content-Type header so the recipient will use some default encoding
- values appended to the
$additional_headers
parameter of themail()
function need to be properly sanitized (or users may inject additional headers)
Modified code (I assume the encoding is UTF-8):
$filterHeaderValue = function ($value) {
return str_replace(array("\r", "\n"), '', trim($value));
};
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . '.';
$headers =
"Content-Type: text/plain; charset=UTF-8\r\n"
. 'From: ' . $filterHeaderValue($_POST['name']) . "\r\n"
. 'Reply-To: ' . $filterHeaderValue($_POST['email']) . "\r\n"
. 'X-Mailer: PHP/' . phpversion()
;
mail($to, $subject, $message, $headers);
if ($_POST['copy'] == 'on') {
mail($_POST['email'], $subject, $message, $headers);
}
Php mail not sending arabic letters
As @Qirel said:
I'm not sure if ISO-8859-1 supports arabic, you might have to use
UTF-8 instead (and that means your entire code, including headers in
HTML/PHP/mail, should be set to UTF-8). I know UTF-8 supports it
anyway.
You can also check this post for confirmation.
PHP - Email with arabic characters
mb_internal_encoding("UTF-8");
$subject_evernote = mb_encode_mimeheader("العربية");
Related Topics
Mysqli + Xdebug Breakpoint After Closing Statement Result in Many Warnings
PHP Include Causes White Space at the Top of the Page
Updating Product Stock Programmatically in Woocommerce 3
Output Text File with Line Breaks in PHP
Pdo Error: SQLstate[Hy000]: General Error: 2031
Check If Row Exists in the Database Before Inserting
How to Run Django and PHP Together on One Apache Server
Redirecting from Http to Https with PHP
How to Display "12 Minutes Ago" etc in a PHP Webpage
File_Get_Contents When Url Doesn't Exist
Laravel - Database, Table and Column Naming Conventions
Laravel 5.1: Keep Uploaded File as Old Input
How to View/Open a Word Document in My Browser Using with PHP or HTML
Differencebetween PHP Require and Include