Problem with accentuation in email form

Asked

Viewed 561 times

2

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

Below is the script I’m using. It’s 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;

    }
}
} ?>

3 answers

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: Manual for PHP

Remember that in order for the message not to be blocked you need to put a valid email from your domain in the from and in the Return-path. Today, all Locaweb Linux servers use Postfix, and if you don’t specify From when 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’ve helped

0

This is very simple, just missing set the encoding to: UTF-8

$mail->CharSet = "utf8";

-2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.