2

I've been working on a website recently and decided to include a PHPMailer on it. The problem is I'm hungarian and the site is fully in hungarian too so I had to use UTF-8 charset. The PHP mailer I decided to use is not yet capable of using UTF-8 and I cant figure out how to make it echo things in my prefered charset. Please help!

<?php

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

$mail = new PHPMailer();

if( isset( $_POST['template-contactform-submit'] ) AND $_POST['template-contactform-submit'] == 'submit' ) {
    if( $_POST['template-contactform-name'] != '' AND $_POST['template-contactform-email'] != '' AND $_POST['template-contactform-message'] != '' ) {

        $name = $_POST['template-contactform-name'];
        $email = $_POST['template-contactform-email'];
        $phone = $_POST['template-contactform-phone'];
        $service = $_POST['template-contactform-service'];
        $subject = $_POST['template-contactform-subject'];
        $message = $_POST['template-contactform-message'];

        $subject = isset($subject) ? $subject : 'New Message From Contact Form';

        $botcheck = $_POST['template-contactform-botcheck'];

        $toemail = ''; // Your Email Address
        $toname = ''; // Your Name

        if( $botcheck == '' ) {

            $mail->SetFrom( $email , $name );
            $mail->AddReplyTo( $email , $name );
            $mail->AddAddress( $toemail , $toname );
            $mail->Subject = $subject;

            $name = isset($name) ? "Név: $name<br>" : '';
            $email = isset($email) ? "E-mail: $email<br><br>" : '';
            $message = isset($message) ? "Üzenet: $message<br>" : '';

            $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>Az üzenetet továbbította: ' . $_SERVER['HTTP_REFERER'] :'';

            $body = "$name $email $message $referrer";

            $mail->MsgHTML( $body );

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

            $sendEmail = $mail->Send();

            if( $sendEmail == true ):
                echo 'Üzenet elküldve!';
            else:
                echo 'Üzenetküldés sikertelen. Kérlek próbáld újra!' . $mail->ErrorInfo . '';
            endif;
        } else {
            echo 'Bot vagy. Kérlek próbálkozz újra!';
        }
    } else {
        echo 'Kérlek tölts ki minden mezőt és próbáld meg újra!';
    }
} else {
    echo 'Valami félrecsúszott. Kérlek próbáld meg mégegyszer.';
}

?>
Zsolt Bíró
  • 57
  • 1
  • 1
  • 7
  • PHPMailer has no trouble with UTF-8 - it's more likely that you have an environment problem that's messing it up. You don't actually say what the problem is or how/where it appears. One thing - `msgHTML` does quite a lot that may be dependent on the charset, so set the `CharSet` property before calling it. Also make sure you're using latest PHPMailer - I can see that you've based your code on an old example. – Synchro Jul 22 '15 at 10:54
  • The problem is when the mailer is called and the mail is sent (or an error accures) theres a jquery code on the site that prints out the corresponding echo from the mailer and its full of "?" characters. Can you show me an example of the latest mailer or can you link it pls? – Zsolt Bíró Jul 22 '15 at 11:27
  • OK, so the corrupted chars are on the web site, not in the email? If so, check that your site is setting the correct Charset in the Content-type header and that your editor is using UTF-8 too. [PHPMailer lives here](https://github.com/PHPMailer/PHPMailer), you'll find examples in the examples folder. – Synchro Jul 22 '15 at 12:17
  • They are in the echo-s and the mail too at $name, $email, $message, $referrer where the original one Sent the mail like this "Name: George" i want it to send the hungarian equivalent "Név: George" and it sends it like this: "N�v: George" also the echos display like this: "�zenet elk�ldve!" instead of "Üzenet elküldve!" ("Message sent!" in english). Thx for the link. Gonna brows it through. – Zsolt Bíró Jul 22 '15 at 12:32
  • OK, so it sounds like you have a more widespread problem and UTF-8 chars are not rendering correctly outside PHPMailer either. – Synchro Jul 22 '15 at 12:35
  • I dont think that the problem stands with the site or the javascript. I tried it without the jquery and the problem still stands. The mail is sent and the Message sent echo comes back (in hungarian ofc) but all the special chars aren't rendered and it looks like this. http://i.imgur.com/sLIvkmN.png – Zsolt Bíró Jul 22 '15 at 12:51
  • If I visit that URL, the text contains corrupt chars. The content-type header is correctly specifying UTF-8. If I tweak the encoding at the client end, I can see that the text is encoded as ISO-8859-1, not UTF-8. I suspect that your editor is at fault - your source file is not in the correct charset. – Synchro Jul 22 '15 at 14:51
  • Just realized it 30seconds ago that DW was set to default encoding.................... FFS! Around 5 hours of headache for nothing and now it works perfectly... Thanks for your answers though at least i updated to the latest phpmailer because of this :D I have another question though. Is there any possible way to make this code spam proof? Because every email i send ends up in the spam directory. – Zsolt Bíró Jul 22 '15 at 15:07
  • Glad you fixed it - it's probably worth deleting this question as it's a local config issue and unlikely to be that useful to others. – Synchro Jul 22 '15 at 15:09
  • Is there any possible way to make this code spam proof? Because every email i send ends up in the spam directory. – Zsolt Bíró Jul 22 '15 at 15:22
  • No simple way, but you should look into using SPF, DKIM, making sure your IPs resolve properly, not forging from address etc. It's all system/network config stuff rather than anything within PHPMailer. – Synchro Jul 22 '15 at 16:37
  • Oki! Thanks for your time. Actually i worked it around with gmail filters so its rather makeshift but it works! – Zsolt Bíró Jul 22 '15 at 16:41

2 Answers2

2

Try replace:

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

by:

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

It's working perfectly here.

Wesley
  • 169
  • 2
  • 7
-1

Something usefull is decode the content by utf8_decode() php function previous to send it.

renevillegasr
  • 589
  • 4
  • 6