Janie
Janie

Reputation: 646

Yii phpmailer not working for gmail

I'm using Yii phpmailer extension to send mail using gmail but it showing SMTP Error: Could not authenticate. instead of sending mail. My code what I used

    Yii::import('application.extensions.phpmailer.JPhpMailer');
    $mail = new JPhpMailer;
    $mail->IsSMTP();
    //$mail->Host = 'smpt.163.com';
    $mail->Host = 'smtp.googlemail.com:465';
    //$mail->Host = 'smtp.gmail.com:587';
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Username = $email;
    $mail->Password = $pass;
    $mail->SetFrom($email, SiteConfig::SITE_TITLE);
    $mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $mail->MsgHTML('<h1>JUST A TEST!</h1>');
    $mail->AddAddress($email, 'My Name');
    $mail->Send();

I followed this link.

How can I solve this problem and send mail using gmail? I’d appreciate any light you can shed on this!!

Upvotes: 1

Views: 399

Answers (3)

Juan
Juan

Reputation: 118

Be sure to set the following attributes:

$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure =  "tls";
$mail->SMTPAuth = true;
$mail->Username =  "your username";
$mail->Password = "your password";

It should work with those attributes set.

Hope it helps.

Upvotes: 0

Ravi Kadia
Ravi Kadia

Reputation: 275

You can debug and get error report

for 1 error and message for 2 message only

$mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only

Upvotes: 0

M1ke
M1ke

Reputation: 6406

Try adding the following line:

$mail->Port = 465;

I am unsure if adding :port works for the Host field as you've done - I can confirm that setting the Port field manually does work, as I have a large system whose code looks very similar to yours which is working as expected.

We also use $mail->Host = 'smtp.gmail.com' but I am almost certain that the smtp.googlemail.com domain should also work.

Upvotes: 1

Related Questions