CodeIgniter’s robust Email Class supports the following features:
> Multiple Protocols: Mail, Sendmail, and SMTP
> TLS and SSL Encryption for SMTP
> Multiple recipients
> CC and BCCs
> HTML or Plaintext email
> Attachments
> Word wrapping
> Priorities
> BCC Batch Mode and enabling large email lists to be broken into small BCC batches.
> Email Debugging tools
How to send email in CodeIgniter
Create a view file send_email.php and save the below code it in application/views/send_email.php.
<!DOCTYPE html> <html> <head> <title>Send Mail</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div class="container mt-4"> <div class="row"> <div class="col-lg-6"> <form action="<?= base_url('demo/send_mail') ?>" method="post"> <label>Email</label> <input type="email" name="to" class="form-control" placeholder="Enter Receiver email"> <br/> <label>Subject</label> <input type="text" name="subject" placeholder="Enter Subject" class="form-control"> <br> <label>Message</label> <textarea rows="6" name="message" placeholder="Enter your message here" class="form-control"></textarea> <br/> <input type="submit" value="Send Email" class="form-control"> </form> </div> </div> </div> </body> </html>
Create a controller file Demo.php and save it in application/controller/Demo.php.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Demo extends CI_Controller { function index(){ $this->load->view("send_mail"); } function send_mail(){ $this->load->library('email'); $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.googlemail.com'; $config['smtp_port'] = '465'; $config['smtp_timeout'] = '7'; $config['smtp_user'] = 'Enter your email'; $config['smtp_pass'] = 'Enter your email password'; $config['charset'] = 'utf-8'; $config['wordwrap'] = TRUE; $config['newline'] = "\r\n"; $config['mailtype'] = 'html'; // or html $config['validation'] = TRUE; // bool whether to validate email or not $this->email->initialize($config); $to = $this->input->post("to"); $subject = $this->input->post("subject"); $message = $this->input->post("message"); $this->email->from('Enter your email', 'Hello'); $this->email->to($to); $this->email->subject($subject); $this->email->message($message); $this->email->send(); redirect("demo"); } }
After Login to your google account and click on the link : Less secure app access
After you Allow less secure apps:ON
Let us execute this example by the following URL in the browser. This URL may be different based on your website.
https://howtowebcode.com/demo