What goes in the username and password fields for PHPMailer?

7.5k views Asked by At

I'm trying to make one of those commonly known contact forms for a website, so that the user can directly send me an email from my website.

Here is the code I have so far

<html>


<body>


<?php
require("PHPMailer_5.2.0/class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';


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

//$mail->IsHTML(true); // if you are going to send HTML formatted emails

$mail->From = "[email protected]";
$mail->FromName = "FROM NAME";

$mail->addAddress("[email protected]","Vik Kalkat");

$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";

if(!$mail->Send())
    echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
    echo "Message has been sent";

?>


</body>


</html>

The error I am getting is

SMTP Error: Could not connect to SMTP host. Message was not sent 
PHPMailer Error: SMTP Error: Could not connect to SMTP host.

I'm guessing this may be because my "Username" and "Password" parameters might be wrong since I'm unsure what username and password is suppose to go here.
Is it the login information for the email account to which I want the emails to be sent to?

3

There are 3 answers

0
Kasper Sanguesa-Franz On

You should try using the TLS one

which is on port 587.

Change

$mail->Port = 465;
$mail->SMTPSecure = 'ssl';

to

$mail->Port = 587;
$mail->SMTPSecure = 'tls';

another issue might be that your Mailer is 'smtp' instead of 'mail'

1
Nouman Ahmad On

Go to your google account and open the security option on the left side. After that scroll down the page and look for option Allow the less secure app and turn that option on.

Try again by refreshing your page and this time you will be able to send the Email using Gmail. Happy coding.

1
Chintan Sudani On

Please change your port number from 465 to 587. and if you are use SMTP mailer then change SMTPSecure from ssl to tls.