尝试将表单数据发送到我的电子邮件

时间:2014-04-05 11:12:23

标签: php html forms email

我的网站上有一个基本的html表单,我想要将表单中输入的数据一旦提交就发送到我的电子邮箱中,按下提交按钮后我就会被带到刚显示的“Sent.html”屏幕上发送的消息,但是当我检查电子邮件帐户时,我还没有收到电子邮件..

HTML表单....

<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代码......

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

3 个答案:

答案 0 :(得分:2)

尝试使用PHPMailer库: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!
}

答案 1 :(得分:1)

您的PHP代码的问题是您从未实际发送过该电子邮件。要在php中发送邮件,请使用以下语法:

mail($sendto, $subject, $msg)

在您的情况下,您可以在设置$ message后立即放置代码,代码如下所示:

mail($myemail, $subject, $message)

有关php邮件的更多信息,请阅读:http://php.net/manual/en/function.mail.php

答案 2 :(得分:0)

添加以下代码,而不是header("location: sent.html");

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