简体   繁体   English

PHP 邮件 function 正在发送空白邮件正文

[英]PHP mail function is sending blank message body

I am aware there are other subjects with similar questions however their answered didn't solve my problem.我知道还有其他主题有类似的问题,但他们的回答并没有解决我的问题。

I developed a website and temporarily decided to go ahead and use PHP mail as opposed to other routes as it was the quickest and easiest (I am an amateur developer).我开发了一个网站,临时决定提前 go 并使用 PHP 邮件而不是其他路由,因为它是最快和最简单的(我是业余开发人员)。 It was working flawlessly for the past month with no issues, however 3 days ago, I started receiving emails with no message contents (these are not spam or just empty messages, I am aware that I should use validation etc).在过去的一个月里,它运行完美,没有任何问题,但是 3 天前,我开始收到没有消息内容的电子邮件(这些不是垃圾邮件或只是空消息,我知道我应该使用验证等)。

I still successfully receive the email to my email account, it also contains the subject header being "Contact Form Request", however the body is empty ie $msg that should be populated aren't?我仍然成功地收到 email 到我的 email 帐户,它还包含主题 header 是“联系表格请求”,但是正文是空的,即应该填充的 $msg 不是吗?

I switched over the placement of "Contact Form Request" and $msg and the $msg contents would be received in the email subject header so it is being populated, however the message body remains empty.我切换了“联系表单请求”和 $msg 的位置,$msg 内容将在 email 主题 header 中收到,因此它正在被填充,但消息正文仍然为空。

Any help would be appreciate.任何帮助将不胜感激。

contact.html联系人.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

<?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");

?>

Your code looks OK on first inspection (not SAFE, but working).您的代码在第一次检查时看起来不错(不是安全的,但可以正常工作)。

My advise:我的建议:

FIRST: Debug.第一:调试。 Just show the message instead of actual mailing.只显示消息而不是实际邮寄。 Like hereunder:如下:

<?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");
?>

If your message contains what you expect, you must look into the email gateway.如果您的消息包含您期望的内容,您必须查看电子邮件网关。 This is much harder.这要困难得多。

I would start by making a script like this, call it testmail.php with only this inside, and see if that works.我会首先制作一个这样的脚本,将它称为 testmail.php,里面只有这个,然后看看它是否有效。

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

EDIT: Third thing to check:编辑:要检查的第三件事:

You are sending a plain text mail.您正在发送纯文本邮件。 Maybe your email client only displays HTML email?也许您的电子邮件客户端只显示 HTML 电子邮件? Please check.请检查。

EDIT: Fourth suggestion:编辑:第四个建议:

I would try the mail utility itself from commandline to check if your PHP is acting strange.我会从命令行尝试邮件实用程序本身,以检查您的 PHP 是否表现得很奇怪。 So if you have SHELL access (BASH or whatever) try something like this:因此,如果您有 SHELL 访问权限(BASH 或其他),请尝试以下操作:

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

If THAT fails too, go have a chat with your hosting provider.如果这也失败了,请与您的托管服务提供商聊天。

If that works: Something is wrong with your PHP in combination with sendmail.如果可行:您的 PHP 与 sendmail 组合出现问题。

Here is some background: https://www.binarytides.com/linux-mail-command-examples/这是一些背景: https : //www.binarytides.com/linux-mail-command-examples/

Set from email with the header.从带有标题的电子邮件中设置。 You can also use content-type text/html if message in HTML format如果消息为 HTML 格式,您还可以使用 content-type 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);

Hope this work希望这项工作

I have the same error, I think that the mail function interprets the text:我有同样的错误,我认为邮件 function 解释文本:

Message: $msg

as header, this may be a bug in PHP or the way SMTP works.作为 header,这可能是 PHP 或 SMTP 工作方式中的错误。

Here is my code:这是我的代码:

function send_email($to, $subject, $message) {
    $headers[] = 'from: kurs@jcubic.pl';
    $headers[] = 'MIME-Version: 1.0';
    $headers[] = 'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $message, implode(PHP_EOL, $headers));
}

send_email("mail@example.com", 'Subscription', "Email: $email");

it works fine when I remove the colon from the message.当我从消息中删除冒号时它工作正常。

send_email("mail@example.com", 'Subscription', "Email $email");

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

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