Skip to content
Advertisement

PHP Mailer languages encoding issues: It accepts only English

I am using php mailer at my website contact form. When i receive a message in greek language, i don’t receive the text as typed in the contact form. In class.phpmailer.php file line 59 the encoding is public $CharSet = 'iso-8859-1'; Is there a way to make my text appear correctly as typed in the contact form?

Languages comonly supported by ISO/IEC 8859-1 can be found here

I have also tried german and albanian languages but i also have the same problem. I can only receive english, if the user types another language on some words i receive “chinese”.

enter image description here

I get this message: enter image description here

The code:

     <?php
        require_once('phpmailer/class.phpmailer.php');

        // if(isset($_POST['g-recaptcha-response'])){
        if (empty($_POST['Email'])) {
            $_POST['Email'] = "-";
        }
        if (empty($_POST['Name'])) {
            $_POST['Name'] = "-";
        }
        if (empty($_POST['Subject'])) {
            $_POST['Subject'] = " ";
        }
        if (empty($_POST['message'])) {
            $_POST['message'] = "-";
        }




        $mymail = smtpmailer("example@gmail.com", $_POST['Email'], $_POST['Name'],
 $_POST['Subject'], $_POST['message']);
        function smtpmailer($to, $from, $from_name, $subject, $body)
        {
            $mail = new PHPMailer;
            $mail->isSMTP();
            $mail->Debugoutput = 'html';
            $mail->Host        = 'smtp.gmail.com';
            $mail->Port        = 587;
            $mail->SMTPSecure  = 'tls';
            $mail->SMTPAuth    = true;
            $mail->Username    = 'example@gmail.com';
            $mail->Password    = 'pass';
            $mail->SetFrom($from, $from_name);
            $mail->Subject  = " Contact form ~Name: $from_name ~ subject: $subject  ";
         $mail->Body = " You have received a new message 
          from $from_name, here are the details:nn_____
          ___________________n" . "nDear $from_name,n
      Your enquiry had been received on " . date("D j F ") . "
       nINFORMATION SUBMITTED: " . "nnName: $from_namennEmail: $from
        nSubject: $subjectnnMessage: $body nnTo:
         $tonDate: " . date("d/m/y") . "nWebsite: " . "n____________
        __________________"; //end body


        $mail->AddAddress($to);
        //send the message, check for errors
        if (!$mail->send()) {
          echo "Mailer Error: " . $mail->ErrorInfo;

         } else {
       echo "Well done $from_name, your message has been sent!n
    We will reply to the following email: $from" . "nYour Message: $body";
            }




        } //end function smtpmailer
        //}


        ?>

Advertisement

Answer

In your example output, the char count amplification suggests that you’re receiving data from your form in UTF-8, but are then telling PHPMailer (by default) that it’s ISO-8859-1, which results in the kind of corruption you’re seeing.

You should be using UTF-8 everywhere. In PHPMailer you do it like this:

$mail->CharSet  = 'UTF-8';

Then you need to be sure that every step of your processing supports UTF-8 as well.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement