Email from PHP page using GMail SMTP server

I am trying to send e-mail through GMail's SMTP server from a PHP page, but this error occurred:

Authentication failed [SMTP: SMTP server does not support authentication (Code: 250, response: mx.google.com, service for you, [98.117.99.235] size is 35651584 8-bit mime starttls enhancedstatusscodes pipelining]]

Can I help you? This is my code:

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <ramona@microsoft.com>";
$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("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");
 }
?>

#1 building

Gmail requires port 465, which is also the code of phpmailer:)

#2 building

Use Swift mailer , it's very easy to send mail via Gmail credentials:

<?php
require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername('GMAIL_USERNAME')
  ->setPassword('GMAIL_PASSWORD');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test Subject')
  ->setFrom(array('abc@example.com' => 'ABC'))
  ->setTo(array('xyz@test.com'))
  ->setBody('This is a test mail.');

$result = $mailer->send($message);
?>

#3 building

I don't recommend Pear Mail. No updates have been made since 2010. The source code is almost obsolete, written in the PHP 4 style, and many errors / errors have been published (Google it). I'm using Swift Mailer.

Swift Mailer is integrated into any Web application written in PHP 5, providing a flexible and elegant object-oriented method to send e-mail with multiple functions.

Send email using SMTP, sendmail, postfix or your own custom transport implementation.

Supports servers that require user names and passwords and / or encryption.

It is immune from header injection attack without splitting the requested data content.

Send MIME compliant HTML / multipart email.

Use event driven plug-ins to customize the library.

Processing large attachments and embedded / embedded images uses less memory.

It is a free open source code that you can Download Swift Mailer And upload to your server. (the feature list is copied from the owner's website.).

Examples of Gmail SSL / SMTP and Swift Mailer work here

// Swift Mailer Library
require_once '../path/to/lib/swift_required.php';

// Mail Transport
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
    ->setUsername('username@gmail.com') // Your Gmail Username
    ->setPassword('my_secure_gmail_password'); // Your Gmail Password

// Mailer
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject Here')
    ->setFrom(array('sender@example.com' => 'Sender Name')) // can be $_POST['email'] etc...
    ->setTo(array('receiver@example.com' => 'Receiver Name')) // your email / multiple supported.
    ->setBody('Here is the <strong>message</strong> itself. It can be text or <h1>HTML</h1>.', 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo 'Mail sent successfully.';
} else {
    echo 'I am sure, your configuration are not correct. :(';
}

I hope it helps. Happy code...:)

#4 building

The code listed in the question requires two changes

$host = "ssl://smtp.gmail.com";
$port = "465";

Port 465 is required for SSL connections.

#5 building

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

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$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('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}

Tags: PHP Swift SSL Google

Posted on Thu, 13 Feb 2020 02:45:46 -0500 by ryanlwh