PHPMailer Mailer Error: Message body empty
Asked Answered
P

4

7

I'm trying to get the basic example working for PHPMailer.

All I done was upload the whole PHPMailer_5.2.2 folder, configured the page below as per the code you see and I keep getting Mailer Error: Message body empty, but I can clearly see the contents.html file has html in it and isn't empty. This is the example file I'm using from the PHPMailer PHPMailer_5.2.2/examples/test_smtp_gmail_basic.php

I tried using the settings I have in Outlook for Gmail that works, I know my username and password, the SMTP port is 587 and it's set to TLS, I tried replacing SSL with TLS in the code below, I still get same error.

I also tried the following code, which has been suggested:

changed this:

$mail->MsgHTML($body);

to this

$mail->body = $body;

I still got same error, is there something else I need to configure? It's my first time using PHPMailer, I can get the standard php mail working, but I want to try this because the page I'm going to be emailing has lots of html and I don't want to have to go through all the html and enter character escapes so someone recommending using this.

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = preg_replace('/[\]/','',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "[email protected]";  // GMAIL username
$mail->Password   = "xxx";            // GMAIL password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("xxx","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "xxx.net";
$mail->AddAddress($address, "John Doe");

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>
Percussive answered 11/12, 2012 at 8:48 Comment(9)
Can you paste the result of a var_dump on the body variable? $mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = preg_replace('/[]/','',$body); var_dump($body); $mail->IsSMTP(); // telling the class to use SMTPLonee
It just comes up as null when i run that code, i also tried putting the full url www.mysite.com/PHPMailer_5.2.2/examples/contents.html When i access that URL i can see the htmlPercussive
I tried $body = "this is a test" first time around it refused it, i got an email from google saying it was suspicious and blocked so i went trough some verification steps and now i can send it if the body is plain text, but once i put back: $body = file_get_contents('contents.html'); $body = preg_replace('/[]/','',$body); I still get the empty message and running your code i get NULL The other thing is even though it sent the plain text message i didnt recieve the emailPercussive
Can you post a screenshot/overview of your folder structure?Lonee
Strange, when i removed this line it works fine? $body = preg_replace('/[]/','',$body); is that needed for anything?Percussive
Not really, since you have control over the HTML code you're sending, no replacing should happen.Lonee
Possible duplicate of PHP Mailer Class issue :Message body emptyNessus
@Manuel Hi did you get the correct answer? I'm also facing the same issue. please help me to solve this Thanks in advanceVermilion
@Vermilion if I rememeber correctly (it was 7 year ago), I ditched PHP Mailer for SwiftMailer, these days I'm using Symfony Mailer both standalone and with Symfony FrameworkAbie
D
20

I had the same problem and I solved just changing:

This:

$mail->body = 'Hello, this is my message.';

Into this:

$mail->Body = 'Hello, this is my message.';

body into Body what a little mistake!

Downstairs answered 22/9, 2015 at 22:42 Comment(1)
Damn, thats hurting my eyes. Thanks anyway!Empire
U
1

PHPMailer->MsgHTML() tries to do something clever with fixing non-absolute URLs, but for me too it just returns empty.

$mail->IsHTML(true);
$mail->Body=$body;
Ul answered 2/2, 2013 at 22:59 Comment(0)
D
0

First I had to enable the ssl extension, (which I have obtained from another post on this site). To do that, open php.ini and enable php_openssl.dll by changing

;extension=php_openssl.dll

to

extension=php_openssl.dll

Now, enable tls and use port 587. Then comment out the preg_replace.

Finally, I was able to make it show up in my gmail "Sent" file, although I was expecting to see it in my "Inbox".

This site is awesome! Thanks.

Dougdougal answered 24/1, 2013 at 13:9 Comment(0)
F
0

When setting $mail->Body equal to variable the body is empty. When I attach a string to the body everything works fine. The solution is to strval() a variable containing Your HTML code.

$message;//Your HTML message code
$mail->Body = strval($message);
Frontality answered 24/1, 2022 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.