PHP Mail Using Gmail

Send email using the GMail SMTP server from a PHP page

// 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>');
}

How do I send emails through PHP to GMail?

Use PHPMailer library.It is better than use mail native function because in PHPMailer you can use user authentication to avoid to send the email to spam.You can use this library without to configure a mail server. You can download in this link https://github.com/PHPMailer/PHPMailer

See an example:

$mail             = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 587; // SMTP port
$mail->Username = "yourusername@gmail.com"; // username
$mail->Password = "yourpassword"; // password

$mail->SetFrom('user@gmail.com', 'Test');

$mail->Subject = "I hope this works!";

$mail->MsgHTML('Blah');

$address = "test@test.com";
$mail->AddAddress($address, "Test");

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Can't Send Email through PHP on localhost using Gmail account

I am going to assume you are connecting to Gmail via the smtp server.

If you are currently using the actual password for the gmail account you are connecting to. The the best thing to do would be to try creating an apps password and using that.

Sign in with App Passwords

$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.example.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'user@example.com'; //SMTP username
$mail->Password = 'APPsPassword'; //New apps password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465;

Another option would be to switch to Xoauth or switching to the Gmail api again using Oauth2.

$mail->oauthUserEmail = "[Redacted]@gmail.com";
$mail->oauthClientId = "[Redacted]";
$mail->oauthClientSecret = "[Redacted]";
$mail->oauthRefreshToken = "[Redacted]";

PHP mail using Gmail

Gmail's SMTP-server requires a very specific configuration.

From Gmail help:

Outgoing Mail (SMTP) Server (requires TLS)
- smtp.gmail.com
- Use Authentication: Yes
- Use STARTTLS: Yes (some clients call this SSL)
- Port: 465 or 587
Account Name: your full email address (including @gmail.com)
Email Address: your email address (username@gmail.com)
Password: your Gmail password

You can probably set these settings up in Pear::Mail or PHPMailer. Check out their documentation for more details.

Use php mailer to send via Gmail - SMTP error

This is possible duplicate of Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?

Probably you didn't allow for less secured apps in Gmail account security dashboard.

How to send mail using php to gmail account using SMTP

In my setup I also have this:

$mail->SMTPSecure = "ssl";
$mail->Port = 465;

Note that you will probably need to authorise this action on your account. You'll get an email from gmail with a link.

Gmail SMTP server stopped working as it no longer support Less Secure Apps

Yes, It's not working after removing the option by google. But nothing to worry! It's still very simple to send email. To send email again you need to do this as bellow:

  1. Login to your gmail
  2. Go to Security setting and Enable 2 factor authentication
  3. After enabling this you can see app passwords option. Click here!
  4. And then, from Your app passwords tab select Other option and put your app name and click GENERATE button to get new app password.
  5. Finally copy the 16 digit of password and click done. Now use this password instead of email password to send mail via your app.

Now you can use just email and this generated pass to send email.



Related Topics



Leave a reply



Submit