Send email using the GMail SMTP server from a PHP page

Cover Image for Send email using the GMail SMTP server from a PHP page
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Sending Email using GMail SMTP Server from a PHP Page: Common Issues and Easy Solutions

So, you're trying to send an email through GMail's SMTP server from a PHP page, but you're running into some trouble? No worries, we've got you covered! 📧💥

The Error and the Code 😱🔍

The error you're encountering looks like this:

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)]

This error message clearly states that the SMTP server doesn't support authentication. The code you provided seems to be correct, and you're using the Mail package to send the email. But why are you still getting this error? Let's find out!

The Issue: Insecure Connection 😬🔐

The problem lies in the connection settings of the SMTP server. By default, GMail's SMTP server requires a secure (TLS) connection to authenticate. However, the code you provided doesn't specify any encryption.

The Solution: Enabling Encryption 🔒🚀

To fix this issue, you need to enable encryption in your PHP code. You can do this by adding the following line before calling $smtp->send:

$smtp->startTLS();

This line tells the Mail package to initiate a secure connection using TLS encryption.

Updated PHP Code 💻✨

Here's the updated version of your PHP code with the necessary change:

<?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
));

$smtp->startTLS(); // Enable encryption

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

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}

?>

With the addition of $smtp->startTLS(), your code will now establish a secure connection with GMail's SMTP server, allowing successful email delivery.

Your Turn! 📝🤩

Now that you've got the solution to your problem, it's time to put it into action! 🚀 Update your code with the changes discussed above and enjoy sending emails through GMail's SMTP server using PHP.

If you have any further questions or need additional assistance, feel free to leave a comment below. Happy emailing! 💬✉️


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello