php邮件发送联系表

时间:2019-03-14 06:14:32

标签: php html phpmailer mail-sender

提交表格后,我会有联系表格,我需要通过电子邮件发送。我是PHP的新手,我也有Xampp。如果我提交表单,它将在浏览器中显示PHP代码。 这是我的HTML代码:

<form class="form-area contact-form text-right" name="myForm" id="myForm"  novalidate action="mail.php" method="POST">
  <div class="row">
    <div class="col-lg-6 form-group">
      <input name="name" id="name" placeholder="Enter your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" class="common-input mb-20 form-control" required="" type="text">
      <input name="email" id="email" placeholder="Enter email address" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" class="common-input mb-20 form-control" required="" type="email">
    <div class="invalid-feedback">Please Enter a Valid Email Id.</div>
    <input name="subject" id="subject" placeholder="Enter your subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your subject'" class="common-input mb-20 form-control" required="" type="text">
    <div class="mt-20 alert-msg" style="text-align: left;"></div>
    </div>
    <div class="col-lg-6 form-group">
      <textarea class="common-textarea form-control" name="message" id="message" placeholder="Message" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Message'" required=""></textarea>
      <button type="submit" class="primary-btn mt-20 text-white" style="float: right;">Send Message</button>
    </div>
  </div>
</form>

mail.php:

<?php
    $to = 'myname@gmail.com';
    $name = $_POST["name"];
    $email= $_POST["email"];
    $text= $_POST["message"];
    $sub= $_POST["subject"];

echo "Hello world!";



    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "From: " . $email . "\r\n"; // Sender's E-mail
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $message ='<table style="width:100%">
        <tr>
            <td>'.$name.'</td>
        </tr>
        <tr><td>Email: '.$email.'</td></tr>
        <tr><td>phone: '.$sub.'</td></tr>
        <tr><td>Text: '.$text.'</td></tr>

    </table>';

    if (@mail($to, $email, $message, $headers))
    {
        echo 'The message has been sent.';
    }else{
        echo 'failed';
    }

?>

如何发送邮件,任何人都可以帮助我。预先感谢

1 个答案:

答案 0 :(得分:0)

您的按钮需要有一个名称。 isset($_POST['submit']总是失败,因为您仅将“ submit”按钮称为“ submit”,而不将其归因于此类型。

<button type="submit" name="submit" ....

通过至少添加一个else,可以使您的生活更轻松

if (isset($_POST['submit'])) {
//your code

} else {
echo 'submit not set';
}

那么您至少会有一些输出来帮助您。