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.
My Php:
<?php
if(isset($_POST['submit'])){
$to = "youremailhere@email.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($to,$subject,$message,$headers);
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.
}
?>
From my own past experience, I’ve had both some email clients reject emails because of incorrect from values, and BCC added to form inputs.
Even if you want to risk emails not being delivered, you should tighten the code up. IMHO, don’t try to block things, concentrate on using only what you want to accept.
The first change I would make is to not test for POST submit, but $_SERVER['REQUEST_METHOD'] === 'POST' because it’s more robust.
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?
Yes the problem is the email is not sending. Also i don’t have an emailing service set up what one would you suggest? and how do make it work with my code?
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
yum-based distros:
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 i’m trying to get the pre installed postfix system work with what the code i’ve got. i found a command to activate the postfix mail system to send emails with:
sudo postfix start
and i’ve done a couple of test emails but I still need to put something in the code to make it go through this. Also i’m running mac os x.
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.
Take a look at the configuration options for the mail function in the PHP manual at http://php.net/manual/en/mail.configuration.php. It looks to me like you’ll need to set the sendmail_path option, but there may be more.
After you make changes to your php.ini, you’ll need to restart the server software to make sure it gets reloaded.
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.