Search code examples
phpemailphpmailer

trouble sending mail using phpmailer in localhost XAMPP


hello guys i'm trying to sending mail in my localhost using phpmailer. whenever i run the code it appears an error like this :

Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

i already tried to change php.ini file uncomment the "extension=php_openssl.dll" and i even tried to changed it to 587 but it didn't work.

<?php
    $mailto = $_POST['mail_to'];
    $mailSub = $_POST['mail_sub'];
    $mailMsg = $_POST['mail_msg'];
   require 'phpmailer/PHPMailerAutoload.php';
   $mail = new PHPMailer();
   $mail ->IsSmtp();
   $mail ->SMTPDebug = 0;
   $mail ->SMTPAuth = true;
   $mail ->SMTPSecure = 'ssl';
   $mail ->Host = "smtp.gmail.com";
   $mail ->Port = 465; //587
   $mail ->IsHTML(true);
   $mail ->Username = "[email protected]";
   $mail ->Password = "mypassword";
   $mail ->SetFrom("[email protected]");
   $mail ->Subject = $mailSub;
   $mail ->Body = $mailMsg;
   $mail ->AddAddress($mailto);

   if(!$mail->Send())
   {
       echo "Mail Not Sent";
       echo "<br>";
       echo 'Mailer Error: ' . $mail->ErrorInfo;;
   }
   else
   {
       echo "Mail Sent";
   }

please help me solve this problem guys


Solution

  • This is a common issue when setting up a webserver or website to send over SMTP via gmail.

    If you provisioned the gmail account, be sure to turn on the setting that says "verify less secure apps" on gmail. Refer to this blog post with screenshots indicating where to configure this: http://www.chriswrites.com/how-to-fix-gmail-authentication-issues/