Send Email in CodeIgniter 4 With SMTP

In this tutorial, we will show you how to send emails from your localhost to a server in Codeigniter 4. This process is easy because of the new functionality available in the stable version of Codeigniter.

To use Codeigniter 4’s email service, you just need to call the services in Codeigniter using the following code. First, you need to create a mail ID and password using Cpanel and set the email and password in the email configuration.

\Config\Services::serviceName();
// i.e  \Config\Services::email();

In this part, you will learn how to use the email library to send emails from your website/localhost to the user/client in Codeigniter 4. But before using the email library, you need to see the library settings in Codeigniter 4.

Step 1: Go to the app/config/Email.php and open the file

Step 2: Update the Email Credentials

<?php
namespace Config;
 
use CodeIgniter\Config\BaseConfig;
 
class Email extends BaseConfig
{
 
  /**
   * @var string
   */
  public $fromEmail;
 
  /**
   * @var string
   */
  public $fromName;
 
  /**
   * @var string
   */
  public $recipients;
 
  /**
   * The "user agent"
   *
   * @var string
   */
  public $userAgent = 'CodeIgniter';
 
  /**
   * The mail sending protocol: mail, sendmail, smtp
   *
   * @var string
   */
  public $protocol = 'smtp';
 
  /**
   * The server path to Sendmail.
   *
   * @var string
   */
  public $mailPath = '/usr/sbin/sendmail';
 
  /**
   * SMTP Server Address
   *
   * @var string
   */
  public $SMTPHost='mail.tutsmake.com';
 
  /**
   * SMTP Username
   *
   * @var string
   */
  public $SMTPUser = 'abcd@tutsmake.com';
 
  /**
   * SMTP Password
   *
   * @var string
   */
  public $SMTPPass = 'YourPasswordHere$';
 
  /**
   * SMTP Port
   *
   * @var integer
   */
  public $SMTPPort = 587;
 
  /**
   * SMTP Timeout (in seconds)
   *
   * @var integer
   */
  public $SMTPTimeout = 15;
 
  /**
   * Enable persistent SMTP connections
   *
   * @var boolean
   */
  public $SMTPKeepAlive = false;
 
  /**
   * SMTP Encryption. Either tls or ssl
   *
   * @var string
   */
  public $SMTPCrypto = 'tls';
 
  /**
   * Enable word-wrap
   *
   * @var boolean
   */
  public $wordWrap = true;
 
  /**
   * Character count to wrap at
   *
   * @var integer
   */
  public $wrapChars = 76;
 
  /**
   * Type of mail, either 'text' or 'html'
   *
   * @var string
   */
  public $mailType = 'html';
 
  /**
   * Character set (utf-8, iso-8859-1, etc.)
   *
   * @var string
   */
  public $charset = 'UTF-8';
 
  /**
   * Whether to validate the email address
   *
   * @var boolean
   */
  public $validate = false;
 
  /**
   * Email Priority. 1 = highest. 5 = lowest. 3 = normal
   *
   * @var integer
   */
  public $priority = 3;
 
  /**
   * Newline character. (Use “\r\n” to comply with RFC 822)
   *
   * @var string
   */
  public $CRLF = "\r\n";
 
  /**
   * Newline character. (Use “\r\n” to comply with RFC 822)
   *
   * @var string
   */
  public $newline = "\r\n";
 
  /**
   * Enable BCC Batch Mode.
   *
   * @var boolean
   */
  public $BCCBatchMode = false;
 
  /**
   * Number of emails in each BCC batch
   *
   * @var integer
   */
  public $BCCBatchSize = 200;
 
  /**
   * Enable notify message from server
   *
   * @var boolean
   */
  public $DSN = false;
 
}

Step 3: Now go to your controller/method and just use/call the library/class

public function yourMethod(){
   $message = "Please activate the account ".anchor('user/activate/'.$data['u_link'],'Activate Now','');
   $email = \Config\Services::email();
   $email->setFrom('ci-4-project@toruqeprogramming.co.in', 'your Title Here');
   $email->setTo($data['u_email']);
   $email->setSubject('Your Subject here | toruqeprogramming.co.in');
   $email->setMessage($message);//your message here
 
   $email->setCC('another@emailHere');//CC
   $email->setBCC('thirdEmail@emialHere');// and BCC
   $filename = '/img/yourPhoto.jpg'; //you can use the App patch 
   $email->attach($filename);
    
   $email->send();
   $email->printDebugger(['headers']);
}

Note that:- If you are sending a mail using Gmail you have to allow non-secure apps to access Gmail you can do this by going to your Gmail settings here.

Once less secure apps are enabled; now you can use your Gmail for sending the emails.

Recommended Codeigniter Posts

  1. Get Latitude and Longitude From Address in Codeigniter
  2. Codeigniter 4 Resize, Compress Image with Image Manipulation
  3. How to Install/Download CodeIgniter Using Composer

Leave a Reply

Your email address will not be published. Required fields are marked *