电子邮件正在发送但没有数据填充

时间:2021-02-12 19:11:29

标签: php html email

遗憾的是,我的语法知识不是它应该的样子,而且我很确定我犯了非常愚蠢的错误。

我有一个 HTML 表单和 php 文件,都在我的服务器上。 发送时,我收到一封电子邮件,但未填充 html 表单中的值。 谁能发现我哪里出错了。

HTML 是:

 <form action="contact.php" mothod="post" id="contact-form" target="blank">
        <input class="form-control input-outline" id="formName" type="text" name="formName" placeholder="Full name:">
        <input class="form-control input-outline" id="formEmail" type="text" name="formEmail" placeholder="Email address:">
        <textarea class="form-control input-outline" id="formMessage" rows="6" cols="50" name="formMessage" placeholder="Message"></textarea>
        <button type="submit">Submit</button>
  </form>

PHP 是:

<?php
    $email_to = "myemail@myaddress.co.uk";
    $email_subject = "New form submissions";

    $name = $_POST['formName']; // required
    $email = $_POST['formEmail']; // required
    $message = $_POST['formMessage']; // required

    $headers .= "\r\n Name: " . $name;
    $headers .= "\r\n Email: " . $email;
    $headers .= "\r\n Message: " . $message;

    @mail($email_to, $email_subject, $headers);
?>

如果可以解释我的错误,以便我能从中吸取教训,那就太棒了。

附言

在我的办公桌前待了很长时间,现在正在编写代码,所以如果这是一个非常愚蠢的错误,那么感谢您对我的耐心。

一切顺利——

2 个答案:

答案 0 :(得分:1)

可能你的错误是你有方法而不是方法

 <form action="contact.php" method="post" id="contact-form" target="blank">

答案 1 :(得分:1)

首先,你没有定义$header,所以定义为空字符串。然后,如果您打算发送实际的电子邮件标头,则必须根据 specification:

包含“发件人”标头 <块引用>

发送邮件时,邮件必须包含 From 标头。这可以使用 additional_headers 参数设置,或者可以在 php.ini 中设置默认值。

您还在 HTML 中输入了一个错字:mothod -> method

$email_to = "myemail@myaddress.co.uk";
$email_subject = "New form submissions";

$name = $_POST['formName']; // required
$email = $_POST['formEmail']; // required
$message = $_POST['formMessage']; // required
$headers = "";

$headers .= "\r\n Name: " . $name;
$headers .= "\r\n Email: " . $email;
$headers .= "\r\n Message: " . $message;

@mail($email_to, $email_subject, $headers);