Vertika Vertika - 1 year ago 37
PHP Question

PHP file is not retrieving values from HTML website

My PHP file is not passing values from the website to my email.

Form code block in index.html:

<!--Contact Form-->

<form id="contact" action="mail.php" method="post" enctype="text/plain">

<input name="name" placeholder="Your name" type="text" tabindex="1" required autofocus>

<input name="email" placeholder="Your Email Address" type="email" tabindex="2" required>

<input name="contact" placeholder="Your Phone Number" type="tel" tabindex="3" required>

<textarea name="message" placeholder="Your Message...." tabindex="4" required></textarea><br>

<input id="submit" name="submit" type="submit" value="Submit"/>
<input id="reset" name="reset" type="reset" value="Reset">


#contact input[type="text"],
#contact input[type="email"],
#contact input[type="tel"],
#contact textarea {
box-shadow:inset 0 1px 2px #DDD, 0 1px 0 #FFF;
-webkit-box-shadow:inset 0 1px 2px #DDD, 0 1px 0 #FFF;
-moz-box-shadow:inset 0 1px 2px #DDD, 0 1px 0 #FFF;
border:1px solid #CCC;
padding:10px 10px;
font-family:Arial, Helvetica, sans-serif;
fieldset {
#contact button [type="submit"],
#contact button [type="reset"]

PHP file:

//collect data
$name = $_POST['name'];//Your Name
$email = $_POST['email'];//Email ID
$contact = $_POST['contact'];//Contact number
$message = $_POST['message'];//Message

//declare data
$to = "";//Recipient Email ID
$subject = "Message from";//Inbox Subject
$body = "Message from-\n Name: $name\n Email: $email\n Contact: $contact\n Message: $message";
mail($to,$subject,$body);//Mail sent

echo "<div align='center' style ='font:30px Arial,Helvetica,sans-serif; color:blue'>Thank you for your message. I will be in touch with you very soon!</div>";//Thank you Screen

Output I am getting on my mail:

'Message from-





[Not getting values of the variables submitted on my website '']

I tried:

  • $body = "From: ($_POST['name'])\n Email: ($_POST['email'])\n Contact: ($_POST['contact'])\n $message = ($_POST['message'])";

  • $body = ("From:" .$name "\nEmail:" .$email "\nContact:" .$contact "\nMessage:" .message);

Do I have to use PHPMailer or can I send mail without that?

Answer Source

Simple; remove enctype="text/plain" from your form tags; it isn't a valid enctype when using POST arrays.

Plus, I didn't see a closing </form> tag in your code, so make sure it is in fact present.

You should also use proper headers, otherwise your code may be mistaken for spam without a valid From: email address.

It's also advisable that you check for any empty inputs, should someone not be using an HTML5 compatible browser; it can happen.

Do I have to use PhpMailer or I can send mails without that?

Even if you were to use PHPMailer or any other mailer, you would still have to remove the enctype from your form.