Can't send an email. Not sure what is going wrong and haven't found an answer through google yet.

$mail = new PHPMailer();
        $mail->SMTPAuth     = true;
        $mail->SMTPSecure   = "tls";
        $mail->Host         = "smtp.gmail.com";
        $mail->Port         = 587;
        $mail->Username     = "myemail@gmail.com";
        $mail->Password     = "mypassword";
        $mail->AddAddress("myemail@gmail.com"); // sending an email to myself to check that it works.
        $mail->Subject      = "Password change for pset7";
        $mail->Body         = "Hello. Here's a link: pset7/password_mail.php?token=a";
            apologize("Something went wrong with sending the link");

Edit: Just leaving this here incase someone else has a similar problem with gmail.

On an account that didn't have 2 step authentication I had to enable access for less secure apps before I was able to send mail with phpmailer. You can also debug phpmailer by setting SMTPDebug to 2 or higher. Something like:

$mail->SMTPDebug = 2;
  • Are you using two step authentication for your Gmail account?
    – ChrisG
    Aug 8 '15 at 7:39
  • Yep. I am using it.
    – Stalris
    Aug 8 '15 at 11:43

If you are using two step authentication for your Gmail account, you have to create a one time password and use this one, as you can't use the two step authentication with phpmailer.


I used single quotes and it works for me. Use single quotes for everything except email, password, body, and subject.

  • Same error message. Everything but those 4 fields has single quotes instead of double quotes, if they need any quotes.
    – Stalris
    Aug 8 '15 at 3:34
  • What phpmailer files did you include?
    – i_am_david
    Aug 8 '15 at 4:13
  • require_once("libphp-phpmailer/class.phpmailer.php");
    – Stalris
    Aug 8 '15 at 11:44
  • You need to include the autoload file and the class.smtp file also.
    – i_am_david
    Aug 8 '15 at 16:43
  • Wow fine be like that
    – i_am_david
    Aug 8 '15 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .