简体   繁体   English

带有 PHP 的电子邮件联系表格数据

[英]E-mail contact form data with PHP

I have a html form and I want to take the data that is entered and send it to myself in an e-mail, I'm not at all familiar with PHP but after some Googleing it seemed to be the way to go.我有一个 html 表格,我想获取输入的数据并通过电子邮件将其发送给自己,我对 PHP 一点也不熟悉,但经过一番 Google 搜索后,它似乎是通往 Z34D1F91FB12E514B85676A 的方式。

I'm not too sure what's not quite working, but any insight would be awesome!!我不太确定什么不太有效,但任何见解都会很棒!

HTML: HTML:

    <section class="contact" id="contact">
        <div class="container">
            <div class="section-heading">
                <h1 data-aos="fade-right" data-aos-delay="150">Contact</h1>
                <h6 data-aos="fade-left" data-aos-delay="150">Contact Me</h6>
            </div>
            <form method="post" name="contact_form" action="contact-form-handler.php" data-aos="fade-up" data-aos-delay="200" onsubmit="return false">
                <label for="name">Name:</label>
                <input type="text" id="name" name="name" placeholder="Enter Your Name..." required>

                <label for="name">Email:</label>
                <input type="email" id="email" name="email" placeholder="Enter Your E-mail..." required>

                <label for="number">Contact Number:</label>
                <input type="number" id="number" name="number" placeholder="Enter Your Contact Number...">
                
                <label for="message">Message:</label>
                <textarea name="subject" id="subject" cols="10" rows="10" placeholder="Enter Your Messgage..."></textarea>
                <input type="submit" value="Submit" onclick="sendContact();">
                


            </form>
            <?php include 'contact-form-handler.php';?>
        </div>

PHP: [separate file in same directory called contact-form-handler.php] PHP:[在同一目录中名为contact-form-handler.php的单独文件]

<?php
if(!empty($_POST["submit"])) {
    $name = $_POST["name"];
    $email = $_POST["number"];
    $subject = $_POST["email"];
    $content = $_POST["subject"];

    $toEmail = "admin@phppot_samples.com";
    $mailHeaders = "From: " . $name . "<". $email .">\r\n";
    if(mail($toEmail, $subject, $content, $mailHeaders)) {
        $message = "Your contact information is received successfully.";
        $type = "success";
    }
}
?>

Again, any kinda advice is very appreciated!再次,非常感谢任何建议!

Everything seems fine in your HMTL.在您的 HMTL 中一切似乎都很好。 But I think that you misunderstood the PHP part about sending an email.但我认为您误解了 PHP 关于发送 email 的部分。

The "FROM:" field in your header should be the address that you own in the mail server, see the example below. header 中的“FROM:”字段应该是您在邮件服务器中拥有的地址,请参见下面的示例。

Also, setting the content type and charset is recommended:)此外,建议设置内容类型和字符集:)

<?php
$mailHeaders = "Content-type:text/html;charset=UTF-8" . "\r\n";

if(!empty($_POST["submit"])) {
    $name = $_POST["name"];
    $email = $_POST["number"];
    $subject = $_POST["email"];
    $content = $_POST["subject"];

    $toEmail = "admin@phppot_samples.com";
    $mailHeaders .= "From: <Your@DomainName.com>\r\n";
    if(mail($toEmail, $subject, $content, $mailHeaders)) {
        $message = "Your contact information is received successfully.";
        $type = "success";
    }
}
?>

Furthermore, I would recommend to read threw the documentation:) https://www.php.net/manual/en/function.mail.php此外,我建议阅读文档:) https://www.php.net/manual/en/function.mail.php

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

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