【问题标题】:Contact form is not working using PHP, I want to just send mail using PHP form [duplicate]联系表单无法使用 PHP,我只想使用 PHP 表单发送邮件 [重复]
【发布时间】:2019-11-18 05:55:58
【问题描述】:

我有 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!";
        }
    }
?>

我们将不胜感激!

【问题讨论】:

  • 你遇到了什么错误?
  • 可能是你的smtp服务器没有设置什么的,试试error_get_last();
  • 不是 PHP 播放器,但只是提出一点,PHP 的 mail 函数不是那么好,并且在发送电子邮件或我说接收时运气不好。我需要切换PhpMailer
  • 你从服务器或本地主机得到什么错误。有时描述发生的错误消息超过 70 个字母,您需要将其包装起来。因为每一行都应该用 LF (\n) 分隔,并且行不应超过 70 个字符。使用下面的代码来包装消息。 $message = wordwrap($message,70); 将包装邮件。
  • 我收到错误“出了点问题!”

标签: php html email


【解决方案1】:

来自 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!";
}

【讨论】:

  • 再次出现同样的错误“出了点问题!”
  • 实际上这不是在本地主机上工作,而是在服务器上工作,但是有一个新问题“现在邮件进入垃圾邮件文件夹”
  • 默认情况下它不会在 localhost 中工作,但它可以在服务器中工作。对于 localhost ,您必须设置您的 localhost 。请参阅此处获取 localhost stackoverflow.com/questions/15965376/… 或使用 phpmailer 获取 localhost
  • 如果你想使用PHPMailer ,请通过这个链接codexworld.com/how-to-send-email-from-localhost-in-php
  • 如果可行,请接受此答案
猜你喜欢
  • 2013-04-27
  • 2013-04-08
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2019-09-04
相关资源
最近更新 更多