Search code examples
phpsmtpgmailphpmailer

phpmailer not working with gmail


i ve been trying to test sending emails from php using the phpmailer class. my config is this:

require '../php/library/class.phpmailer.php';
require '../php/library/class.smtp.php';

$mail = new PHPMailer;

$mail->isSMTP();

$mail->SMTPDebug = 2;

$mail->Debugoutput = 'html';

$mail->Host = 'smtp.gmail.com';

$mail->Port = 587;

$mail->SMTPSecure = 'ssl';

$mail->SMTPAuth = true;

$mail->Username = "[email protected]";

$mail->Password = "thepassword";

$mail->setFrom('[email protected]', 'Real Name');

$mail->addAddress( $validEmail );

$mail->Subject = 'Confirmation Code';

$mail->msgHTML('Follow this link: <a href="http://test.com/confirm?code=' . $newAccount->confirmationCode . '">http://test.com/confirm?code=' . $newAccount->confirmationCode . '</a>');

$mail->AltBody = 'This is a plain-text message body with this link: http://test.com/confirm?code=' . $newAccount->confirmationCode;

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

However, things do not seem to work, and i just copy pasted the code from the gmail example. i ve searched the world wide web for the error output:

SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP connect() failed.
Mailer Error: SMTP connect() failed.

but still can not find out wha't wrong. any help please?

UPDATE i have disabled the two step verification in gmail


Solution

  • Try starting with the gmail example code provided with phpmailer rather than the old code you've used. https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

    Then check out the troubleshooting guide here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting