<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$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 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. please help.
Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled
This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.
If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.
Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.
I think this should do the trick. I just added an
if(isset
and added concatenation to the variables in the body to separate PHP from HTML.Try these two thigs separately and together:
if($_POST['submit']){}
$from
(just my gut)If you only use the
mail()
function, you need to complete the config file.You need to open the mail expansion, and set the
SMTP smtp_port
and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a
.txt
file in a folder namedmailoutput
.In the case if you are using a free hosing service like
000webhost
orhostinger
, those service providers disable themail()
function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference, PHP mail()
To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)
Hope this helped.