CodeIgniter, Send Email, BIGROCK

Send an Email Using CodeIgniter and Challenges

7 min read

  • July 16, 2024
  • Rajesh Goel

Sending emails is straightforward and flexible; you can either configure it on the fly or set your preferences in the app/Config/Email.php file

Configure Email Preferences

Before sending an email, you need to set up your email preferences. CodeIgniter allows you to set these preferences either in the app/config/email.php file or directly in your controller. Here’s how you can do it in your controller:

$config['protocol'] = 'sendmail';

$config['smtp_host'] = your_smtp_host'; // e.g., smtp.gmail.com or smtp.office365.com

$config['smtp_port'] = 587;

$config['smtp_user'] = '*****';

$config['smtp_pass'] = '*****';

$config['mailtype'] = 'html';

$config['validate'] = true;

$config['charset'] = 'utf-8';

$config['smtp_crypto'] = 'ssl';

$config['newline'] = "\r\n";

$config['crlf'] = "\r\n";

$config['smtp_timeout'] = "10";

Set Up Email Parameters

After configuring the email settings, you need to set the email parameters such as the sender's email, recipient's email, subject, and message.

$this->email->from('your_email@example.com', 'Your Name');

$this->email->to('recipient_email@example.com');

$this->email->cc('another@another-example.com');

$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');

$this->email->message('Testing the email class.');

Send the Email

Once all the parameters are set, you can send the email using the send method. It’s a good practice to check if the email was sent successfully and handle any errors accordingly.

if ($this->email->send()) {

echo 'Email sent successfully!';

} else {

show_error($this->email->print_debugger()); }