PHP邮件功能正在发送空白邮件正文

时间:2019-12-02 10:18:57

标签: php email

我知道还有其他类似的问题,但是他们的回答并不能解决我的问题。

我开发了一个网站,并暂时决定继续使用PHP邮件,而不是其他途径,因为它是最快,最简单的方法(我是一名业余开发人员)。在过去的一个月中,它运行良好,没有任何问题,但是三天前,我开始收到没有邮件内容的电子邮件(这些邮件不是垃圾邮件,或者只是空邮件,我知道我应该使用验证等)。

我仍然成功收到了发送到我的电子邮件帐户的电子邮件,其中还包含主题标题为“ Contact Form Request”,但是正文为空,即不是应该填充的$ msg吗?

我切换了“联系表单请求”和$ msg的位置,$ msg内容将在电子邮件主题标头中接收,因此将其填充,但是消息正文仍然为空。

任何帮助将不胜感激。

contact.html

<div id="contact-form">
        <form action="contact.php" method="post">

          <div class="form-group">
            <label for="inputName">Name *</label>
            <input type="text" class="form-control" name="name" placeholder="Enter name" required>
          </div>

          <div class="form-group">
            <label for="inputNumber">Contact Number *</label>
            <input type="tel" class="form-control" name="phone" placeholder="Enter contact number" required inputMode="tel">
          </div>

          <div class="form-group">
            <label for="inputEmail">Email *</label>
            <input type="email" class="form-control" name="email" placeholder="Enter email" required>
          </div>

          <div class="form-group">
            <label for="inputSubject">Subject *</label>
            <input type="text" class="form-control" name="subject" placeholder="Enter subject" required>
          </div>

          <div class="form-group">
            <label for="inputMessage">Message *</label>
            <textarea type="text" class="form-control" name="message" placeholder="Enter message" rows="3" required maxlength="300"></textarea>
          </div>

          <p><small><strong>*</strong> These fields are required.</small></p>

          <button type="submit" class="btn btn-send">Send</button>

        </form>
      </div>

contact.php

<?php

$webmaster_email = "test@drivingschool.co.uk";

$success_page = "thankyoucontact.html";

$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

$msg =
"You have received a message from " . $name . "\r\n\n" .
"Subject: " . $subject . "\r\n\n" .
"Message: " . $message . "\r\n\n" .
"Name: " . $name . "\r\n" .
"Phone: " . $phone . "\r\n" .
"Email: " . $email . "\r\n\n\n\n" .
"DISCLAIMER:\r\n\nThis e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.";

mail($webmaster_email, "Contact Form Request", $msg);
header("Location: $success_page");

?>

2 个答案:

答案 0 :(得分:0)

您的代码在第一次检查时看起来还可以(不是安全的,但是可以正常工作)。

我的建议:

第一:调试。仅显示消息而不是实际邮件。如下所示:

<?php

$webmaster_email = "test@drivingschool.co.uk";

$success_page = "thankyoucontact.html";

$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

$msg =
"You have received a message from " . $name . "\r\n\n" .
"Subject: " . $subject . "\r\n\n" .
"Message: " . $message . "\r\n\n" .
"Name: " . $name . "\r\n" .
"Phone: " . $phone . "\r\n" .
"Email: " . $email . "\r\n\n\n\n" .
"DISCLAIMER:\r\n\nThis e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.";

echo "The message contains: $msg";    
exit;

// mail($webmaster_email, "Contact Form Request", $msg);
// header("Location: $success_page");
?>

如果您的消息包含您期望的内容,则必须调查电子邮件网关。这要困难得多。

我将首先制作一个这样的脚本,将其仅包含在其中,将其称为testmail.php,然后看看是否可行。

<?php
mail('test@drivingschool.co.uk', "testing", "testing.\nDoes this arrive?");
?>

编辑:要检查的第三件事:

您正在发送纯文本邮件。也许您的电子邮件客户端仅显示HTML电子邮件?请检查。

编辑:第四条建议:

我会从命令行尝试邮件实用程序本身,以检查您的PHP是否表现异常。 因此,如果您具有SHELL访问权限(BASH或其他方式),请尝试以下操作:

mail -s "Testing mail" test@drivingschool.co.uk <<< 'Testing. Is this body visible?'

如果同样失败,请与您的托管服务提供商进行聊天。

如果可行:结合sendmail的PHP出了点问题。

以下是一些背景: https://www.binarytides.com/linux-mail-command-examples/

答案 1 :(得分:0)

从带有标题的电子邮件中进行设置。如果邮件为HTML格式,您还可以使用内容类型 text / html

$from ='testmail@mail.com';
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
@mail($webmaster_email,$subject,$message,$headers);

希望这项工作