How to send email in Codeigniter without smtp?

Member

by denis , in category: PHP Frameworks , 8 months ago

How to send email in Codeigniter without smtp?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 3 months ago

@denis 

In CodeIgniter, you can use the built-in Email library to send emails without using an SMTP server. You can use the mail() function provided by PHP to send emails.


Here is an example of how to send an email using the Email library and the mail() function in CodeIgniter:

  1. Load the Email library in your controller using the following line of code:
1
$this->load->library('email');


  1. Set the email configuration options. You can set the following options:
  • to: The email address of the recipient.
  • from: The email address of the sender.
  • subject: The subject of the email.
  • message: The message body of the email.


You can set these options using the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$config = array(
   'protocol' => 'mail',
   'mailtype' => 'html',
   'charset'  => 'utf-8'
);

$this->email->initialize($config);

$this->email->to('recipient@example.com');
$this->email->from('sender@example.com', 'Sender Name');
$this->email->subject('Email Subject');
$this->email->message('Email Message');


  1. Send the email using the send() method of the Email library.
1
2
3
4
5
if ($this->email->send()) {
   // Email was sent successfully
} else {
   // An error occurred while trying to send the email
}


Note that if your server does not have the mail() function configured, you will not be able to send emails using this method. In that case, you will need to use an SMTP server to send emails.