I have this contact form on my contact page. It works correctly with attachment. But if there's not attachment file I got the e-mails with incorrect characters for example: ". KĂśrĂźlbelĂźl 35 oldalrĂłl lenne szĂł. " I need the e-mails in hungarian, charset-utf-8. where should I write in it? I'm trying to learn the php language, but please if you have an idea where i am wrong, please tell me. Here is my code:
<?php
$statusMsg='';
if(isset($_FILES["file"]["name"])){
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$captcha;
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Kérjük pipáld be, hogy nem vagy robot!</h2>';
exit;
}
$secretKey = "6LcsouEUAAAAAHNvPVT_dANY7m0u7yJoGT68hqhW";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo '<h2>Köszönjük az üzenetet! Hamarosan jelentkezünk</h2>';
} else {
echo '<h2>You are spammer!</h2>';
}
$fromemail = $email;
$subject = $subject;
$email_message = '<h2>Új ajánlatkérés érkezett</h2>
<p><b>Név:</b> '.$name.'</p>
<p><b>Email:</b> '.$email.'</p>
<p><b>Tárgy:</b> '.$subject.'</p>
<p><b>Üzenet:</b><br/>'.$message.'</p>';
$email_message.="Melléklet lehet csatolva. Az üzenet az amidit.hu-n keresztül érkezett.";
$semi_rand = md5(uniqid(time()));
$headers = "From: ".$fromemail;
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
if($_FILES["file"]["name"]!= ""){
$strFilesName = $_FILES["file"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: application/octet-stream;\n" .
" name=\"{$strFilesName}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$strContent .= "\n\n" .
"--{$mime_boundary}--\n";
}
$toemail="info@amidit.hu";
if(mail($toemail, $subject, $email_message, $headers)){
$statusMsg= "Az üzenet küldése sikeres.";
}else{
$statusMsg= "Az üzenetet nem sikerült elküldeni. Kérjük, próbáld meg újra.";
}
}
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Köszönjük az üzenetet. Hamarosan jelentkezünk!');
window.location = 'https://amidit.hu';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Köszönjük az üzenetet. Hamarosan jelentkezünk!');
window.location = 'https://amidit.hu';
</script>
<?php
}
?>
Thanks for your help!
you can add "utf-8" on the Email Header section Like below.
$headers = "Content-Type: text/html; charset=UTF-8";
Several things to check:
Next, bear in mind that constructing email messages correctly is very difficult — using the PHP mail()
function directly should be avoided whenever possible. Instead, use a library, such as PHPMailer that you tagged this question with, which will take care of many things for you, and in that you just need to set:
$mail->CharSet = 'utf-8';
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.