'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). 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).

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 i.e. $msg that should be populated aren't?

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.

Any help would be appreciate.

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 = "[email protected]";

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

?>


Solution 1:[1]

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 = "[email protected]";

$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.

<?php
mail('[email protected]', "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? Please check.

EDIT: Fourth suggestion:

I would try the mail utility itself from commandline to check if your PHP is acting strange. So if you have SHELL access (BASH or whatever) try something like this:

mail -s "Testing mail" [email protected] <<< '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.

Here is some background: https://www.binarytides.com/linux-mail-command-examples/

Solution 2:[2]

Set from email with the header. You can also use content-type text/html if message in HTML format

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

Hope this work

Solution 3:[3]

I have the same error, I think that the mail function interprets the text:

Message: $msg

as header, this may be a bug in PHP or the way SMTP works.

Here is my code:

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

send_email("[email protected]", 'Subscription', "Email: $email");

it works fine when I remove the colon from the message.

send_email("[email protected]", 'Subscription', "Email $email");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Trupti Barad
Solution 3 jcubic