Saturday 6 February 2016

Send email using the GMail SMTP server from a PHP page




I am trying to send an email via GMail's SMTP server from a PHP page, but I get this error:




authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: mx.google.com at your service, [98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]




Can anyone help? Here is my code:




require_once "Mail.php";

$from = "Sandra Sender ";
$to = "Ramona Recipient ";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "smtp.gmail.com";
$port = "587";
$username = "testtest@gmail.com";

$password = "testtest";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,

'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("

" . $mail->getMessage() . "

");
} else {
echo("

Message successfully sent!

");
}
?>


Answer



// Pear Mail Library
require_once "Mail.php";

$from = '';
$to = '';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";


$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,

'username' => 'johndoe@gmail.com',
'password' => 'passwordxxx'
));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo('

' . $mail->getMessage() . '

');
} else {
echo('

Message successfully sent!

');

}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...