Problem with accent in e-mail form

1

Colleagues,

I have an e-mail form in PHP that does not correctly send accented words, several questions appear in words with accent, cedilla etc. I tried several things but none worked.

Can anyone give me strength? Below is the script I'm using. It is running on a Wordpress page template.

<?php
//If the form is submitted
if(isset($_POST['submitted'])) {
//Check to see if the honeypot captcha field was filled in
if(trim($_POST['checking']) !== '') {
    $captchaError = true;
} else {

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactName']) === '') {
        $nameError = 'Informe seu nome.';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['emaill']) === '')  {
        $emailError = 'Informe se endereço de e-mail.';
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['emaill']))) {
        $emailError = 'Informe um endereço de e-mail válido.';
        $hasError = true;
    } else {
        $emaill = trim($_POST['emaill']);
    }

    //Check to make sure comments were entered
    if(trim($_POST['comments']) === '') {
        $commentError = 'Escreva sua mensagem.';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    //If there is no error, send the email

    if(!isset($hasError)) {
        $emailTo = '[email protected]';
        $subject = '[Contato] '.$name;
        $sendCopy = trim($_POST['sendCopy']);
        $body = "Name: $name \n\nEmail: $emaill \n\nComments: $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emaill; 
        mail($emailTo, $subject, $body, $headers);

        if($sendCopy == true) {
            $subject = 'You emailed Your Name';
            $headers = 'From: Tanmay <[email protected]>';
            mail($emaill, $subject, $body, $headers);
        }

        $emailSent = true;

    }
}
} ?>

Thank you in advance.

    
asked by anonymous 19.09.2018 / 19:40

2 answers

0

This is very simple, you just need to set the encoding for: UTF-8

$mail->CharSet = "utf8";
    
19.09.2018 / 20:02
0

Use the following code:

$headers    = array
    (
        'MIME-Version: 1.0',
        'Content-Type: text/html; charset="UTF-8";',
        'Content-Transfer-Encoding: 7bit',
        'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
        'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
        'X-Mailer: PHP v' . phpversion(),
        'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
    );

Source: PHP Manual

Remembering that in order for the message not to be blocked you need to put in the from and in the return-path a valid email from your domain. Today, all Locaweb Linux servers use Postfix, and if you do not specify From at the time of sending the message, it forges from [email protected] (the host would be the name of the server where your site is hosted) , and this is blocked. That is, your message will not be sent.

I hope I have helped

    
19.09.2018 / 20:18