简体   繁体   English

使用PHP将表单数据发送到电子邮件

[英]Sending form data to E-mail with PHP

I'm trying to send form data to my E-mail with no success. 我试图将表单数据发送到我的电子邮件,但没有成功。 I want that when the user click on "submit" button (and after validation) the data field will send to my e-mail and the user see a message "Thank you for contacting us" instead of the form field. 我希望当用户单击“提交”按钮(并在验证之后)时,数据字段将发送到我的电子邮件中,并且用户看到一条消息“谢谢您与我们联系”,而不是表单字段。 I am very new with PHP and hope one of you can help me. 我对PHP非常陌生,希望你们中的一员能帮助我。

This is my HTML form: 这是我的HTML表单:

    <form id="contactForm" method="post" action="contact.php" onsubmit="return validateForm()">
            <input type="text" name="name" id="name" placeholder="Full Name">

            <input type="email" name="email" id="email" placeholder="E-mail Address">

            <input type="text" name="phone" id="phone" placeholder="Phone Number">

            <input type="text" name="company" id="company" placeholder="Company">

            <textarea name="message" id="message" placeholder="Write Your Message Here..."></textarea>

            <input type="submit" value="SEND" id="submitForm">
</form>

and this is my PHP Code: 这是我的PHP代码:

<?php

if($_POST["submit"]) {
    $recipient="myEmail@gmail.com";
    $subject="Message from Website";
    $sender=$_POST["name"];
    $senderEmail=$_POST["email"];
    $senderPhone=$_POST["phone"];
    $senderCompany=$_POST["company"];
    $message=$_POST["message"];

    $mailBody="Name: $sender\nEmail: $senderEmail\nPhone: $senderPhone\nCompany: $senderCompany\n\n$message";

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

$thankYou="<p>Thank you! Your message has been sent.</p>";
}

?>

I also have JS validation function to this form called validateForm() which works fine. 我也有这种形式的JS验证功能,称为validateForm(),可以正常工作。 Should I need to define something on the hosting server to make this PHP works? 我是否需要在托管服务器上定义一些内容才能使该PHP工作?

Add the following lines to the top of your script 将以下行添加到脚本顶部

error_reporting(E_ALL);
ini_set("display_errors", "On");

If your server is not configured correctly with mail you will see errors. 如果未使用邮件正确配置服务器,则会看到错误。 The best practice is to use PHPMailer. 最佳实践是使用PHPMailer。 With that you can use sendmail or any SMTP server 这样您就可以使用sendmail或任何SMTP服务器

Have you tried using something like PHPMailer? 您是否尝试过使用类似PHPMailer的工具?

https://github.com/PHPMailer/PHPMailer/ https://github.com/PHPMailer/PHPMailer/

Its a class that'll send the email with the appropriate headers and is both easy to use and debug. 它是一个类,它将使用适当的标题发送电子邮件,并且易于使用和调试。

You'll also find that since it has the appropriate headers, spam filters wont stop it from reaching the correct destination. 您还会发现,由于它具有适当的标题,因此垃圾邮件过滤器不会阻止其到达正确的目的地。

Hope it helps. 希望能帮助到你。

use this 用这个

<input type="submit" value="SEND" id="submit">

UPDATE: 更新:

It is the name="xx" attribute of the input button that is used to give a variable name to the values passed on the $_POST or $_GET arrays 它是输入按钮的name="xx"属性,用于为$ _POST或$ _GET数组中传递的值赋予变量名称

So you need to add a name="submit" to the button html tag like so 因此,您需要像这样在按钮html标记中添加一个name="submit"

<input type="submit" value="SEND" name="submit">

Now this PHP statement will work as it will see a named variable called 'submit' in the $_POST array 现在,此PHP语句将起作用,因为它将在$ _POST数组中看到一个名为“ submit”的命名变量。

if ( $_POST['submit'] )

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

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