-2

I want to send mail from local server but mail will not send locally. The error is:

SMTP Error: Could not authenticate.

<?php
include_once'mail/class.phpmailer.php';
include_once'mail/class.pop3.php';
include_once'mail/class.smtp.php';
class MailSender{
    public static function SendMail($to,$subj,$body,$username){
        $mail=new PHPMailer();
        $mail->IsSMTP();
        //$mail->Host="mail.gmail.com";
        $mail->SMTPAuth=true;
        $mail->SMTPSecure = 'ssl';
        $mail->Host="smtp.gmail.com";
        $mail->Port=587;
        $mail->Username="rahullodhi3636@gmail.com";
        $mail->Password="fdslkfsd";
        $mail->SetFrom('rahullodhi3636@gmail.com','Rahul lodhi');
        $mail->Subject=$subj;
        $mail->MsgHTML($body);
        $mail->AltBody="to view the msg";
        $address=$to;
        $mail->AddAddress($address,$username);
        if(!$mail->Send()){
            return"Mailer Error :".$mail->ErrorInfo;
            }else{
                return"message sent";
                }
        }
    }
?>
Moppo
  • 18,797
  • 5
  • 65
  • 64

1 Answers1

2

The port you should use is 465 (SSL) instead of 587 (TLS).

Also try to use the following Host value:

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

javierfdezg
  • 2,087
  • 1
  • 22
  • 31