{"id":6538,"date":"2021-06-22T04:07:32","date_gmt":"2021-06-22T04:07:32","guid":{"rendered":"https:\/\/programmingfields.com\/?p=6538"},"modified":"2026-05-02T06:29:33","modified_gmt":"2026-05-02T06:29:33","slug":"send-email-in-codeigniter-4-using-smtp","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/","title":{"rendered":"How to Send Email in CodeIgniter 4 Using Gmail SMTP"},"content":{"rendered":"\n<p>You can send an email using Gmail SMTP In CodeIgniter 4. For sending an email, you have to configure the email settings in your application. If you are sending email through the SMTP then there are two protocols that are <strong>TLS <\/strong>and <strong>SSL<\/strong>. Both protocols work on different ports. In this post, I will show you how you <strong>send email using Gmail<\/strong> in CodeIgniter 4. In CodeIgniter, there are different preferences available for determining how your&nbsp;<strong>email<\/strong>&nbsp;messages are sent. You can either set them manually or automatically. You can manage the preferences stored in the project config file. Here, I will start with a new project in CodeIgniter 4.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Create_a_Project_to_Send_Email_Using_Gmail\" >Create a Project to Send Email Using Gmail<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Configure_Project_Environment_in_CodeIgniter_4\" >Configure Project Environment in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Email_Configuration_in_CodeIgniter_4\" >Email Configuration in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Create_a_Controller_to_Send_Email_Using_Gmail\" >Create a Controller to Send Email Using Gmail<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Add_Functionality_to_Send_Email_Using_Gmail\" >Add Functionality to Send Email Using Gmail<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Create_Routes_in_CodeIgniter_4\" >Create Routes in CodeIgniter 4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Create_a_View_to_Send_Email_Using_Gmail\" >Create a View to Send Email Using Gmail<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Check_Result_of_Email\" >Check Result of Email<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/programmingfields.com\/send-email-in-codeigniter-4-using-smtp\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><strong>Prerequisites<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To make a working email configuration in CodeIgniter 4, you need to have a project. But, for creating a new project, you must have the below configuration.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>PHP &gt;= 7.3<\/strong><\/li><li><strong>MySQL (version &gt; 5)<\/strong><\/li><li><strong>Apache\/Nginx Server<\/strong><\/li><li><strong>VS Code Editor<\/strong><\/li><li><strong>Composer<\/strong><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Project_to_Send_Email_Using_Gmail\"><\/span><strong>Create a Project to Send Email Using Gmail<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating the project, I will be using the composer. Hence, hit the below command to start the installation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">composer create-project codeigniter4\/appstarter ci4-email<\/code><\/pre>\n\n\n\n<p>The above command will start the installation inside the specified directory.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-05-10-08-35-14-edited.png?w=643&amp;ssl=1\" alt=\"Install CodeIgniter 4 Using Composer\"\/><figcaption><strong>Install CodeIgniter 4 Using Composer<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Once the project has been created, you need to do the email configuration.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-implement-jquery-datatable-in-codeigniter-4\/\">How to Implement jQuery Datatable in CodeIgniter 4<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configure_Project_Environment_in_CodeIgniter_4\"><\/span><strong>Configure Project Environment in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Open the created project in VS Code editor and in the root of project, you will have the <strong>env<\/strong> file. So, firstly, you need to rename it to <strong>.env<\/strong>. Then search for the Environment configuration there.<\/p>\n\n\n\n<p>You have to change the environment to development from the production.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">#--------------------------------------------------------------------\n# ENVIRONMENT\n#--------------------------------------------------------------------\n\nCI_ENVIRONMENT = development<\/code><\/pre>\n\n\n\n<p>That&#8217;s it for the environment configuration. In the next step, you will have to configure the email settings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Email_Configuration_in_CodeIgniter_4\"><\/span><strong>Email Configuration in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Inside the project folder, look for the <strong>app\/Config\/Email.php<\/strong> file. Here, you have to set the email settings. You have to do the below configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">public $SMTPHost = 'smtp.googlemail.com';\npublic $SMTPUser = 'YOUR_EMAIL';\npublic $SMTPPass = 'EMAIL_ACCOUNT_PASSWORD';\npublic $SMTPPort = 465\/587;\npublic $SMTPTimeout = 60;\npublic $SMTPCrypto = 'ssl\/tls';\npublic $mailType = 'html';<\/code><\/pre>\n\n\n\n<p>For the Gmail password, you have to generate it inside the Google account settings. It will accept the app password. So, don&#8217;t use the real password of your Gmail account. <\/p>\n\n\n\n<p>The configuration will look like this-<\/p>\n\n\n\n<pre title=\"Config\/Email.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace Config;\n\nuse CodeIgniter\\Config\\BaseConfig;\n\nclass Email extends BaseConfig\n{\n    \/**\n     * @var string\n     *\/\n    public $fromEmail;\n\n    \/**\n     * @var string\n     *\/\n    public $fromName;\n\n    \/**\n     * @var string\n     *\/\n    public $recipients;\n\n    \/**\n     * The \"user agent\"\n     *\n     * @var string\n     *\/\n    public $userAgent = 'CodeIgniter';\n\n    \/**\n     * The mail sending protocol: mail, sendmail, smtp\n     *\n     * @var string\n     *\/\n    public $protocol = 'smtp';\n\n    \/**\n     * The server path to Sendmail.\n     *\n     * @var string\n     *\/\n    public $mailPath = '\/usr\/sbin\/sendmail';\n\n    \/**\n     * SMTP Server Address\n     *\n     * @var string\n     *\/\n    public $SMTPHost = 'smtp.googlemail.com';\n\n    \/**\n     * SMTP Username\n     *\n     * @var string\n     *\/\n    public $SMTPUser = 'YOUR_EMAIL';\n\n    \/**\n     * SMTP Password\n     *\n     * @var string\n     *\/\n    public $SMTPPass = 'EMAIL_ACCOUNT_PASSWORD';\n\n    \/**\n     * SMTP Port\n     *\n     * @var integer\n     *\/\n    public $SMTPPort = 465;\n\n    \/**\n     * SMTP Timeout (in seconds)\n     *\n     * @var integer\n     *\/\n    public $SMTPTimeout = 60;\n\n    \/**\n     * Enable persistent SMTP connections\n     *\n     * @var boolean\n     *\/\n    public $SMTPKeepAlive = false;\n\n    \/**\n     * SMTP Encryption. Either tls or ssl\n     *\n     * @var string\n     *\/\n    public $SMTPCrypto = 'ssl';\n\n    \/**\n     * Enable word-wrap\n     *\n     * @var boolean\n     *\/\n    public $wordWrap = true;\n\n    \/**\n     * Character count to wrap at\n     *\n     * @var integer\n     *\/\n    public $wrapChars = 76;\n\n    \/**\n     * Type of mail, either 'text' or 'html'\n     *\n     * @var string\n     *\/\n    public $mailType = 'html';\n\n    \/**\n     * Character set (utf-8, iso-8859-1, etc.)\n     *\n     * @var string\n     *\/\n    public $charset = 'UTF-8';\n\n    \/**\n     * Whether to validate the email address\n     *\n     * @var boolean\n     *\/\n    public $validate = false;\n\n    \/**\n     * Email Priority. 1 = highest. 5 = lowest. 3 = normal\n     *\n     * @var integer\n     *\/\n    public $priority = 3;\n\n    \/**\n     * Newline character. (Use \u201c\\r\\n\u201d to comply with RFC 822)\n     *\n     * @var string\n     *\/\n    public $CRLF = \"\\r\\n\";\n\n    \/**\n     * Newline character. (Use \u201c\\r\\n\u201d to comply with RFC 822)\n     *\n     * @var string\n     *\/\n    public $newline = \"\\r\\n\";\n\n    \/**\n     * Enable BCC Batch Mode.\n     *\n     * @var boolean\n     *\/\n    public $BCCBatchMode = false;\n\n    \/**\n     * Number of emails in each BCC batch\n     *\n     * @var integer\n     *\/\n    public $BCCBatchSize = 200;\n\n    \/**\n     * Enable notify message from server\n     *\n     * @var boolean\n     *\/\n    public $DSN = false;\n\n}<\/code><\/pre>\n\n\n\n<p>Now, that&#8217;s it for the configuration. Let&#8217;s create a controller to implement the functionality to send email using Gmail.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/generate-fake-data-in-codeigniter-4-using-seeder-and-faker\/\" target=\"_blank\" rel=\"noreferrer noopener\">Generate Fake Data in CodeIgniter 4 Using Seeder and Faker<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_Controller_to_Send_Email_Using_Gmail\"><\/span><strong>Create a Controller to Send Email Using Gmail<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For creating the controller, I will be using the spark command. Hence, hit the below command in the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">php spark make:controller EmailController<\/code><\/pre>\n\n\n\n<p>It will create a controller with the name <strong>EmailController.php<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"109\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-06-47-40.png?resize=1024%2C109&#038;ssl=1\" alt=\"Create Controller in CodeIgniter \" class=\"wp-image-6544\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-06-47-40.png?resize=1024%2C109&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-06-47-40.png?resize=300%2C32&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-06-47-40.png?resize=768%2C82&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-06-47-40.png?w=1187&amp;ssl=1 1187w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Create Controller in CodeIgniter <\/strong><\/figcaption><\/figure>\n\n\n\n<p>After creating the controller, you will have to implement the functionality to send email using gmail. <\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-remove-index-php-from-url-in-codeigniter-4\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Remove index.php From URL in CodeIgniter 4<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Add_Functionality_to_Send_Email_Using_Gmail\"><\/span><strong>Add Functionality to Send Email Using Gmail<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>CodeIgniter provides the email services to be load before sending the email. Hence, you need to load the below services inside the controller. You may create the constructor or even load it especially inside the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\\Config\\Services::email()<\/code><\/pre>\n\n\n\n<p>We will send an email through the form. So, we will pass the data dynamically. Firstly, add the below functions in the controller. <\/p>\n\n\n\n<pre title=\"EmailController.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\nnamespace App\\Controllers;\n\nuse App\\Controllers\\BaseController;\n\nclass EmailController extends BaseController\n{\n    \/**\n     * function to load email view\n     * @param NA\n     * @return NA\n     *\/\n    public function index()\n    {\n        return view('email');\n    }\n\n    \/**\n     * Function to send email\n     * @param NA\n     * @return $msg to view\n     *\/\n    public function sendMail() { \n        $inputs = $this-&gt;validate([\n            'email' =&gt; 'required|valid_email',\n            'subject' =&gt; 'required|min_length[5]',\n            'message' =&gt; 'required|min_length[10]'\n        ]);\n\n        if (!$inputs) {\n            return view('email', [\n                'validation' =&gt; $this-&gt;validator\n            ]);\n        }\n\n        $to = $this-&gt;request-&gt;getVar('email');\n        $subject = $this-&gt;request-&gt;getVar('subject');\n        $message = $this-&gt;request-&gt;getVar('message');\n        \n        $email = \\Config\\Services::email();\n \n        $email-&gt;setTo($to);\n        $email-&gt;setFrom('admin@programmingfields.com', 'Contact Email');\n        $email-&gt;setSubject($subject);\n        $email-&gt;setMessage($message);\n \n        if ($email-&gt;send()) {\n            $response = 'Email successfully sent';\n        } \n        else \n        {\n            $data = $email-&gt;printDebugger(['headers']);\n            $response ='Email send failed';\n        }\n        return redirect()-&gt;to( base_url('email') )-&gt;with('message', $response);\n    }\n}<\/code><\/pre>\n\n\n\n<p>In the above source code, we have two functions. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The first function is just for loading a view. The view will contain a basic form for sending the email.<\/li><li>In the second function, firstly, I have created a validation rule. Once the form will be validated then it will trigger the next iteration.<\/li><li>Next, I have read the data which are coming through the form. Then loaded the email services for applying email configuration in this function.<\/li><li>Now, set the form data to a separate variable. Lastly, called the <strong>send()<\/strong> function using the email service object.<\/li><li>If the email is sent, it will return the success message to the view with the session flash data.<\/li><\/ul>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/upload-multiple-image-with-validation-in-codeigniter-4\/\" target=\"_blank\" rel=\"noreferrer noopener\">Upload Multiple Image with Validation in Codeigniter 4<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Routes_in_CodeIgniter_4\"><\/span><strong>Create Routes in CodeIgniter 4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>After creating the functions, let&#8217;s add routes. You can find the<strong> Routes.php <\/strong>inside the<strong> app\/Config <\/strong>folder.<\/p>\n\n\n\n<pre title=\"Routes.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$routes-&gt;get('email', 'EmailController::index');\n$routes-&gt;post('email', 'EmailController::sendMail');<\/code><\/pre>\n\n\n\n<p>After creating the routes, you need to create the view from where the data will be coming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_a_View_to_Send_Email_Using_Gmail\"><\/span><strong>Create a View to Send Email Using Gmail<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Navigate to the <strong>app\/Views<\/strong> folder and create a view named <strong>email.php<\/strong>. After creating the view, let&#8217;s put the below code inside it.<\/p>\n\n\n\n<pre title=\"email.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;title&gt;Send Email in CodeIgniter 4&lt;\/title&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"&gt;\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH\/1fQ784\/j6cY\/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\"&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;div class=\"container-fluid py-4\"&gt;\n    \n    &lt;?php $validation =  \\Config\\Services::validation(); ?&gt;\n\n    &lt;h3 class=\"display-6 text-center font-weight-bold\"&gt;Send Email in CodeIgniter 4 Using Gmail SMTP &lt;\/h3&gt;\n        &lt;div class=\"row pt-4 border border-bottom-0\"&gt;\n            &lt;div class=\"col-xl-6 col-lg-6 col-md-8 col-sm-12 m-auto\"&gt;\n                &lt;form action=\"&lt;?= base_url('email') ?&gt;\" method=\"POST\"&gt;\n                \n                    &lt;?= csrf_field() ?&gt;\n\n                    &lt;?= (session()-&gt;getFlashdata('message')) ? \n                    '&lt;div class=\"alert alert-success alert-dismissible\"&gt;\n                            &lt;button type=\"button\" class=\"close\" data-dismiss=\"alert\"&gt;&amp;times;&lt;\/button&gt;'\n                           .session()-&gt;getFlashdata('message').\n                        '&lt;\/div&gt;': '' ?&gt;\n                        \n\n                    &lt;div class=\"card shadow\"&gt;\n                        &lt;div class=\"card-header\"&gt;\n                            &lt;h4 class=\"card-title font-weight-bold\"&gt;Send Email &lt;\/h4&gt;\n                        &lt;\/div&gt;\n\n                        &lt;div class=\"card-body\"&gt;\n                            &lt;div class=\"form-group\"&gt;\n                                &lt;label&gt;To &lt;span class=\"text-danger\"&gt;*&lt;\/span&gt;&lt;\/label&gt;\n                                &lt;input type=\"email\" name=\"email\" class=\"form-control &lt;?= $validation-&gt;getError('email') ? 'is-invalid': ''?&gt;\" placeholder=\"Email Recipient\"\/&gt;\n                                &lt;?= $validation-&gt;getError('name') ? '&lt;div class=\"invalid-feedback\"&gt;'.$validation-&gt;getError(\"email\") .'&lt;\/div&gt;': ''?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group\"&gt;\n                                &lt;label&gt;Subject &lt;span class=\"text-danger\"&gt;*&lt;\/span&gt;&lt;\/label&gt;\n                                &lt;input type=\"text\" name=\"subject\" class=\"form-control &lt;?= $validation-&gt;getError('subject') ? 'is-invalid': ''?&gt;\" placeholder=\"Subject\"\/&gt;\n                                &lt;?= $validation-&gt;getError('subject') ? '&lt;div class=\"invalid-feedback\"&gt;'.$validation-&gt;getError(\"subject\") .'&lt;\/div&gt;': ''?&gt;\n                            &lt;\/div&gt;\n\n                            &lt;div class=\"form-group\"&gt;\n                                &lt;label&gt;Message &lt;span class=\"text-danger\"&gt;*&lt;\/span&gt; &lt;\/label&gt;\n                                &lt;textarea name=\"message\" class=\"form-control &lt;?= $validation-&gt;getError('message') ? 'is-invalid': ''?&gt;\" placeholder=\"Your Message\"&gt;&lt;\/textarea&gt;\n                                &lt;?= $validation-&gt;getError('message') ? '&lt;div class=\"invalid-feedback\"&gt;'.$validation-&gt;getError(\"message\") .'&lt;\/div&gt;': ''?&gt;\n                            &lt;\/div&gt;\n                        &lt;\/div&gt;\n\n                        &lt;div class=\"card-footer\"&gt;\n                            &lt;button type=\"submit\" class=\"btn btn-success\"&gt;Send Email &lt;\/button&gt;\n                        &lt;\/div&gt;\n                    &lt;\/div&gt;\n                &lt;\/form&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>In the above snippet, I have created a form with three inputs are as follow.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Email Recipient<\/li><li>Subject<\/li><li>Message<\/li><\/ul>\n\n\n\n<p>In the form, I have displayed the validation error message using the<strong> validation() <\/strong>service. Also, dsiplayed a flash data message that will come after the email is sent.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-upload-image-and-file-in-codeigniter-4-with-validation\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Upload Image and File in Codeigniter 4 with Validation<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Check_Result_of_Email\"><\/span><strong>Check Result of Email <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, check the result in the browser by navigating the the route. The route is- <code>http:\/\/localhost:8080\/email<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"625\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-47.png?resize=1024%2C625&#038;ssl=1\" alt=\"send email using gmail\" class=\"wp-image-6540\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-47.png?resize=1024%2C625&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-47.png?resize=300%2C183&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-47.png?resize=768%2C469&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-47.png?w=1115&amp;ssl=1 1115w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Send Email Using Gmail in CodeIgniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, try sending email without filling up the required details. In the response, you will get the validation error message as showing below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"643\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-58.png?resize=1024%2C643&#038;ssl=1\" alt=\"validation error message in codeigniter 4\" class=\"wp-image-6541\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-58.png?resize=1024%2C643&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-58.png?resize=300%2C188&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-58.png?resize=768%2C482&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-37-58.png?w=1116&amp;ssl=1 1116w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Email Validation Error Message in CodeIgniter 4<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Lastly, try filling up details correctly and hit the button to trigger email.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"667\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-13.png?resize=1024%2C667&#038;ssl=1\" alt=\"send email using gmail\" class=\"wp-image-6542\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-13.png?resize=1024%2C667&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-13.png?resize=300%2C195&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-13.png?resize=768%2C500&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-13.png?w=1080&amp;ssl=1 1080w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Email Sent in CodeIgniter 4 Using Gmail<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>Here, email has been received in other mailbox. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"340\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-40.png?resize=1024%2C340&#038;ssl=1\" alt=\"Email Recieved in CodeIgniter 4 Sent By Gmail\" class=\"wp-image-6549\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-40.png?resize=1024%2C340&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-40.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-40.png?resize=768%2C255&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-22-07-52-40.png?w=1054&amp;ssl=1 1054w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption><strong>Email Received in CodeIgniter 4 Sent By Gmail<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We have configured the Gmail SMTP in CodeIgniter 4 for sending emails. After the successful configuration, we are able to send email using Gmail SMTP. We have seen the SMTP protocol as TLS and SSL. If you use SSL then the email will be transmitted through a secured channel. It is a good and secure way to send email using SSL. The ports for both protocols are different. The delivery of email totally depends on the mail host. So, I hope this post will helpful for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can send an email using Gmail SMTP In CodeIgniter 4. For sending an email, you have to configure the email settings in your application. If you are sending email through the SMTP then there are two protocols that are TLS and SSL. Both protocols work on different ports. In this post, I will show [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":6553,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1977],"tags":[2126,2130,2127,2128],"yst_prominent_words":[812],"class_list":{"0":"post-6538","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-codeigniter-4","8":"tag-codeigniter-send-email","9":"tag-gmail-host","10":"tag-gmail-smtp-in-codeigniter-4","11":"tag-smtp-protocol","12":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/send-email-using-gmail.png?fit=2240%2C1260&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/6538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=6538"}],"version-history":[{"count":1,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/6538\/revisions"}],"predecessor-version":[{"id":8638,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/6538\/revisions\/8638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/6553"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=6538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=6538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=6538"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=6538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}