0

So, here we go with silly question number too many to count!

I've made a very simple PHP contact form using tutorials from the internet (I still need to add security measures to it but I wanted to get it working first) When I click on the send button on my website I do get the message sent script however no email arrives in my in box.

Any ideas what I'm doing wrong? The website is currently hosted locally via XAMPP.

$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$from = 'From: me@mywebsite.co.uk';
$to = 'me@mywebsite.co.uk';
$subject = 'Enquiry';

$body = "From: $name\n Company: $company\n Email: $email\n Telephone: $tel\n Message: $message\n";

if ($_POST['send']) {
    if(mail($to, $subject, $body, $from)) {
        echo '<p> Your message has been sent!</p>';
    } else {
        echo '<p>Message could not be sent.<br>Please check that you have completed the name, email and message fields and try again</p>';
    }
}
Tessa
  • 163
  • 1
  • 1
  • 13
  • 1
    You probably need to install a mail server. Try getting an email sent from the command line before using `mail()` – Parris Varney Oct 03 '15 at 12:34
  • Variables don't get parsed in single quotes, so nothing in the body will process, nor will `\n`'s. – Funk Forty Niner Oct 03 '15 at 12:36
  • Plus, `From:` expects an email address. Check for errors. – Funk Forty Niner Oct 03 '15 at 12:38
  • I've changed the quotes and added an email to the "From:" section but still not working. Can you suggest a mail server – Tessa Oct 03 '15 at 12:45
  • I added the code error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump"); to the top of my PHP code, no error was reported. – Tessa Oct 03 '15 at 12:47
  • Which message is echoed from your script: "_Your message has been sent_" or "_Message could not be sent_"? – Gustavo Straube Oct 03 '15 at 13:10
  • Your message has been sent – Tessa Oct 03 '15 at 13:12
  • mail() function " Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. " In other words - there will be NO error message if mail was not sent - so, you can't rely too much on error messages when mail() function is used... If you want to send email from localhost - you need to install and configure mail server, as mentioned in first comment. – sinisake Oct 03 '15 at 13:23
  • You are using XAMPP does that mean you are running on a windows system? If you are, windows does not come with a mail server, so `mail()` will not work. I suggest you look at phpMailer. – RiggsFolly Oct 03 '15 at 13:31
  • Thanks nevermind, yes, I was aware that the true response doesn't mean that the message actually reached the desired destination. Can anyone suggest which mail server I should use as when I programmed a form previously I used code igniter and it worked without. Also, will I need to edit the code in order for it to work when it goes live? – Tessa Oct 03 '15 at 13:31
  • Codeigniter uses `phpMailer` under the covers I believe – RiggsFolly Oct 03 '15 at 13:32
  • That explains it. Thanks Riggs – Tessa Oct 03 '15 at 13:33

2 Answers2

1

Alright:

Step 1: check your error logs for any problems with the mail not being sent. Normally when installing an Apache inside windows the most people skip to set the from server and credentials.

I used WAMP alot and that one works normally only with an external account.

Step 2: If anything fails.

Download a mailer library and use Gmail to send the emails. Here is an tutorial on that : http://phpmailer.worxware.com/?pg=examplebgmail

Works great. Sure there is a lot of files in phpmailer but it works and easy to upgrade when new software versions are released.

user2420647
  • 183
  • 11
0

As when I've previously set up contact forms I've been doing so using Code Igniter I didn't realise that I couldn't use mail() without installing a mail server.

Thanks to Parris Varney and RiggsFolly for pointing this out and thanks again to Riggs for letting me know that Code Igniter uses the PHPMailer library.

By using PHPmailer I was able to correct the code and get the form working perfectly in very short order.

For anyone interested the new code used with the latest version of PHPmailer is:

$name = $_REQUEST['name'];
$co = $_REQUEST['company'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$message = $_REQUEST['message'];

require("PHPMailerAutoLoad.php");

$mail = new PHPMailer();

$mail->isSMTP();

$mail->Host = "mail.mydomain.co.uk";
$mail->SMTPAuth = true;
$mail->Username = "me@mydomain.co.uk";
$mail->Password = "password";
$mail->SMTPAutoTLS = false;

$mail->From = $email;
$mail->addAddress("me@mydomain.co.uk", "Me");
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Enquiry";

$mail->Body = "From: $name<br>Company: $co<br>Email: $email<br>Telephone: $tel<br>Message: $message";
$mail->AltBody = "From: $name Company: $co Email: $email Telephone: $tel Message: $message";

if(!$mail->Send())
{
    echo "Message could not be sent";
}
echo "Message has been sent";
?>
Tessa
  • 163
  • 1
  • 1
  • 13