简体   繁体   English

为什么我的PHP表单无法正确发送电子邮件?

[英]Why won't my PHP form send emails correctly?

It sends the email, but without a body, subject or anything. 它发送电子邮件,但没有正文,主题或其他任何内容。 This is what I get: 这是我得到的:

在此处输入图片说明

HTML: HTML:

<div class="contact-form">
            <form action="mail.php" method="post">
                <label for="name">Name:<input type="text" id="name" class="text" /></label>
                <label for="email">Email:<input type="text" id="email" class="text" /></label>
                <label for="message">Message:<textarea id="message"></textarea></label>
                <input type="submit" class="submit" value="Send Message" />
            </form>
        </div>

PHP (mail.php): PHP(mail.php):

<?php

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $recipient = "me@christianselig.com";
    $subject = "Message From Website";
    $headers = "From: " . $email;

    mail($recipient, $subject, $message, $headers);
    echo "Thanks! The message was sent. :)";

?>

Any insight? 有见识吗? Thanks so much. 非常感谢。

Forms fields are based on their names not their ids. 表单字段基于其名称而不是其ID。

replace your : 替换您的:

id="(...)"

by 通过

id="(...)" name="(...)"

You should have a look to Swift Mailer which is strongly safer than your current method. 您应该看一下Swift Mailer ,它比当前方法安全得多。

You need to have a name attribute on your HTML input fields. 您需要在HTML输入字段上具有name属性。 That's what the form uses to create the indexes in the $_POST array. 这就是表单用来在$_POST数组中创建索引的方式。 Corrected HTML is below 更正的HTML如下

<div class="contact-form">
        <form action="mail.php" method="post">
            <label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
            <label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
            <label for="message">Message:<textarea id="message" name="message"></textarea></label>
            <input type="submit" class="submit" value="Send Message" />
        </form>
    </div>

Into my server works, are you sure that in $_POST are set? 进入我的服务器,您确定在$ _POST中设置了吗? If you can't send email assicure you can try to set SMTP to send your email Try to use something like phpmailer to send email. 如果您无法发送电子邮件,可以尝试将SMTP设置为发送电子邮件。尝试使用phpmailer之类的工具来发送电子邮件。

In your form for every field you have to put: - id="example_id" - name="example_name" 您必须在表格中的每个字段中输入:-id =“ example_id”-name =“ example_name”

you need to give name attribute for each html element . 您需要为每个html元素赋予name属性。 Use below html code: 使用以下html代码:

<div class="contact-form">
            <form action="mail.php" method="post">
                <label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
                <label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
                <label for="message">Message:<textarea id="message" name="message"></textarea></label>
                <input type="submit" class="submit" value="Send Message" />
            </form>
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM