I want to send an arabic email using php, but it gives me others characters. How to send it with the arabic characters? This is my code:
<?php
if( isset($_POST['name']) )
{
$to = 'support#alkramlaundry.qa'; // Replace with your email
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . '.';
$headers = 'From: ' . $_POST['name'] . "\r\n" . 'Reply-To: ' . $_POST['email'] . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
if( $_POST['copy'] == 'on' )
{
mail($_POST['email'], $subject, $message, $headers);
}
}
?>
you aren't specifying any Content-Type header so the recipient will use some default encoding
values appended to the $additional_headers parameter of the mail() 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);
}
By default, email (or the smtp protocol) only accepts the first 7 bits in ascii. To enable support for other languages add the following field to your headers:
Content-Type: text/plain; charset=UTF-8
Here is the RFC backing:
rfc5335
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=UTF-8\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
First thing try:
ini_set('default_charset', 'UTF-8');
Related
I have a local instance of sendmail running on my machine, and am using a php script in order to send custom messages in order to run an internal anonymous suggestions application.
I currently use the following script:
<?php
$to = '*to*';
$subject = '*subject*';
$message = '*message*';
$headers = 'From: *from*' . "\r\n" .
'Reply-To: *replyto*' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I would like to be able to support HTML, which I would assume was the following:
<?php
$to = '*to*';
$subject = '*subject*';
$message = '<html>here</html>';
$headers = 'From: *from*' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Contesnt-type:text/html;charset=UTF-8' . "\r\n";
$headers .= 'Reply-To: *replyto*' . "\r\n";
mail($to, $subject, $message, $headers);
?>
I do this via these functions:
private function plaintext_version($text) {
return "--PHP-nextpart-".$this->random_hash
."\nContent-Type: text/plain; charset=\"iso-8859-1\""
."\nContent-Transfer-Encoding: 7bit"
."\n$text";
} // EOF plaintext_version
private function HTML_version($html) {
return "--PHP-nextpart-".$this->random_hash."
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$html
";
} // EOF HTML_version
Here's the "Send" function. Note that the Content-Type is "multipart/alternative" and a "boundary" is specified; I create the boundary by concatenating "--PHP-nextpart-" with the output of a hashing function.
public function Send() {
if (strlen($this->cc_to)) $this->headers .='Cc: '.$this->cc_to . "\r\n";
if (strlen($this->bcc_to)) $this->headers .='Bcc: '.$this->bcc_to . "\r\n";
if (strlen($this->reply_to)) $this->reply_to = $this->mail_from;
$l_headers = 'From: '.$this->mail_from . "\r\n"
.'Reply-To: '.$this->reply_to. "\r\n"
.'X-Mailer: PHP/' . phpversion()."\r\n";
if (strlen($this->headers)) $l_headers .= $this->headers;
$l_headers .= "Content-Type: multipart/alternative; boundary=\"PHP-nextpart-".$this->random_hash."\"";
$mess = $this->plaintext_version($this->msg_text) . "\n\n". $this->HTML_version($this->html_version);
if (strlen($this->to_name)) {
$to = "\"".$this->to_name."\" <".$this->mail_to.">";
} else {
$to = $this->mail_to;
}
$sent = mail($to,$this->subject,$mess,$l_headers);
if ($sent) return true;
return false;
} //EOF Send()
I have such code for send email:
$email = $_POST['message_email'];
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
before that I check whether the email is correct so it has to be.
Then I send an email:
$sent = wp_mail($newTo, $subject, $companyName . "\n" . $email . "\n" . $phoneNumber . "\n" . strip_tags($message) . "\n" . $outputMail, $headers);
and it doesn't work. I've tried change headers to:
$headers = 'From: My Name <myname#mydomain.com>' . "\r\n\\";
and then it works. Why my code does't work? I need to provide that email retreived from form.
Try like this:
****Ive edited the code****
$from = "your#mail.com";
$to = "to#mail.com";
$message = "Message";
$subject = 'subject';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
if(mail($from, $subject, $message, $headers)){
//success
}else{
//error
}
I am using the mail function
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <>' . "\r\n";
$headers .= 'Cc: ' . "\r\n";
$mail_sent = #mail( $to, $subject, $message, $headers );
I want to pass the email-value for "From:" as a variable. Can this be done? And how can I pass other values like mobile number on to the mail?
Pass the email address($fromAddress) as a header
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$fromAddress.'>' . "\r\n".
'Reply-To:'.$fromAddress. "\r\n" .
$headers .= 'Cc: ' . "\r\n";
$mail_sent = #mail( $to, $subject, $message, $headers );
From example2 in the docs..
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I am new to PHP and send an email using following code
<?php
$to = 'to#xyz.com';
$subject = 'the subject';
$message = '<table dir="rtl"><tr><td>'. "\r\n";
$message .= '<b>This is Bold</b></br> <i> This is Italics</i></br>شطب 14 مرشحا لمجلس الأمة الكويتي'. "\r\n";
$message .= '</td></tr></table>';
//Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
$headers .= 'From: from#xyz.com' . "\r\n";
$headers .= 'Reply-To: replyto#xyz.com' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
ini_set ( "SMTP", "smtp.xyz.com" );
mail($to, $subject, $message, $headers);
?>
I can receive the email and it display the arabic also but it also show the from email address in the body of the email.
And other issue is that arabic is RTL even after mentioning in table dir-"RTL" it still show the message as LTR.
Example of Email Received
from#xyz.com
This is Bold
This is Italics
شطب 14 مرشحا لمجلس الأمة الكويتي
You could try using the alternative of CSS2 style:
style="direction:RTL; unicode-bidi:embed;"
Extra reading: http://www.i18nguy.com/markup/right-to-left.html
I got a strange behavior from the mail function in php
here is the code :
$header = "From: aa#aa.com\n";
$header .= "Reply-To: bb#bb.com\n";
$header .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n";
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";
$header .= "Content-Type: text/plain;charset=utf-8\n";
$send = mail($to,$subject,$message,$headers);
but the email i receive have a from address from the main admin of the server like : user123#s12panelboxmanage.com
why ?
Maybe it's because you set a variable $header, but pass to mail() variable $headers. If it's not the cause, try inserting \r\n instead of \n.
You should use the -f option in the mail function too to set the (valid) sender:
$header = 'MIME-Version: 1.0'."\n";
$header .= 'Content-type: text/'.$contentType.'; charset=iso-8859-1'."\n";
$header .= 'From: '.$from."\n";
$header .= 'Reply-To: '.$mailFrom."\n";
$header .= 'X-Mailer: PHP '.phpversion()."\n";
$header .= 'X-Sender-IP: '.$_SERVER['REMOTE_ADDR']."\n";
mail($to,$subject,$message,$header, "-f aa#aa.com");
<?php
$to = 'user#domain.com';
$subject = 'Subject';
$message = 'This is a test';
$headers = 'From: webmaster#yourdot.com' . "\r\n" .
'Reply-To: webmaster#yourdot.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>