How EI sends emails (SMTP and PHP Mailer)

Sep 25, 2025

On EI, there are several use cases where communicating with users through email is important. While registering, an activation link or OTP is sent (I am still deciding which one to use, OTP or activation link). I am using PHP as my base language. I am using no pre-made frameworks. Framework I am using is made by myself in production of EI (so it doesn’t really have any documentation – though I might work on one in the future).

I am using the PHP Mailer and an SMTP server. Acquiring PHP Mailer is pretty simple. You must have Composer installed on your machine. Once Composer is installed, it creates a /vendor folder with tons of dependencies in it. Mailer is one of them.

For the SMTP mailing service, I had several options. Two options seem relevant for my use case: Google’s Gmail SMTP or Hostinger’s Business Email plan. For testing, I am using Google’s SMTP server service as it is enough for testing. It has a 500-mail daily limit (which is more than enough to use in production) – it is free.

Surprisingly, acquiring Google’s SMTP isn’t explained in adequate detail on the internet. There is a lot of confusion going around about the topic. Most online forums or discussions fail to explain that to set up SMTP, we must navigate to Google Account’s settings and NOT Gmail’s settings. Gmail settings look like this:

You don’t get the SMTP features here, instead I had to navigate to “My Google Account’s” settings through the “Manage Google Account” page. The index page of these settings should look like this:

From here, most platforms online ask us to navigate to the Security tab and then to navigate to “App Passwords” tab. In my experience, I wasn’t able to find “App Passwords” in the security tab. It is much more efficient to simply search for “App Passwords” in the settings. Once in the App Passwords settings, something like this should appear:

In my tries, occasionally when navigating to this page, Google signs out and asks for the account password. I found no specific reasons as to why Google was doing this, but signing in again on the same page solves the problem. On Reddit, several users got an error stating they must enable two-factor-authentication to use this feature. I already had 2FA enabled so I didn’t get the error. If you do encounter it, simply enable 2FA in the same settings (I would suggest you add an Authenticator App).

Here I entered the App Name. It could’ve been anything, it doesn’t really matter. An app password is generated. This is a 16-character string with random characters. This can not only be used for SMTP but having this password gives anybody full access to your Google account if they also know your email. This is dangerous. Anyway, I copied this password as I would’ve needed it later. Let’s say the password I got was xxxx xxxx xxxx xxxx.

In the EI directories, I made a new file called mailer.php. In this, I put this code (replace Port with the port Google SMTP is using (usually 465 with SSL and 587 with TLS), Username with the Google email and Password with the 16-character code we got earlier):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?php   use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception;   require __DIR__ . "/../vendor/autoload.php";   $mail = new PHPMailer(true);   $mail->isSMTP(); $mail->SMTPAuth = true;   $mail->Host = "smtp.gmail.com"; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = xxx; $mail->Username = "xxxx@gmail.com"; $mail->Password = "xxxx xxxx xxxx xxxx";   $mail->isHtml(true);   return $mail;

Putting the sensitive details in mailer.php is not a good idea (as I have done in this code). These are hard-coded values. I will later put these in a .config file and mailer.php will fetch these details dynamically from the config file (for now, I let them stay like this).

This is our mailer ready. I could now call the mailer.php file anywhere and use a code snippet to allow me to send emails to the user. The code snippet I have written is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ... $mail = require __DIR__ . "/mailer.php";         $mail->setFrom("emailAddress@mail.com", "emailName@ei.com");         $mail->addAddress($email);         $mail->Subject = "Subject of the Email";         $mail->Body = <<<END         // body of the mail         END; try {             $mail->send();         } catch (Exception $e) {             error_log("Mailer Error: {$mail->ErrorInfo}");             die("Failed to send the email. Please try again later.");         } ...

In the code, if you are following along, you must replace the emailAddress@mail.com with the email you are using (the one we added in SMTP) and emailName@ei.com with the name of the sender you would like to display (e.g. “no-reply”). I used a try… catch… block to enable me to debug better and to handle bugs and errors more gracefully. The variable $email stores the users email I am sending this email to.

This was this. I thought I should post about it since I couldn’t really come up with any interesting blog ideas (and also my Physics exams are coming near, so I am spending more time studying).

-faiq

ComputersEdgeItTech




Comments

Leave a comment