PHPMailer: Mailer Error: Message body empty
Mailer Error: Message body emptyShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
var_dump($mail->Body) prints string(0) ""Body was never set, or was set with the wrong property nameFix - 2
AltBody has text, Body is emptyOnly AltBody is setFix - 3
Body 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
The mail is a signal with a subject only, and an empty Body is deliberateAn empty message is intendedFix
- 1
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
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
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
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
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
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 fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix