Search code examples
phpajaxemailaccent-sensitive

My mail function return success and the mail is not received when there is french accent in the message


So I've got a script that send a mail from a form on my web page. The user simply put his mail address, the message and his name.

First I send the mail to my contact address. (This is working fine) And after that I send a mail to the user's mail address. (This in not working)

And the last mail function return a success but the user doesn't got a mail.

I tried to log everything and I can't figure out why the mail is probably sent but not received.

The mail is not sending when I have a french accent in the body like "répondons" but it works when it's just "repondons". I don't understand why, but It would be better with accent

<?php
    // site owner
    $site_name = 'just-request.fr';
    $sender_domain = 'contact@just-request.fr';
    $to = 'contact@just-request.fr';


    // contact form fields
    $name = trim( $_POST['name'] );
    $email = trim( $_POST['email'] );
    $subject = trim( $_POST['subject'] );
    $message = trim( $_POST['message'] );


    // check for error
    $error = false;

    if ( $name === "" ) { $error = true; }

    if ( $email === "" ) { $error = true; }

    if ( $subject === "" ) { $error = true; }

    if ( $message === "" ) { $error = true; }


    // if no error, then send mail
    if ( $error == false )
    {
        $body = "Name: $name \n\nEmail: $email \n\nMessage: $message";

        $headers = "From: " . $site_name . ' <' . $sender_domain . '> ' . "\r\n";
        $headers .= "Reply-To: " . $name . ' <' . $email . '> ' . "\r\n";

        $mail_result = mail( $to, utf8_decode($subject), utf8_decode($body), $headers );

        if ( $mail_result == true )
        {
            $body = "Bonjour,\n\n";
            $body .= "Merci de votre mail, nous le prenons en compte et vous repondrons des que possible.\n\n";
            $body .= "Cordialement,\n";
            $body .= "L'equipe Request. ";

            $subject = "Réponse automatique";

            $new_mail_result = mail( $email, utf8_decode($subject), utf8_decode($body), $headers );

            if ( $new_mail_result == true )
            {
                echo 'success';
            }
            else
            {
                echo 'unsuccess';
            }
        }
        else
        {
            echo 'unsuccess';
        }
    }

    else
    {
        echo 'error';
    }


Solution

  • Try to use mb_send_mail() instead of mail().
    Set mb_language() to German/de (ISO-8859-15) or English/en (ISO-8859-1).
    Both ISO-8859-15 and ISO-8859-1 include french extra letters.
    ISO-8859-15 is ISO-8859-1 after Euro upgrade.

    https://www.php.net/manual/en/function.mb-send-mail.php

    Sends email. Headers and messages are converted and encoded according to the mb_language() setting. It's a wrapper function for mail(), so see also mail() for details.


    Comment: I miss mb_language() option for UTF-8 with quoted-printable encoding.
    It would be nice for most (european) "almost ASCII" language specific alphabets.