Google SMTP with Codeigniter

Google SMTP Configuration

A simple guide to setting up Google's SMTP for sending emails.

Step 1: Get an App Password

To use Google's SMTP, you must use a specific App Password instead of your regular Google password. You can generate one by following these steps:

  1. Go to your Google Account settings.
  2. Navigate to Security.
  3. Security
  4. Find 2-Step Verification and make sure it's turned On. This is a requirement.

  5. Under the "Signing in to Google" section, find App Passwords. Or search it like below.
  6. Select a name for your app (e.g., "My Web App") or create one, and click "Generate".
  7. Google will provide a 16-character password.

Google SMTP Server Details

  • Server: smtp.gmail.com
  • Port (SSL): 465
  • Security: (TLS/STARTTLS/ssl)
  • Username: Your full Gmail address
  • Password: The generated App Password

Let's try to check the STMP before going into Codeigniter framework

  1. Vist SMTP Test Tool
  2. Add details by filling the form

It will be something similar to this. 👇

SMTP Test

Test your SMTP server credentials by filling out this form.

We will use the Email library in CodeIgniter 4. This is one of the most used libraries in web application development. Sometimes we need to send an email related to account creation or purchasing a product, like sending an invoice or validating an account creation

If we are going to send an email using our local server, we need to configure SMTP.

We have app/Config/Email.php class. There, we have to configure our SMTP server. Once the configuration is completed, we have to load that email service into our controller.

  1. Let’s configure SMTP first.
  2. app>Config>Email.php

    change the following variables

    
    public $protocoal = “smtp”’;
    public $SMTPHost = “smtp.gmail.com”;
    public $SMTPUser = ‘your email address’;
    public $SMTPPass = ‘App password generated’;
    public $SMTPPort = 465;
    public $SMTPTimeout = 60;
    public $SMTPCrypto = ‘ssl’;
    public $mailType = ‘html’
    
      
  3. Let’s test the configuration by creating a controller class.
  4. Use terminal

    php spark make:controller TestMail

    Now open the TestMail.php class. And let's write our index function

    
      public function index()
        {
            $from = ‘your email';
            
            $to = 'email of the reciever';
            $subject = 'Account Activation';
            $message = 'Hi Kevin,

    Please click the link below to activate your account.' .'

    Activate Account Now

    Thanks,
    Team CodeIgniter'; //instantiate email service $email = \Config\Services::email(); $email->setFrom($from); $email->setTo($to); $email->setSubject($subject); $email->setMessage($message); //We can attach images too. $filepath = 'public/assets/images/1.png'; $email->attach($filepath); if($email->send()) { echo 'Email sent successfully. Please Activate Your Account.'; } else { //predefined function printDebugger $data = $email->printDebugger(['headers']); print_r($data); } }
  5. Declare Routes
  6. Add the URI we just created.

    
      $routes->get('testmail', 'TestMail::index');
      
  7. Now you can try the email is working by visiting your_baseurl/testmail.

Popular posts from this blog

Evolution of computers and Computers today

Convert into binary

Processor Types and Specifications