First time using this platform, please be patient with me.
I have a contact form on my webpage. When I submit the message the body of the message that contains my Name, Phone, Subject, Message and Reply to appears in my mail as strange symbols.
I searched in the internet but the only thing found was: '=?UTF-8?B?' . base64_encode("Съобщение от сайта: " . $subject) . '?='; And it worked just for my subject. When I try to apply this for to my body, I get no success.
I will appreciate if someone can help me or give a direction how to fix this problem. Thank you in advance.
Enough said here is my code:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "Грешка; трябва да изпратите формата";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Име и имейл са задължителни.";
exit;
}
if(IsInjected($visitor_email))
{
echo "Невалиден имейл.";
exit;
}
$email_from = 'z3robot@...';//<== update the email address
$email_subject = '=?UTF-8?B?' . base64_encode("Съобщение от сайта: " . $subject) . '?='; "\n";
$email_body = "Name: $name. \n".
"Phone: $phone. \n".
"Subject: $subject. \n".
"The message is: $message. \n".
"Reply to: $visitor_email \n";
$to = "z3robot@...";//<== update the email address
$headers = "From: $email_from \r\n";
if(mail($to, $email_subject, $email_body, $headers)){
//if successful go to index page in 3 seconds.
echo "Съобщението е изпратено успешно. Ще бъдете прехвърлени на главната страница след 3 секунди.";
header("refresh:3; url=index.html");
}else{
//if not successful go to contacts page in 5 seconds.
echo "Съобщението НЕ е изпратено успешно. Ще бъдете прехвърлени отново в страница Контакти след 5 секунди.";
header("refresh:5; url=contact-us.html");
}
//done.
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}else{
return false;
}
}
?>
Thanks to all that make suggestions. I made a completely new file and used PHPMailer and everything works. Here is the code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Get data from form
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
//Preparing mail content
$messagecontent = "Име: <br><b>$name</b>" .
"<br><br>Телефон: <br><b>$phone</b>" .
"<br><br>Основание: <br><b>$subject</b>" .
"<br><br>Съобщение: <br><b>$message</b>" .
"<br><br>Имейл: <br><b>$visitor_email </b><br>";
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8'; //to convert chars to Cyrillic alphabet
try {
//Line for debugging if needed
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.mail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'mail@mail.com'; //SMTP username
$mail->Password = 'password'; //SMTP password
//$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //SMTP Encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('mail@mail.com', 'Mailer'); //Add visitors email and name $$
$mail->addAddress('mail@mail.com', 'text'); //Add a recipient
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = "Съобщение от сайта: " . $subject; //subject of the send email
$mail->Body = $messagecontent; //Content message of the send email
$mail->send();
header("Location: sent.html");
}
catch (Exception $e)
{
header("Location: not-sent.html");
//echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>