How to send email in Codeigniter using smtp?

by mallory_cormier , in category: PHP Frameworks , 9 months ago

How to send email in Codeigniter using smtp?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , 3 months ago

@mallory_cormier 

To send email in CodeIgniter using SMTP, you will need to configure the SMTP settings in the application/config/email.php file. Here is an example of how to do this:

  1. Open the application/config/email.php file in a text editor.
  2. Find the $config array and update the following values:Set protocol to smtp.Set smtp_host to the hostname of your SMTP server.Set smtp_port to the port number of your SMTP server.Set smtp_user to the username for your SMTP server.Set smtp_pass to the password for your SMTP server.
  3. If your SMTP server requires encryption, set smtp_crypto to the encryption type (e.g., ssl or tls).
  4. Save the email.php file.


To send an email using these settings, you can use the following code in your controller or model:

1
2
3
4
5
6
7
8
$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();


This will send an email from your@example.com to someone@example.com with the subject "Email Test" and the message "Testing the email class."


You can also use the email library's various methods to set additional email options, such as CC and BCC recipients, attachments, etc. Refer to the CodeIgniter user guide for more information on the email library.