CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

How to Send Email in CodeIgniter 4 With SMTP


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

app/Config/Email.php

<?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='smtp.gmail.com';

/**
* SMTP Username
*
* @var string
*/
public $SMTPUser='divyasundarsahu@gmail.com';

/**
* SMTP Password
*
* @var string
*/
public $SMTPPass='your password here';

/**
* SMTP Port
*
* @var integer
*/
public $SMTPPort = 587;

/**
* 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 = '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 = '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 2: Create a controller Contact.php

Note: In the controller call the method

$email = \Config\Services::email();

app/Controllers/Contact.php

<!DOCTYPE html>
<html>
<head>
<title>Codeigniter 4 Image upload example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
 <div class="container">
    <br>
    <div class="row">
<div class="col-md-4">
   <?php if (session('msg')) : ?>
        <div class="alert alert-success alert-dismissible">
            <?= session('msg') ?>
            <button type="button" class="close" data-dismiss="alert"><span>×</span></button>
        </div>
        <?php endif ?>
        <form action="<?php echo base_url('contact/upload');?>" name="ajax_form" id="ajax_form" method="post" accept-charset="utf-8" enctype="multipart/form-data">
 
          <div class="form-group">
            <label for="formGroupExampleInput">Name</label>
            <input type="file" name="file" class="form-control" id="file" style="height:35px;">
          </div>
 
          <div class="form-group">
           <button type="submit" id="send_form" class="btn btn-success">Submit</button>
          </div>
         
</form>
</div>
 
    </div>
 
</div>
</body>