Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

<?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>");  } ?> 
like image 537
skb Avatar asked Apr 03 '09 02:04

skb


People also ask

How send Gmail via SMTP?

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.

Can we send email from localhost PHP?

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.


2 Answers

// 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>'); } 
like image 142
pavan kumar Avatar answered Sep 19 '22 23:09

pavan kumar


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); ?> 
like image 39
shasi kanth Avatar answered Sep 21 '22 23:09

shasi kanth