How to send a mail using gmail smtp in codeigniter?

by filiberto , in category: Third Party Scripts , 8 days ago

How to send a mail using gmail smtp in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 7 days ago

@filiberto 

To send an email using Gmail SMTP in CodeIgniter, you can use the following code:


First, make sure you have configured CodeIgniter to use an email library such as SMTP. You can do this by updating the 'config' file located at application/config/config.php with your Gmail SMTP details:


$config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.gmail.com'; $config['smtp_port'] = '465'; $config['smtp_user'] = '[email protected]'; $config['smtp_pass'] = 'your_password';


Next, create a controller method where you will send the email:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public function send_email(){
    $this->load->library('email');

    $this->email->from('[email protected]', 'Your Name');
    $this->email->to('[email protected]'); 
    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');

    if($this->email->send()) {
        echo 'Email sent';
    } else {
        show_error($this->email->print_debugger());
    }
}


Don't forget to replace '[email protected]' and 'your_password' with your actual Gmail email address and password. Also, update '[email protected]' with the email address of the recipient.


Now, you can call the 'send_email' method from your browser or any other HTTP client to send an email using Gmail SMTP in CodeIgniter.