简体   繁体   中英

PHP send mail with question marks although using utf-8

Using the following php file to send mail. But the text that is not English inside the mail body get as ???? and the $subject of the mail get as "‚º ëÃ"

what do I need to check/add/change in order to fix it?

<?php include('config.php');
header('Content-Type: text/html; charset=utf-8');

class messenger
{

    public $name;
    public $number;
    public $email;
    public $address;
    public $category;
    public $tool;

    /*----------user registration start--------------*/
    function signup()
    {

        $name = $_REQUEST['name'];
        $number = $_REQUEST['number'];
        $email = $_REQUEST['email'];
        $address = $_REQUEST['address'];
        $category = $_REQUEST['category'];
        $tool = $_REQUEST['tool'];
        $datetime = date("Y-m-d H:i:s");

        //Generate the email message:
        $to = 'someEmailAddress@gmail.com';
        $subject = "הזמנת חדשה";
        $msg = "In time: " . $datetime . "\n New Power Tool Rental: \n\n            Name: " . $name . "\r\n            Email: " . $email . "\n            Mobile Number: " . $number . "\n            Address: " . $address . "\n            Category: " . $category . "\n            Tool: " . $tool . "\n\n Regards\n " . $name . " \n " . $email;
        $headers = "From:  $email" . "\r\n";
        $headers .= "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type: text/plain; charset=UTF-8";
        $headers .= "X-Mailer: PHP/" . phpversion();

        //Send the email
        mail($to, $subject, $msg, $headers);

        //Enter data to the SQL table
        $sql = "INSERT INTO user (name, number, email, address, category, tool, dateTime)
                 VALUES ('$name', '$number', '$email', '$address', '$category', '$tool','$datetime')";

        //Check if entered to SQL and if Yes send back in json message
        $result = mysql_query($sql);
        if (!$result) {
            $message["result"] = "Problemmm";
        } else {
            $mysql_insert_id = mysql_insert_id();       //get the new ID number from sql table
            $message["id"] = $mysql_insert_id;          //add the new ID data to the response message
            $message["name"] = $_REQUEST['name'];
            $message["number"] = $_REQUEST['number'];
            $message["email"] = $_REQUEST['email'];
            $message["address"] = $_REQUEST['address'];
            $message["result"] = "successfully";
        }

        //Send back the json message
        echo json_encode($message);
        die;
    }
    /*----------user registration end--------------*/
}

?>

just tried to change the subject to:

$subject = "הזמנת כלי להשכרה";
$encSubject = '=?UTF-8?B?'.base64_encode($subject).'?=';

now I get the subject of the mail as: ú é

Setting the headers is not enough.

Both subject (must be RFC 2047 ) and message (must be UTF-8 in your case) have also be in their respecive correct encoding, the php:: mail doc + user-notes covers most of what you miss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM