Sending email in PHP using Gmail’s SMTP server

A practical use case of the PHPMailer class in which we send mail from PHP using the Gmail SMTP server.

In the previous PHP workshop article we explained a few and saw how easy it was to send an email with advanced features like attaching files. But there is an example that we left out that I think is worth dealing with independently: sending email from PHP, but using a different SMTP server than the one you have configured in your php.ini.

One of the curiosities, or utilities, of PHPMailer is the ability to send email using Gmail’s SMTP server. It can be useful when you don’t have your own SMTP server, although personally I have my reservations and that’s why I refer to it more as a curiosity than anything else.

However, the example of sending email using Gmail’s sending servers is interesting because it illustrates one of the needs that we may have in some cases, such as the use of an SMTP server that requires authentication.

Note: If you don’t have a local SMTP server to send emails in PHP, there are various programs that provide you with this function, such as the one we discussed at .com years ago. Most hosting servers (or at least all the ones I’ve used) have an SMTP server available that you can use from PHP. What I have come across on occasion is a hosting provider that requires SMTP authentication to send emails from PHP. For all these cases, PHPMailer is an exceptional ally!

The idea is very simple, use the data of a Gmail email account to send the messages, indicating in PHPMailer the data of that Google account that you are going to use.

See also  RSS readers

Note: This time I have simplified the code of the example somewhat, since part of the code is commented in detail in the introduction to PHPMailer article. The example that I am commenting below is based on the code that we found in the PHPMailer download, in the test_smtp_gmail_basic.php file in the “examples” folder.

require_once(‘../class.phpmailer.php’);
$mail = new PHPMailer();
// tell the class to use SMTP
$mail>IsSMTP();
//allows debug mode to see messages of things that are happening
$mail>SMTPDebug = 2;
// I have to do SMTP authentication
$mail>SMTPAuth = true;
$mail>SMTPSecure = “ssl”;
//indicate the Gmail server for SMTP
$mail>Host = “smtp.gmail.com”;
//indicate the port that Gmail uses
$mail>Port = 465;
//indicate a username / password of a gmail user
$mail>Username = “your_email_gmail@gmail.com”;
$mail>Password = “your password”;
$mail>SetFrom(‘your_email_gmail@gmail.com’, ‘Full name’);
$mail>AddReplyTo(“your_email_gmail@gmail.com”,”Full name”);
$mail>Subject = “Sending email using Gmail SMTP”;
$mail>MsgHTML(“Hello, how are you, this is the body of the message!”);
//indicate recipient
$address = “recipient@delcorreoe.com”;
$mail>AddAddress($address, “Full name”);
if(!$mail>Send()) {
echo “Error sending: ” . $mail>ErrorInfo;
} else {
echo “Message sent!”;
}

This code is commented, so it can be understood quite well. Just call attention to the issue of the “debug” of the SMTP service $mail>SMTPDebug = 2; With this we indicate that it shows us messages from the communication process with the SMTP server that we are using, as well as possible errors. We could also place the number 1 as a value, to tell it to only show us error messages found. Or, if we do not assign anything in the SMTPDebug property, it will not show us any additional messages to those that PHPMailer already shows by default.

See also  Theoretical introduction to observables in Angular

Note: Using a similar script we could also send email from PHP using SMTP servers from other free providers like Hotmail (now Outlook.com) or whatever you have. You simply have to know the configuration data of that service (email address, SMTP server, username and password, as well as the port). It would simply be a matter of indicating all the data of that email account that you intend to use.

Possible problems using another authenticated SMTP server from PHP

This example of using PHPMailer can lead to various problems that may cause us to lose a few hours of work. Nothing that can’t be solved with some testing and a bit of calm, but to make life easier for you, I’ll tell you how it went for me.

I have managed to make this example work on two different computers. On my Linux computer, where I have installed PHP following the instructions in the article, I have successfully run the script and it has sent me the mail the first time!

But of course, like everything in life, things don’t always work out the first time and on my other computer, with a Windows system where I have PHP installed through Xampp, I haven’t had the same luck. However, the solution is quite simple.

Note: The first thing, of course, is to check that your Gmail account is perfectly specified. I mean the Gmail username and password, which you must have indicated in the $mail>Username and $mail>Password properties. The second thing is to verify the port, which in my case has worked with the value 465.

See also  Check marks on your website

From Windows system with Xampp I get the following error message “Unable to find the socket transport ‘ssl’ did you forget to enable it when you configured PHP?”

The solution is to activate the openssl PHP extension, for which I have uncommented the line in the php.ini (removed the semicolon “;” at the beginning):

extension=php_openssl.dll

After that small configuration, and restarting Apache (of course), email can be sent without problems from my Xampp installed on Windows.

Conclusion on PHPMailer

If you have ever tried to send an attachment in an email with PHP, you will have noticed that it was not something immediate. If you’ve ever tried to use an SMTP server from PHP that requires authentication, you’ve probably gone crazy trying to configure PHP. If you ever wanted to send an email in PHP using an SMTP server not configured in the php.ini, you may have hit a wall.

If you want to overcome all these barriers and surely many others, you have the option of using PHPMailer which works very well, simplifies advanced email sending tasks and supports many things that you might not know how to do otherwise.

This article has started from the base that you knew the , related in a previous publication. So we recommend you read that article again if you think that we have left certain things up in the air. But all in all, we think PHPMailer is an important class to consider.

Loading Facebook Comments ...
Loading Disqus Comments ...