How to send an email via Gmail SMTP server using CodeIgniter-4?

Monayem Islam
3 min readJul 11, 2021

--

Do you know we can send email from our CodeIgniter-4 web application to others using our personal Gmail? If your answer is “No” then let’s have a look at this article.

Step-1: Change App Access Rule

First Login to your Gmail account and click on Your Image Icon > Manage Your Google Account > Security as represented below.

Now, change the Allow less secure apps status and make it ON.

Step 2: Configure Email.php.

Open app/config/Email.php file from your CodeIgniter-4 application folder and provide your Gmail credentials and other parameters value.

<?phpnamespace 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= ‘smtp.googlemail.com’;/*** SMTP Username** @var string*/public $SMTPUser=’example-email-name@gmail.com’;//writhe here your email/*** SMTP Password** @var string*/public $SMTPPass=’example-password’;//write here your password/*** SMTP Port** @var integer*/public $SMTPPort = 465;/*** SMTP Timeout (in seconds)** @var integer*/public $SMTPTimeout = 5;/*** Enable persistent SMTP connections** @var boolean*/public $SMTPKeepAlive = false;/*** SMTP Encryption. Either tls or ssl** @var string*/public $SMTPCrypto = ‘ssl’;/*** 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 = ‘text’;/*** 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: Make a Controller

Create a controller SendEmail.php to your app/Controllers folder . After creating, write a push method inside the controller which is given below.

<?phpnamespace App\Controllers;use CodeIgniter\Controller;class SendEmail extends Controller{public function push(){$to = ‘destination-name@gmail.com’;//Type here the mail address where you want to send$subject = ‘Subject of Email’;//Write here Subject of Email$message=”Conngrats ! You did it.”;//Write the message you want to send$email = \Config\Services::email();$email->setTo($to);$email->setFrom(‘example-name@gmail.com’, ‘Mail Testing’);//set From$email->setSubject($subject);$email->setMessage($message);if($email->send()){echo “Email has been Sent.”;}else{echo “Something went wrong !”;}}}

Step-4: Call the push() function

Now paste the URL (ex: http://localhost/Email-Sending-Codeigniter4-Gmail-SMTP/public/sendemail/push) to browser and hit enter ! Congrats ! You did it.

--

--

Monayem Islam
Monayem Islam

Written by Monayem Islam

Passionate about programming and working towards B.Sc. in Information and Communication Technology at Islamic University, Bangladesh.

Responses (1)