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:
<?php require_once "Mail.php"; $from = "Sandra Sender <[email protected]>"; $to = "Ramona Recipient <[email protected]>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "smtp.gmail.com"; $port = "587"; $username = "[email protected]"; $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>"); } ?>
Set up the app or device with the Gmail SMTP serverOn your device or in the app, enter smtp.gmail.com as the server address. In the Port field, enter one of the following numbers: If you're using SSL, enter 465. If you're using TLS, enter 587.
In the PHP web application, mail() function used to send the mail. But mail() function will not work in the localhost environment. In this tutorial, we will send an email from the localhost system using PHP and Gmail. In this tutorial, we will use PHPmailer to send email from the localhost using PHP.
// Pear Mail Library require_once "Mail.php"; $from = '<[email protected]>'; $to = '<[email protected]>'; $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' => '[email protected]', 'password' => 'passwordxxx' )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo('<p>' . $mail->getMessage() . '</p>'); } else { echo('<p>Message successfully sent!</p>'); }
Using Swift mailer, it is quite easy to send a mail through 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('[email protected]' => 'ABC')) ->setTo(array('[email protected]')) ->setBody('This is a test mail.'); $result = $mailer->send($message); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With