I’m trying to send an email using PHP mail(). I created a form that allows the user to input their information and press a submit button to send the email.
What I’m trying to do is to grab $email and set it to the $to header to send to the receiver. Everything else works perfectly, $name and $contact outputs just missing $email.
<form action="#" method="post"> <input type="text" name="name" placeholder="Your Name"><br/> <input type="text" name="email" placeholder="Your Email"><br/> <input type="text" name="contact" placeholder="Your Mobile"><br/> <input type="submit" name="submit" value="Submit"> </form> <?php echo "Welcome: ". $_POST['name']. "<br />"; echo "Your Email is: ". $_POST["email"]. "<br />"; echo "Your Mobile No. is: ". $_POST["contact"]; ?> <?php $name = $_POST['name']; $email = $_POST['email']; $contact = $_POST['contact']; $to = $email; $subject = "Test Email"; $message = 'Hello '.$name.', contact: '.$contact.', this is a test email'; $headers = "From: Me <". strip_tags('test@mail.com') . ">rn"; mail($to,$subject,$message,$headers);
?>
Any tips would be appreciated
Advertisement
Answer
Solution:
<?php if (!array_key_exists('Submitted',$_POST)) { ?> <form method="post" action="#"> <input type="hidden" name="Submitted" value="true"><form action="#" method="post"> <input type="text" name="name" placeholder="Your Name"><br/> <input type="text" name="email" placeholder="Your Email"><br/> <input type="text" name="contact" placeholder="Your Mobile"><br/> <input type="submit" name="submit" value="Submit"> </form> <?php echo "Welcome: ". $_POST['name']. "<br />"; echo "Your Email is: ". $_POST["email"]. "<br />"; echo "Your Mobile No. is: ". $_POST["contact"]; ?> <?php } else { $name = $_POST['name']; $contact = $_POST['contact']; $to = $_POST['email']; $subject = "Test Email"; $message = 'Hello '.$name.',<br /> contact #: '.$contact.', this is a test email'; $headers = "From: Me <". strip_tags('test@mail.com') . ">rn"; $headers .= "MIME-Version: 1.0rn"; $headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; if(mail($to,$subject,$message,$headers)) { echo "Message Sent"; } else { echo "Message Not Sent"; } } ?>