PHP mailer Character encoding issue - php

I m facing some character encoding issues like this:
D\\\'Huison-Longueville Référence
Original Text was :
D'Huison-Longueville Référence
Here is my PHP Mailer script:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsMail();
$mail->Subject = $subject;
$mail->IsHTML(true);
$mail->Body = $email_body;
$mail->From='support#xyz.com';
$mail->FromName= "Support Team";
$mail->AddAddress($toEmail);
$mail->Send();

I had the same problem. The charset was not being changed although I was specifying the new one like you with:
$mail->CharSet = 'UTF-8';
Since you use "IsMail" you have to go to the folder where class.phpmailer.php resides (generally in the same folder as the main phpmailer.php file) and edit it. There you will see the default values for the mail and you will see that it is set to "ISO-8859-1".
Change
public $CharSet = 'ISO-8859-1';
to
public $CharSet = 'UTF-8';
Change it to "UTF-8" or any other charset you want.

Related

PHPMailer - adding attachment breaks charset

I've got a simple e-mail being sent from my script using PHPMailer:
include_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsMail();
$mail->IsHTML(true);
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->Body = $content;
the above is sent perfectly, even if both subject and body contain foreign characters.
However if I add an attachment:
$mail->AddAddress($email);
$mail->AddAttachment("file.pdf");
$mail->Subject = $subject;
the characters are replaced by ÅÄ
I've been using PHP Mailer for years but it's the first time I have to use both attachments and foreign characters in the message. Strangely the subject is fine, just the body of the e-mail is broken.
Has anybody experienced same/similar problem?

PHPMailer: Subject name in arabic (non-English) characters

I am using PHPMailer API for sending emails. I was wondering how can I send the subject in Arabic (non-English) language
$mail->CharSet = 'utf-8';
$array= FetchTable('cos');
$subject = $_POST['subject'];
$body = $_POST['body'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "host";
$mail->SMTPAuth = true;
$mail->Username = "yaz#enfaltourism.com";
$mail->Password = "*******";
$mail->Port = "587";
$mail->From = "yaz#enfaltourism.com";
$mail->FromName = "Enfaltourism";
$mail->Subject = $subject;
$mail->AddAddress($email);
$mail->Send();`
The email is being successfully sent, but the problem is in sending subject in Arabic language. The email message body is displayed properly in Arabic after I set the char encoding but the the subject is being displayed in weird characters
Update
include("../mail/class.phpmailer.php");
$array= FetchTable('cos');
$subject = $_POST['subject'];
$body = $_POST['body'];
$mail = new PHPMailer();
$mail->CharSet = 'utf-8';
$mail->IsSMTP();
$mail->Host = "mail.enfaltourism.com";
$mail->SMTPAuth = true;
$mail->Username = "yaz#enfaltourism.com";
$mail->Password = "*****";
$mail->Port = "587";
$mail->From = "yaz#enfaltourism.com";
$mail->FromName = "Enfaltourism";
$mail->Subject = $subject;
i fixed as u told but the subject still weird character like this
I had this same problem with Japanese characters, both in the body and in the subject line:
I resolved it (email looks outstanding now) by doing:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
There really isn't any need to encode the subject separately so long as the object has the CharSet property set to UTF-8.
you can try using this code. Let me know if it helps
$phpmailer->Subject = "=?UTF-8?B?".base64_encode($subject)."?=";
Try encoding subject with base64_encode
$subject = '=?UTF-8?B?'.base64_encode('سلام علیکم').'?=';
However with setting the issue must be solved.
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
Try this:
$mail->CharSet = 'UTF-8';
$mail->Subject = html_entity_decode("Recuperación de contraseña");
I did use this in my code and it worked!
Results

How to save mail body as html

I am looking for a way to save my template file which I use as a certificate snf sttsch it to my my.
Below is the code to replace fields in my template with one I have queried.
require("class.phpmailer.php");
//placeholders array
$pholders = array('replace_name', 'replace_number', 'replace_title');
//Certificate replace values array
$failedValues = array($FullName, $TestNo, $Title);
$mail_body = file_get_contents("failed.html");
$mail_body = str_replace($pholders,$failedValues,$mail_body);
//mailer
$mail = new phpmailer;
$mail->IsSMTP(); // set mailer to use SMTP
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Results";
$mail->Body = $mail_body;
$mail->AddAttachment("cpd.html");
I have omitted code for the smtp setting. The file cpd.html has to be generated and attached. I am looking for a way on how to generate it and save it for attaching it to the mail.

PHPmailer sending HTML CODE

I am using PHPmailer to send email.
I have also used HTML = True content type
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = $Host;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password;
$mail->From = $From;
$mail->FromName = $FromName;
$mail->AddAddress($To , $ToName);
$mail->WordWrap = 50; // set word wrap
$mail->Priority = 1;
$mail->IsHTML(true);
$mail->Subject = $Subject;
$mail->Body = $Body;
Once the email is sent i see the actual HTML code instead of the contents please check below
**Not sure what is the issue **
Calling the isHTML() method after the instance Body property (I mean $mail->Body) has been set solved the problem for me:
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->IsHTML(true); // <=== call IsHTML() after $mail->Body has been set.
There is msgHTML() method, which also call IsHTML().
The name IsHTML is confusing...
/**
* Create a message from an HTML string.
* Automatically makes modifications for inline images and backgrounds
* and creates a plain-text version by converting the HTML.
* Overwrites any existing values in $this->Body and $this->AltBody
* #access public
* #param string $message HTML message string
* #param string $basedir baseline directory for path
* #param bool $advanced Whether to use the advanced HTML to text converter
* #return string $message
*/
public function msgHTML($message, $basedir = '', $advanced = false)
or if you have still problems you can use this
$mail->Body = html_entity_decode($Body);
In version 5.2.7 I use this to send plain text:
$mail->set('Body', $body);
do like this-paste your html code inside your separate html file using GET method.
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->addAttachment= $_GET['addattachment']; $mail->AltBody
=$_GET['AltBody']; $mail->Subject = $_GET['subject']; $mail->Body = $_GET['body'];
just you need to pass true as an argument to IsHTML() function.
all you need to do is just add $mail->IsHTML(true); to the code it works fine..

PHPMailer character encoding issues

I try to use PHPMailer to send registration, activation. etc mail to users:
require("class.phpmailer.php");
$mail -> charSet = "UTF-8";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mydomain.org";
$mail->From = "name#mydomain.org";
$mail->SMTPAuth = true;
$mail->Username ="username";
$mail->Password="passw";
//$mail->FromName = $header;
$mail->FromName = mb_convert_encoding($header, "UTF-8", "auto");
$mail->AddAddress($emladd);
$mail->AddAddress("mytest#gmail.com");
$mail->AddBCC('mytest2#mydomain.org', 'firstadd');
$mail->Subject = $sub;
$mail->Body = $message;
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
The $message contains latin characters. Unfortunately all webmail (gmail, webmail.mydomain.org, emailaddress.domain.xx) is using a different coding.
How can I force to use UTF-8 coding to show my mail exactly the same on all mailboxes?
I tried to convert the mail header width mb_convert_encoding(), but without luck.
If you are 100% sure $message contain ISO-8859-1 you can use utf8_encode as David says. Otherwise use mb_detect_encoding and mb_convert_encoding on $message.
Also take note that
$mail -> charSet = "UTF-8";
Should be replaced by:
$mail->CharSet = "UTF-8";
And placed after the instantiation of the class (after the new). The properties are case sensitive! See the PHPMailer doc fot the list & exact spelling.
Also the default encoding of PHPMailer is 8bit which can be problematic with UTF-8 data. To fix this you can do:
$mail->Encoding = 'base64';
Take note that 'quoted-printable' would probably work too in these cases (and maybe even 'binary'). For more details you can read RFC1341 - Content-Transfer-Encoding Header Field.
$mail -> CharSet = "UTF-8";
$mail = new PHPMailer();
line $mail -> CharSet = "UTF-8"; must be after $mail = new PHPMailer();
try this
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
I work myself this way
$mail->FromName = utf8_decode($_POST['name']);
http://php.net/manual/en/function.utf8-decode.php
When non of the above works, and still mails looks like ª הודפסה ×•× ×©×œ:
$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->Subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';;
Sorry for being late on the party. Depending on your server configuration, You may be required to specify character strictly with lowercase letters utf-8, otherwise it will be ignored. Try this if you end up here searching for solutions and none of answers above helps:
$mail->CharSet = "UTF-8";
should be replaced with:
$mail->CharSet = "utf-8";
I was getting ó in $mail->Subject /w PHPMailer.
So for me the complete solution is:
// Your Subject with tildes. Example.
$someSubjectWithTildes = 'Subscripción España';
$mailer->CharSet = 'UTF-8';
$mailer->Encoding = 'quoted-printable';
$mailer->Subject = html_entity_decode($someSubjectWithTildes);
Hope it helps.
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "16bit";
The simplest way and will help you with is set CharSet to UTF-8
$mail->CharSet = "UTF-8"
To avoid problems of character encoding in sending emails using the class PHPMailer we can configure it to send it with UTF-8 character encoding using the "CharSet" parameter, as we can see in the following Php code:
$mail = new PHPMailer();
$mail->From = 'midireccion#email.com';
$mail->FromName = 'Mi nombre';
$mail->AddAddress('emaildestino#email.com');
$mail->Subject = 'Prueba';
$mail->Body = '';
$mail->IsHTML(true);
// Active condition utf-8
$mail->CharSet = 'UTF-8';
// Send mail
$mail->Send();

Categories