php - Trying to send form data to my email


I have a basic html form on my website and I'm wanting the data entered into the form to be sent to my email once submitted, after submit button pressed I'm taken to the "Sent.html" which just displays a sent message on-screen, but when i check the email account I have not received the email..

HTML Form....

<form name="contactform" method="POST" action="formphp.php">

<table width="450px">
    <td valign="top">
    <label for="name">Name *</label>
    <td valign="top">
    <input  type="text" name="Name" maxlength="50" size="30">

    <td valign="top"">

    <label for="email">Email *</label>
    <td valign="top">
    <input  type="text" name="Email" maxlength="50" size="30">

<td valign="top">

    <label for="subject">Subject *</label>
    <td valign="top">
    <input  type="text" name="Subject" maxlength="50" size="30">

    <td valign="top">
    <label for="questions">Question/Feedback *</label>
    <td valign="top">
    <textarea name="Question" cols="40" rows="5"></textarea>

    <td colspan="2" class="page_speed_181809701">
    <input type="submit" value="Submit">

PHP code...

/* set email */
$myemail = "[email protected]";

/* declare id */
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$subject = $_POST['Subject'];
$Questions = $_POST['Questions'];

/* set subject heading */
$subject = "Subject";

/* Message */
$message = "$Name + $Email + $Questions


/* redirect this form after email sent */
header("location: sent.html");



Try using PHPMailer library:

$mail = new PHPMailer(true); 

$html = '
<!DOCTYPE html>

try {
    $mail->AddAddress('[email protected]', 'Domain Name');
    $mail->SetFrom('[email protected]', 'Domain Name');
    $mail->Subject = 'Contact from';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; 
    if(!$mail->send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       header("location: sent.html");
} catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!



The issue with your php code is that you never actually sent the email. To send mail in php, you use this syntax:

mail($sendto, $subject, $msg)

In your case, you would put the code right after you set your $message, and the code would look like this:

mail($myemail, $subject, $message)

For more info regarding php mail, read here:




Add the following code insted ofheader("location: sent.html");

    header("location: sent.html");
   die('Failed to send an email');

People are also looking for solutions to the problem: mysql - Format php datetime as "Wednesday, April 16, 2014 at 2:24 PM"


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.