-1

I have only dabbled in PHP a little, I am trying to remember what I did last year. I am having trouble getting this to work - the user is redirected to index.php but then nothing happens. No email is recieved and no verification 'email sent/not sent' etc.

I am sure it is probably a silly mistake.

Any help would be appreciated.

contact.html

<form method="post" action="index.php">


<label>Name</label>
<input name="name" placeholder="Type Here">

<label>Email</label>
<input name="email" type="email" placeholder="Type Here">

<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>

<label>*What is 2+2? (Anti-spam)</label>
    <input name="human" placeholder="Type Here">

<input id="submit" name="submit" type="submit" value="Submit">

</form>

index.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email']; // GET EMAIL ADDRESS FROM FORM
$to = 'annie.palmer@outlook.com'; 
$subject = 'Website enquiry from' .$name;
$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$headers = "From: Annie<$from>\r\nReturn-path: $from" . "\r\n";
$headers .= "Content-type:text/text;charset=ISO-8859-1"; 

?>
<?php
if ($_POST['submit'] && ($_POST['human'] == '4') {
/* Anything that goes in here is only performed if the form is submitted */
if (mail ($to, $subject, $body, $headers, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Anniee
  • 73
  • 6
  • Has your php.ini been configured correctly to send email? Can you add that file to your question? [This](https://www.tutorialspoint.com/php/php_sending_emails.htm) might help. – FrankRalphBob Mar 10 '17 at 22:40
  • If there's *no* output at all then it sounds like it's not even getting to the point where it tries to send the email. Check the PHP error logs, turn on error reporting, put in some additional `echo` statement just to trace what the code is doing, etc. – David Mar 10 '17 at 22:45
  • 2
    `mail()` uses 4 arguments, not 5. RTM http://php.net/manual/en/function.mail.php – Funk Forty Niner Mar 10 '17 at 22:48
  • @RuhulAmin: _"$_POST['human'] == '4' why 4, is it string or number? you should use $_POST['human'] == 4"_ - no, you should not. Form values received via GET or POST are _always_ of type string. – CBroe Mar 10 '17 at 23:00
  • 1
    [Do not set From headers based on user input.](http://stackoverflow.com/questions/42674080/html-form-with-sendemail/42682934#42682934) –  Mar 10 '17 at 23:18
  • @CBroe You mean the ones where they either ignore comments/answers, or just take off? *lol* yeah, I know those all too well. My (new) habits have since changed quite fast ;-) – Funk Forty Niner Mar 10 '17 at 23:25
  • Sorry it is my daughters birthday and party today so I have been busy. I am not sure how to configure the php.ini file? My site is on a live server, do I have to create one and upload it? – Anniee Mar 11 '17 at 19:17

2 Answers2

1

I'm posting this as a community wiki; I don't feel any rep should come of this, nor do I want rep.

mail() uses 4 arguments, not 5.

There is a 5th but it doesn't do what you're expecting your 5th to do.

So remove the , $from from this:

if (mail ($to, $subject, $body, $headers, $from))
                                        ^^^^^^^

The From: belongs in the header and expects an email address.

Example from the manual:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Try using the PHPMailer. Once uploaded to your site sending an email is easy.

require ('../mail/PHPMailerAutoload.php');
require ('../mail/class.phpmailer.php');

//set up your email

$mail = new PHPMailer();
$mail->isSMTP(); 
$mail->SMTPAuth   = true;                
$mail->SMTPSecure = 'tbs'; //set as required 
$mail->Host       = "enter your email host here"; 
$mail->Port       = 25;                  
$mail->Username   = "enter your mail account username here"; 
$mail->Password   = "enter your mail account password here";
$mail->From      = "the from email address";
$mail->FromName  = "the from name";
$mail->Subject   = "the subject goes here";
$mail->IsHTML(true); //your choice
$body = "the body of you email - html or plain text";
$mail->Body = $body;

$mail->AddAddress("add the email address(es) of recipients");
$mail->Send();
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • How about sticking to what was posted? The error's obvious here. Plus, they'll have to download and configure that entire library. – Funk Forty Niner Mar 10 '17 at 23:28
  • Just offering what is consistently being suggested as the preferred option. The op's choice. By doing so might eliminate the error. Maybe let them decide? – RGriffiths Mar 10 '17 at 23:32
  • Sure, but that leaves them scrambling to find out where to download the phpmailer library and how to configure it properly. – Funk Forty Niner Mar 10 '17 at 23:38
  • Like I said, let them decide. Not exactly difficult. And there is no configuring necessary - upload and go. It is excellent. You can download it here https://sourceforge.net/projects/phpmailer/ – RGriffiths Mar 10 '17 at 23:38