简体   繁体   中英

the html form - php function doesn

I want to send an email from the HTML form using PHP but the email does not display the incoming email even though the code is no longer wrong.

   <?php
include("library/config.php");
// Check for empty fields
if(empty($_POST['name'])      ||
   empty($_POST['address'])   ||
   empty($_POST['email'])     ||
   empty($_POST['subject'])   ||
   empty($_POST['pesan'])   ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
   return false;
   }

$name  = strip_tags(htmlspecialchars($_POST['name']));
$address = strip_tags(htmlspecialchars($_POST['address']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$subject = strip_tags(htmlspecialchars($_POST['subject']));
$pesan = strip_tags(htmlspecialchars($_POST['pesan']));

// Create the email and send the message
$to = 'marketing@haradeco.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name and $subject";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nAddress: $address\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@haradeco.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";   
if(mail($to,$email_subject,$email_body)){
    header("location:dashboard.php?pag=contact&message=Successfully");
}else{
   header("location:dashboard.php?pag=contact&message=error");
}
return true;
?>

the result always show error in this message.

if there is one that needs to be changed next to which one has to be changed. I have tried to add, if the button you press will do the code inside the PHP. But it doesn't work again.

You have to add the $headers variable to your mail command:

if(mail($to,$email_subject,$email_body,$headers)){

Your headers also lack the correct end-of-line characters: add "\\r\\n" to each line.

A complete example for a plaintext mail would be:

$headers = 'From: webmaster@example.com' . "\\r\\n" .
'Reply-To: webmaster@example.com' . "\\r\\n" .
'Content-type: text/plain; charset=UTF-8'."\\r\\n".
'MIME-Version: 1.0'."\\r\\n".
'X-Mailer: PHP/'.phpversion();

Note that the reply-to address must be set with an additional parameter:

mail($to,$email_subject,$email_body,$headers,'-freplyto@webmaster.com')

Also check out the manual: https://www.php.net/manual/en/function.mail.php

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