Search code examples
phphtmlsmtpphpmailer

PHPMailer $mail->send() showing results on page


I have PHPMailer working through Gmail. I've sent emails to myself to test that it's working, and it is. The user submits a signup form (very basic setup) on index.php, which then triggers sending the email.

My problem is that after submitting the form, it "echoes" to the page the process that it's going through: to give a sense of it, here's the start (3000 characters so I'm only including a bit):

2017-03-01 21:25:36 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( ) 2017-03-01 21:25:36 Connection: opened 2017-03-01 21:25:36 SERVER -> CLIENT: 220 smtp.gmail.com ...

This shows up directly on the page. I'm sure I could get around it by redirecting to another page on success, but it seems much easier to simply not print all of the information to the page in the first place. I've tested my code and it's definitely the $mail->send(); command that's triggering it.

Here's the code I'm using to call PHPMailer (it's inside <?php and ?> tags.

require_once "phpmailer/PHPMailerAutoload.php";

$mail = new PHPMailer;

$mail->SMTPDebug = 3;
$mail->isSMTP();                                   
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;                               
$mail->Username = "[email protected]";                 
$mail->Password = "mypassword";                           
$mail->SMTPSecure = "tls";
$mail->Port = 587;     

$mail->From = "[email protected]";
$mail->FromName = "my name";
[![enter image description here][1]][1]
$mail->addAddress("[email protected]", "their name");

$mail->isHTML(true);

$mail->Subject = "Testing email for phpmailer";
$mail->Body = "<i>Mail body in HTML</i>. It worked!";
$mail->AltBody = "This is the plain text version of the email content. Also, it worked!";

if($mail->send()) 
{
    echo "Message has been sent successfully";
} 
else 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}

Misc. system information if it helps: I'm using XAMPP on Windows 10, running in localhost. I'm connected to my Gmail account.

Here's what the problem looks like on the page (I didn't screenshot much as I'm unsure how much is sensitive information): Screenshot of the problem


Solution

  • PhpMailer have option to debug.

    $mail->SMTPDebug  // enables SMTP debug information (for testing)
    
    // 1 = errors and messages
    
    // 2 = messages only
    

    Comment $mail->SMTPDebug = 3; in the code.