首页 > 解决方案 > 联系表单无法使用 PHP,我只想使用 PHP 表单发送邮件

问题描述

我有 PHP 电子邮件表单,我想使用 PHP 表单发送电子邮件,但这不起作用。

HTML:-

<form action="mail_handler.php" method="post" name="form" class="form-box">
    <label for="name">Name</label><br>
    <input type="text" name="name" class="inp" placeholder="Enter Your Name" required><br>
    <label for="email">Email ID</label><br>
    <input type="email" name="email" class="inp" placeholder="Enter Your Email" required><br>
    <label for="phone">Phone</label><br>
    <input type="tel" name="phone" class="inp" placeholder="Enter Your Phone" required><br>
    <label for="message">Message</label><br>
    <textarea name="msg" class="msg-box" placeholder="Enter Your Message Here..." required></textarea><br>
    <input type="submit" name="submit" value="Send" class="sub-btn">
</form>

PHP:-

<?php
    if(isset($_POST['submit'])){
        $name=$_POST['name'];
        $email=$_POST['email'];
        $phone=$_POST['phone'];
        $msg=$_POST['msg'];

        $to='123@gmail.com'; // Receiver Email ID, Replace with your email ID
        $subject='Form Submission';
        $message="Name :".$name."Phone :".$phone."Wrote the following :".$msg;
        $headers="From: ".$email;

        if(mail($to, $subject, $message, $headers)){
            echo "<h1>Sent Successfully! Thank you, We will contact you shortly!</h1>";
        }
        else{
            echo "Something went wrong!";
        }
    }
?>

答案将不胜感激!

标签: phphtmlemail

解决方案


来自 PHP 手册https://www.php.net/manual/en/function.mail.php

您在这里没有使用标题。使用下面的代码

// To send HTML mail, the Content-type header must be set. I think you are missing this
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers . 
$headers[] = 'From:  <'.$email.'>'; 

// Mail it
if(mail($to, $subject, $message, implode("\r\n", $headers))) //Multiple extra headers should be separated with a CRLF (\r\n).
{
   echo "<h1>Sent Successfully! Thank you, We will contact you shortly!</h1>";
}else{
  echo "Something went wrong!";
}

推荐阅读