Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHPMailer: Mailer Error: Message body empty

ErrorMailer Error: Message body empty

Short answer

PHPMailer refuses to send when Body is empty. The check looks only at Body, so a message with just AltBody fails, and so does a Body of "0" or a variable that came back empty. Set Body (note the capital B) before send(), or set AllowEmpty to true for a message you really want empty.

  • Applies to PHP 8.3, PHPMailer 6.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print Body right before send(). Wrap it in var_dump so an empty string, null and "0" can be told apart.

    var_dump($mail->Body);
    var_dump($mail->AltBody);
  2. 2Match what you see, then jump to its fix

    1. 1var_dump($mail->Body) prints string(0) ""Body was never set, or was set with the wrong property nameFix
    2. 2AltBody has text, Body is emptyOnly AltBody is setFix
    3. 3Body is built from a template, a form field or a database row, and it is empty in this request onlyThe value that fills Body is empty at run timeFix
    4. 4The mail is a signal with a subject only, and an empty Body is deliberateAn empty message is intendedFix

What does the error look like?

  • Mailer Error: Message body empty

Cause 1 of 4

Body was never set, or was set with the wrong property name

1Check

The property is named Body, with a capital B, as in the README example. A line such as $mail->body = ... or $mail->Message = ... sets something else and leaves Body empty.

grep -rn "mail->[Bb]ody\|->Message" src/

2Fix

Assign the content to Body. For an HTML mail also call isHTML(true), and keep a plain-text copy in AltBody.

$mail->isHTML(true);
$mail->Body    = '<p>Hello, your order shipped.</p>';
$mail->AltBody = 'Hello, your order shipped.';
$mail->send();

3Verify

send() returns true and ErrorInfo stays empty.

var_dump($mail->send(), $mail->ErrorInfo);

Confirmed by github.com

Did this fix it?

Cause 2 of 4

Only AltBody is set

1Check

In preSend() the empty-message check tests Body alone, so AltBody does not satisfy it.

var_dump(empty($mail->Body), empty($mail->AltBody));

2Fix

Put the main content in Body. If the mail is plain text, leave isHTML off and set Body only.

$mail->isHTML(false);
$mail->Body = 'Plain text message';

3Verify

send() returns true.

var_dump($mail->send());

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

The value that fills Body is empty at run time

1Check

The check uses PHP's empty(), so an empty string, null and the string "0" all count as empty. Print the source value one step before it is assigned.

$html = file_get_contents(__DIR__ . '/mail/welcome.html');
var_dump($html);

2Fix

Fix the source (wrong template path, missing POST field, query that returned nothing) and stop the script with a clear message when it is empty.

if ($html === false || $html === '') {
    throw new RuntimeException('Mail template is empty');
}
$mail->Body = $html;

3Verify

The script either sends the mail or fails with your own message, never with PHPMailer's.

var_dump($mail->send());

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

An empty message is intended

1Check

AllowEmpty is the property that decides whether a message with an empty body may be sent; the empty-body check only fires when it is false.

var_dump($mail->AllowEmpty);

2Fix

Turn it on for that message.

$mail->AllowEmpty = true;

3Verify

send() returns true without a body.

var_dump($mail->send());

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Copy the property names from the README example, including the capitals.
  • Build the body in one place and check it is not empty before touching the mailer.
  • Keep ErrorInfo in your logs, so the failure is visible without a debug session.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Email

PHPMailer addCC() adds no CC recipient

The mail arrives for the To address but not for the CC address

addCC() returns false when it rejects the address, and PHPMailer then sends without that CC.

3 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.