John Davidson

html - PHP mail function doesn't complete sending of e-mail

0 comments
Message:


<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = '[email protected]';
$subject = 'Customer Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>


I've tried creating a simple mail form. The form itself is on my index.html page, but it submits to a separate "thank you for your submission" page, thankyou.php, where the above PHP code is embedded.
The code submits perfectly, but never sends an email. How can I fix this?



My Answer:
The PHP mail() function is used to send email from a PHP script. Here is an example of how to use the mail() function in an HTML file:

html



Send Email



if(isset($_POST['submit'])){
$to = "[email protected]";
$subject = "Test Email";
$message = "This is a test email sent from a PHP script.";
$headers = "From: [email protected]";

if(mail($to, $subject, $message, $headers)){
echo "Email sent successfully.";
} else {
echo "Email sending failed.";
}
}
?>









In this example, when the user clicks the "Send Email" button, the form is submitted and the PHP code inside the HTML file is executed. The PHP code checks if the form has been submitted and then uses the mail() function to send an email to the specified recipient with the specified subject, message, and headers.

Please note that the PHP mail() function may not work on all servers, and you may need to configure your server to send emails using PHP. Additionally, it is important to sanitize user input and validate email addresses before using them in the mail() function to prevent security vulnerabilities.

Rate this post

3 of 5 based on 8892 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us