I’ve been trying to get the form to send me an email after someone submits it containing the information the person put in it at the moment my code doesn’t work and i can’t figure out how to fix it. My code is below.
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
Is the issue you’re having that the email is not being sent? PHP’s mail function uses the sendmail facility to actually handle the processing of the outgoing email (this is how it’s done on Linux/Mac; I don’t know about on Windows, but I imagine it would do something similar). Do you have an emailing service such as sendmail configured on your test environment?
That depends on your setup. If you’re working on Linux/Mac, you should install and use sendmail. On Windows, according this this Stack Overflow answer, you need to configure an SMTP service. If you use GMail, I’ve been able to use my GMail account to send outgoing emails (I was using Swift Mailer when I did it, but that shouldn’t matter). To use SMTP, it looks like you’ll have to make some changes to your php.ini, and it looks like the settings you need to set them to to work with GMail are explained on this page.
What I think @mittineague was getting at earlier is that a lot of ISPs mail servers won’t allow you to use a “from” address for a domain that isn’t configured on their servers. So if the person who fills in your form has a “gmail” address, unless your ISPs mail server is configured to send emails from that domain, it may well reject it. Basically allowing stuff like that is called “open relay”, was used a lot by spammers and therefore frowned upon now. You should send the email from one of your own email addresses, and set the “reply-to” header so you can just click “reply” if you need to.
If you’re on Linux, you almost certainly have it in your distro’s repositories. On Debian/Ubuntu based distros, something like this should work:
sudo apt-get install sendmail
sudo yum install sendmail
It looks like MacOS uses postfix for it’s sendmail implementation, which should be installed already.
Any of these choices will require some configuration, and you still haven’t said what your environment is. Just know that you’ll need an email service installed (which should be easy enough to figure out how to do with a little searching), and then configure your php.ini to point to that local service to implement the mail function.
OK. So if you’ve got postfix running on your system, you should just need to configure php to use it to send mail. For this, you’ll need to edit your php.ini file (I’m not sure where it’s at on MacOS, but you can find out by doing something like this:
php -i | grep -i Configuration
This should show you which php.ini file is being read.
Also, real quick, there may be more than one php.ini on your system (this is the case for Debian-based distros), and the one read by the web server may be different than the one used for your CLI. You can use the phpinfo function in a web-accessible page to output the configuration and find out which php.ini is being read by the web server.