1

I have created a send to email PHP script.

The form seems to work correctly when sending only one field (the message field) but as soon as other fields are added, the form ceases to be emailed on to the inbox (yet the form still fires correctly and reroutes to the thankyou just fine.)

Here is the code I currently have, this does not work

<?php
  $email = $_REQUEST['email'] ;
  $name = $_REQUEST['name'] ;
  $services = $_REQUEST['services'] ;
  $message = $_REQUEST['message'] ;

  if (!isset($_REQUEST['email'])) {
    header( "Location: feedback.html" );
  }
  elseif (empty($email) || empty($message)) {
     header( "Location: error.html" );
 }
  else {
    mail( "info@website.co.uk", "Message via your website!",
           $name, $services, $message, "From: $email" );
    header( "Location: thankyou.html" );
  }
?>

This is the previous code, this does work, but only displays the message

<?php
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;

  if (!isset($_REQUEST['email'])) {
    header( "Location: feedback.html" );
  }
  elseif (empty($email) || empty($message)) {
     header( "Location: error.html" );
 }
  else {
    mail( "info@website.co.uk", "Message via your website!",
          $message, "From: $email" );
    header( "Location: thankyou.html" );
  }
?>

This is the HTML for the form:

<form method="post" action="sendmail.php">
    <label>Name:</label> <input name="name" type="text" /><br />
    <label>Email:</label> <input name="email" type="text" /><br />
    <label>What service do you require?:</label>  <input name="services" type="text" /><br />
    <label>Message:</label><br />
    <textarea name="message" rows="15" cols="40">
    </textarea><br />
    <input type="submit" />
</form>
juco
  • 6,331
  • 3
  • 25
  • 42
Francesca
  • 26,842
  • 28
  • 90
  • 153
  • You should sanitize your inputs http://stackoverflow.com/q/1055460/1607098 – Touki Mar 13 '13 at 10:24
  • You are giving Mail() too many parameters. Are you wanting $name, $services and $message to all be in the message body? If so you need something like this `mail( "info@website.co.uk", "Message via your website!", "$name $services $message", "From: $email" );` – Jacob Tomlinson Mar 13 '13 at 10:24
  • php mail functon returns true or false after sending the mail. do check what does the mail function returns??? – 웃웃웃웃웃 Mar 13 '13 at 10:24

4 Answers4

3

As per the mail() docs the message body should be passed as a single parameter. So it should look something more like:

mail( "info@website.co.uk", "Message via your website!", "$name\r\n $services\r\n $message", "From: $email");
juco
  • 6,331
  • 3
  • 25
  • 42
  • Hi, this works however the services does not come through via email, any reasons you can see? The field names all seem correct. – Francesca Mar 13 '13 at 10:37
  • Would need to do some debugging. Try a `var_dump($_POST);` Also, to neaten it all up and help with debugging, you could consider constructing the body outside of the `mail()` function, e.g. `$body = "$name\r\n$services\r\n$message";` Then try `echo $body` to check it looks how you'd expect. – juco Mar 13 '13 at 10:40
1

According to php.net the correct use of the mail() function is:

<?
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

You are trying to send the additional data ($services) as a header.

Try to merge your $message and $service >

$message = $service . "\r\n\r\n" . $message;
 mail( "info@website.co.uk", "Message via your website!",
      $message, "From: $email" );
Gergely Králik
  • 440
  • 4
  • 14
0

You can't just add more and more variables to mail(). There is only one parameter that specifies the content of the email, which would be the third one, as you can see in the official documentation:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

If you want to have more than just $message in your mail, you need to put everything into one variable to be given to the function as content.

$content = $message;
$content .= "\n" . $services;
// etc...
mail($to, $subject, $content);
Till Helge
  • 9,253
  • 2
  • 40
  • 56
-1

Try this

<?php
  $email = $_POST['email'] ;
  $name = $_POST['name'] ;
  $services = $_POST['services'] ;
  $message = $_POST['message'] ;

  if (!isset($_POST['email'])) {
    header( "Location: feedback.html" );
  }
  elseif (empty($email) || empty($message)) {
     header( "Location: error.html" );
 }
  else {
    mail( "info@website.co.uk", "Message via your website!",
           $name . $services. $message .  "From: $email" );
    header( "Location: thankyou.html" );
  }
?>
Tschallacka
  • 27,901
  • 14
  • 88
  • 133