简体   繁体   English

PHP从HTML表单发送电子邮件

[英]PHP Send Email From HTML Form

I want to get an email from an html form. 我想从html表单获取电子邮件。 I was following a tutorial that used PHP, which I have never used before, and it doesn't seem to work. 我正在跟踪一个使用PHP的教程,该教程以前从未使用过,并且似乎无法正常工作。 After I hit submit, the php page opens, but an email is not sent to me. 当我点击提交后,php页面打开,但是没有电子邮件发送给我。 Can someone please help me. 有人可以帮帮我吗。

HTML(A couple inputs like name and email, etc. and a submit button at the bottom): HTML(几个输入,例如姓名和电子邮件等,底部是一个提交按钮):

<form action="contact_form_process.php" method="POST">
    <section id="contact" class="py-3">
      <div class="container">
        <div class="row">
          <div class="col-md-9 mx-auto">
            <div class="card p-4">
              <div class="card-body">
                <h3 class="text-center lato-font">Please Fill Out This Form To Contact Us</h3>
                <hr>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="first_name" type="text" class="form-control" placeholder="First Name">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="last_name" type="text" class="form-control" placeholder="Last Name">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="email" type="text" class="form-control" placeholder="Email">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="phone_num" type="text" class="form-control" placeholder="Phone Number">
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <textarea class="form-control" name="" id="message" rows="10" placeholder="Message"></textarea>
                    </div>
                  </div>
                </div>
              </div>
              <div class="card-footer">
                <input type="submit" class="btn btn-outline-success btn-block" value="Send">
                <button type="submit" class="btn btn-outline-success btn-block">Send</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </form>

Here is my PHP: 这是我的PHP:

<?php

  // Subject and Email Variables
  $emailSubject = 'Test Email';
  $webMaster = 'tamiroffen@gmail.com';

  // Gathering Data Variables
  $first_nameField = $_POST['first_name'];
  $last_nameField = $_POST['last_name'];
  $emailField = $_POST['email'];
  $phone_numField = $_POST['phone_num'];
  $messageField = $_POST['message'];

  $body = <<<EOD
<br><hr><br>
Email: $emailField <br>
Name: $first_nameField <br>
Phone Number: $phone_numField <br>
Message: $message <br>
EOD;

  $headers = "From: $emailField\r\n";
  $headers .= "Content-type: text/html\r\n";
  $success = mail($webMaster, $emailSubject, $body, $headers);



?>

Thank you! 谢谢!

mail() needs to be enabled as @esqew points out in his comment - this would look something like this (this is on my BigRock environment YMMV) - @esqew在他的评论中指出,需要启用mail()-看起来像这样(在我的BigRock环境YMMV中)-

ini_set("include_path", '<path to php>' . ini_get("include_path") );
require_once "Mail.php";

Also, if you are using GMail, you also need to set up GMail to allow sending mail through SMTP - https://support.google.com/mail/answer/7126229?visit_id=636741342414654323-2069917091&hl=en&rd=1 另外,如果您使用的是GMail,则还需要设置GMail以允许通过SMTP发送邮件-https: //support.google.com/mail/answer/7126229? visit_id=636741342414654323-2069917091 &hl=zh_CN&rd=1

 $webMaster = 'tamiroffen@gmail.com';

I assume this is the address where the emails are sent to. 我假设这是电子邮件发送到的地址。 From what I've heard, PHP mail() function doesn't work for gmail(if it's on the receiver end). 据我所知,PHP mail()函数不适用于gmail(如果它在接收器端)。

You can just use another email provider like hotmail or any and forward emails from it to your gmail 您可以只使用其他电子邮件提供商,例如hotmail或任何其他电子邮件提供商,并将其转发到Gmail

Watch this video if you need more info on it, How to create a PHP Contact Form Tutorial 如果需要更多信息,请观看此视频, 如何创建PHP联系人表单教程

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

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