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( "[email protected]", "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( "[email protected]", "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>
3
  • You should sanitize your inputs stackoverflow.com/q/1055460/1607098 Commented Mar 13, 2013 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( "[email protected]", "Message via your website!", "$name $services $message", "From: $email" ); Commented Mar 13, 2013 at 10:24
  • php mail functon returns true or false after sending the mail. do check what does the mail function returns??? Commented Mar 13, 2013 at 10:24

4 Answers 4

3

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

mail( "[email protected]", "Message via your website!", "$name\r\n $services\r\n $message", "From: $email");
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, this works however the services does not come through via email, any reasons you can see? The field names all seem correct.
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.
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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\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( "[email protected]", "Message via your website!",
      $message, "From: $email" );

Comments

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);

Comments

-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( "[email protected]", "Message via your website!",
           $name . $services. $message .  "From: $email" );
    header( "Location: thankyou.html" );
  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.