简体   繁体   中英

PHP emails from form scramble cyrillic characters

Here's what this is all about. Since I am using PHP as of today I am facing this issue. I have build a form , for the ISP company that I work for, which emails the form results. Customers fill out this form to request new service from us. Since the company is foreign (Bulgarian) 99% of people are going to fill it out in Cyrillic , so I tested it by filling it out myself and I got this email.


From: ÐÑд

Phone number: 56

Selected Services:

Internet: 100 || TV: comfort

Message:

ЪЕÐÐÐÐÐÐ


So I made some research , checked out a few other posts about this in StackOverflow but none worked... most suggested to add $header .= "\\nContent-type: text/plain; charset=\\"utf-8\\"";

One issue is that I'm not sure EXACTLY how to implement this solution, second is that it didn't work for the person who asked the question, not even in one of the questions I reviewed.

Here is the PHP code and the HTML one, hoping you can give me an EXACT way of fixing it, since I have barely no idea of syntax in PHP.

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Online Request</title>
<meta content="php, contact, form, thinking" name="keywords">
<meta content="Contact us and let us know if we can help you out further." name="description">

<style>
input, textarea {
    color: #fff;
    padding: 5px;
    margin: 10px;
    font-family: Cambria, Cochin, serif;
    font-size: medium;
    font-weight: bold;
    outline: none;
}
p {
    font-family: Cambria, Cochin, serif;
    font-size: large;
    margin-bottom: -5px;
}
input[type=text], textarea {
    width: 350px;
    background-color: #000;
    border: 1px solid #000;
}
input[type=submit] {
    width: 100px;
    background-color: #710000;
    border: 1px solid #000;
    font-size: large;
    color: #FFFFFF;
}
input[type=submit]:hover {
    background-color: #990000;
    cursor: pointer;
}
input[type=submit]:active {
    background-color: #4A6F00;
}

h1 {
  text-size: 26px;
  font-family: Arial;
  text-shadow:
  -1px -1px 0 #000,
  1px -1px 0 #000,
  -1px 1px 0 #000,
  1px 1px 0 #000;
  color: #ff0000;}
body {
    padding: 10px;
    background-color: #F4F4F4;
}
.checkbox {
  font-family: Cambria, Cochin, serif;
  font-size: large;
  margin-bottom: -5px;
}
</style>

</head>

<body>
    <h1>Online Request</h1>
    <form action="emailer.php" method="POST" accept-charset="UTF-8">
        <div>
            <p>Имена</p>
            <input name="name" type="text"> <br>
        </div>
        <div>
            <p>Телефон за връзка</p>
            <input type="text" name="number" min="10" max="10">
            <br>
        </div>
        <div>
            <p> Вид услуга - Internet </p> <br/>
            <input name="servicetypeINT" type="radio" value="30"><span class="checkbox"> Internet Value - 30Mpbs </span> <br/>
            <input name="servicetypeINT" type="radio" value="60"><span class="checkbox"> Internet Mania - 60 Mbps </span><br/>
            <input name="servicetypeINT" type="radio" value="100"><span class="checkbox"> Internet Extreme - 100Mbps</span> <br/>
            <input name="servicetypeINT" type="radio" value="150"><span class="checkbox"> Internet Pro - 150Mpbs</span><br/>
        </div>
        <hr/>
        <div>
            <p>Вид услуга - Телевизия</p> <br/>
            <input name="servicetypeTV" type="radio" value="anal"><span class="checkbox"> Аналогова телевизия - 50 канала </span> <br/>
            <input name="servicetypeTV" type="radio" value="start"><span class="checkbox"> Start TV - 40+ Цифрови канала </span><br/>
            <input name="servicetypeTV" type="radio" value="comfort"><span class="checkbox"> Confort TV - 160+ Цифрови канала</span> <br/>
            <input name="servicetypeTV" type="radio" value="mania"><span class="checkbox"> Mania TV - 160+ Цифрови / 20+ HD канала</span><br/>
        </div>
            <div>
            <p>Comment</p>
            <textarea cols="30" name="comment" rows="9"></textarea>
            <br> </div>
        <div>
        <input name="submit" type="submit" value="Изпрати"> </div>
    </form>
</body>

</html>


<?php

if(isset($_POST['submit'])) {
$to = "denislav@svishtov.net";
$subject = "New Internet and/or TV request";

// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$phone_field = filter_var($_POST['number']);
$selectedINT_field = filter_var($_POST['servicetypeINT']);
$selectedTV_field = filter_var($_POST['servicetypeTV']);
$comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);


//constructing the message
$body = "
From: $name_field\n\n
Phone number: $phone_field\n\n
Selected Services:\n\nInternet: $selectedINT_field || TV: $selectedTV_field \n\n
Message:\n\n $comment ";

// ...and away we go!
mail($to, $subject, $body);

// redirect to confirmation
header('Location: confirmation.html');
} else {
// handle the error somehow
echo "Грешка в попълването на формата";
}
?>

Another interesting fact is that when I opened the file from my local machine (since if I try from the one hosted online, I will get the error echo) the Cyrillic text in the php code itself was displayed just like the email result... But when I opened it from the host , by directly addressing it I got the error message right... in Bulgarian... without any gibberish.

The line of code in the PHP

header('Content-Type: text/html; charset=utf-8');

seemed to convert the header message. I tried addressing $name_field('Content-Type: text/html; charset=utf-8');

But result was blank , the php didn't function properly, I'm guessing since I gave it a wrong or inoperable line of code with that addressing of $name_field .

Question is, how do I address it properly and I'm I even on the right track with this approach?

FIXED added $headers content type at the beginning of the php code

<?php
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text; charset=utf-8' . "\r\n";

and included $headers in the MAIL function

mail($to, $subject, $body, $headers);

Took me four hours but thats how it is in the beginning. Anyway, thanks for the help, or the lack of such. Have a good day and hope this one helps someone

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