How to send email in Codeigniter without smtp?

Member

by denis , in category: PHP Frameworks , a year ago

How to send email in Codeigniter without smtp?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by mac , 9 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.

Member

by domenico , 3 months ago

@denis 

To send an email in CodeIgniter without using SMTP, you can make use of the CodeIgniter Email Library and configure it to use the PHP mail() function. Here are the steps:

  1. Load the email library in your controller or autoload it in your config file:
1
$this->load->library('email');


  1. Configure the email settings by setting the necessary parameters:
1
2
3
4
5
6
7
8
$config = array(
    'protocol' => 'mail',
    'mailpath' => '/usr/sbin/sendmail',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);

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


Note: The 'mailpath' parameter value may vary depending on your server configuration. You can consult your hosting provider or server administrator to get the correct value.

  1. Set the email details such as the sender, recipient, subject, and message body:
1
2
3
4
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->subject('Email Subject');
$this->email->message('Email Message');


  1. Finally, send the email:
1
$this->email->send();


That's it! The email should be sent using the PHP mail() function without the need for SMTP. Remember to handle any errors that may occur during the email sending process.