How to check smtp mail work or not in codeigniter
To check if SMTP mail works or not in CodeIgniter, you can use the email
library provided by CodeIgniter. Here's an example of how you can do it:
-
First, make sure you have the
email
library loaded in your controller or model. You can do this by adding the following line of code:$this->load->library('email');
-
Next, set the SMTP settings in the
email
library. You can do this by adding the following code:$config = array( 'protocol' => 'smtp', 'smtp_host' => 'your_smtp_host', 'smtp_user' => 'your_smtp_username', 'smtp_pass' => 'your_smtp_password', 'smtp_port' => 587, 'mailtype' => 'text', 'charset' => 'utf-8', 'wordwrap' => TRUE ); $this->email->initialize($config);
Replace
your_smtp_host
,your_smtp_username
,your_smtp_password
, and587
with your actual SMTP settings. -
Now, you can use the
send_mail()
method to send a test email. This method will returnTRUE
if the email is sent successfully, andFALSE
otherwise.$to = '[email protected]'; $subject = 'Test Email'; $message = 'This is a test email sent using CodeIgniter';
if ($this->email->send_mail($to, $subject, $message)) { echo 'Email sent successfully!'; } else { echo 'Error sending email!'; }
Replace `[email protected]` with the email address you want to send the test email to.
4. If the email is sent successfully, you can check the email logs to verify that the email was sent. You can do this by adding the following code:
```php
echo $this->email->print_debugger();
This will print the email logs to the screen, including the sender, recipient, subject, and message.
Here's the complete code:
<?php
class Test_email extends CI_Controller {
public function index()
{
$this->load->library('email');
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'your_smtp_host',
'smtp_user' => 'your_smtp_username',
'smtp_pass' => 'your_smtp_password',
'smtp_port' => 587,
'mailtype' => 'text',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->email->initialize($config);
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using CodeIgniter';
if ($this->email->send_mail($to, $subject, $message)) {
echo 'Email sent successfully!';
} else {
echo 'Error sending email!';
}
echo $this->email->print_debugger();
}
}
?>
Note that you should replace your_smtp_host
, your_smtp_username
, your_smtp_password
, and 587
with your actual SMTP settings.