Cobby
Cobby

Reputation: 19

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">
<tr>
    <td valign="top">
    <label for="name">Name *</label>
</td>
    <td valign="top">
    <input  type="text" name="Name" maxlength="50" size="30">
</td>
</tr>
<tr>

    <td valign="top"">

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

<td valign="top">

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

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

<tr>
    <td colspan="2" style="text-align:center">
    <input type="submit" value="Submit">
</td>
</tr>
</table>
</form>

PHP code...

<?php
/* set email */
$myemail = "dcmagpies@hotmail.co.uk";

/* 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");
?>

Upvotes: 0

Views: 316

Answers (3)

Charlie74
Charlie74

Reputation: 2903

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: http://php.net/manual/en/function.mail.php

Upvotes: 1

Marcos Aguayo
Marcos Aguayo

Reputation: 7210

Try using PHPMailer library: https://github.com/PHPMailer/PHPMailer

$mail = new PHPMailer(true); 

$html = '
<!DOCTYPE html>
<html>
<head>
    <title>Contact</title>
</head>
<body>
    CONTENT HERE
</body>
</html>
';

try {
    $mail->AddAddress('info@domain.com', 'Domain Name');
    $mail->SetFrom('info@domain.com', 'Domain Name');
    $mail->Subject = 'Contact from domain.com';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; 
    $mail->MsgHTML($html);
    if(!$mail->send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }else{
       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!
}

Upvotes: 2

Shijin TR
Shijin TR

Reputation: 7788

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

if(mail($Email,$subject,$message))
    header("location: sent.html");
else
   die('Failed to send an email');

Upvotes: 0

Related Questions