Reputation: 714
Since I found that the in-build mail function in PHP has security vulnerabilities I tried to use PEAR. I have installed & made the necessary configuration on my localhost (WAMP server 2.2). However each time I try to send an email the following message is displayed.
error: authentication failure [SMTP: Invalid response code received from server (code: 535, response: 5.7.8 Username and Password not accepted. Learn more at 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 gg10sm16772067pbc.46 - gsmtp)]
.
Both the username and password is correct I have checked it over & over again. I have checked online documents & similar questions by other members but I am still stuck on this issue. Any insights to this problem is greatly appreciated. And by the way here is the PHP code that I used to send an email.
<?php
require "Mail.php";
$from = "[email protected]";
$to = "[email protected]";
$subject= "hiii";
$body = "\n\nEmail contents here";
$host = "ssl://smtp.gmail.com";//"smtp.gmail.com";
$port = "465";//"587";
$user = "my username";
$pass = "mypassword";
$headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
$smtp = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
$mail = @$smtp->send($to, $headers, $body);
if(PEAR::isError($mail)){
echo "error: {$mail->getMessage()}";
}else{
echo "Message sent";
}
?>
Upvotes: 4
Views: 11302
Reputation: 3844
The username that you use for creating the SMTP object needs to be your full gmail email address, e.g. [email protected] and the host variable should just be "smtp.gmail.com" - it doesn't need to start with "ssl://"
This will result in an email being sent with no problems. (I put your code into a file named 20031009.php, fixed the errors, made a few other changes so it would work with my gmail account and ran it, the following is the result.)
$ php 20031009.php
Message sent
On another note, it looks like you need to swap around the values for $from and $to. :)
This is the working code, in it's entirety (with account and email details changed back)
require "Mail.php";
$to = "[email protected]";
$from = "[email protected]"; // the email address
$subject = "hiii";
$body = "\n\nEmail contents here";
$host = "smtp.gmail.com";
$port = "587";
$user = "my username";
$pass = "mypassword";
$headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject);
$smtp = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass));
$mail = @$smtp->send($to, $headers, $body);
if (PEAR::isError($mail)){
echo "error: {$mail->getMessage()}";
} else {
echo "Message sent";
}
Upvotes: 3