end Email in CodeIgniter With SMTP

How To Send Email in CodeIgniter With SMTP Server

Most 5 Ways Send Email in CodeIgniter With SMTP Server

  1. Way

Codeigniter supported an inbuilt Email library class which simplify the email sending process. To send an email we need to do some configuration, need to load the email library. Codeigniter supports Multiple Protocols, Mail, Sendmail, and SMTP, Multiple recipients, CC and BCCs, HTML or Plain text email, Attachments, Word wrapping and many more

Setting Email Preferences

Let’s start with the Codeigniter email program configuration. The first load the built-in email library in the controller constructor.

$this->load->library(’email’);

 

Configure the smtp details like domain name, port, username and password

// Email configuration

 $config = Array(

‘protocol’ => ‘smtp’,

‘smtp_host’ => ‘smtp.yourdomainname.com.’,

‘smtp_port’ => 465,

‘smtp_user’ => ‘admin@yourdomainname.com’, // change it to yours

‘smtp_pass’ => ‘******’, // change it to yours

‘mailtype’ => ‘html’,

‘charset’ => ‘iso-8859-1’,

‘wordwrap’ => TRUE

);

 

Refer to the complete Codeignitor email sending program. In this program, you just need to configure your domain SMTP details

Code php

class SendEmail extends Controller

function SendEmail(){

 parent::Controller();

 $this->load->library(’email’); // load the library

}

function index(){

 $this->sendEmail();

}

public function sendEmail(){

// Email configuration

 $config = Array(

‘protocol’ => ‘smtp’,

‘smtp_host’ => ‘smtp.yourdomainname.com.’,

‘smtp_port’ => 465,

‘smtp_user’ => ‘admin@yourdomainname.com’, // change it to yours

‘smtp_pass’ => ‘******’, // change it to yours

‘mailtype’ => ‘html’,

‘charset’ => ‘iso-8859-1’,

‘wordwrap’ => TRUE

);

 

 $this->load->library(’email’, $config);

 $this->email->from(‘admin@yourdomainname.com’, “Admin Team”);

 $this->email->to(“test@domainname.com”);

 $this->email->cc(“testcc@domainname.com”);

 $this->email->subject(“This is test subject line”);

 $this->email->message(“Mail sent test message…”);

 $data[‘message’] = “Sorry Unable to send email…”;

if ($this->email->send()) {

 $data[‘message’] = “Mail sent…”;

}

// forward to index page

 $this->load->view(‘index’, $data);

} }

 

  1. Send an email by using Codeigniter library via localhost

public function sendemail(){

  $config = Array(

  ‘protocol’ => ‘smtp’,

  ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,

  ‘smtp_port’ => 465,

  ‘smtp_user’ => ‘xyzl@gmail.com’,

  ‘smtp_pass’ => ‘*************’, );

$this->load->library(’email’, $config);

$this->email->set_newline(“rn”);

$this->email->from(‘xyz@gmail.com’, ‘Name’);

$this->email->to(‘your@yahoo.com’);

$this->email->subject(‘ My mail through codeigniter from localhost Vkprogramming ‘);

$this->email->message(‘Hello Vkprogramming…’);

if (!$this->email->send()) {

show_error($this->email->print_debugger()); }

else {

echo ‘Your e-mail has been sent!’ By Vkprogramming;

}

}

 

I Found an error when I use Codeigniter to send an email:

Message: mail() : Failed to connect to mailserver at

“localhost” port 25, verify your “SMTP”, “smtp_port” setting in

php.ini or use ini_set().

 

Please Try This code.

function sendMail(){

    $config = Array(

  ‘protocol’ => ‘smtp’,

  ‘smtp_host’ => ‘ssl://smtp.googlemail.com’,

  ‘smtp_port’ => 465,

  ‘smtp_user’ => ‘xxx@gmail.com’, // change it to yours

  ‘smtp_pass’ => ‘xxx’, // change it to yours

  ‘mailtype’ => ‘html’,

  ‘charset’ => ‘iso-8859-1’,

  ‘wordwrap’ => TRUE

);

 

$message = ”;

$this->load->library(’email’, $config);

$this->email->set_newline(“rn”);

$this->email->from(‘xxx@gmail.com’); // change it to yours

$this->email->to(‘xxx@gmail.com’);// change it to yours

$this->email->subject(‘Resume from JobsBuddy for your Job posting’);

$this->email->message($message);

if($this->email->send()){

echo ‘Email sent.’;

}else{

show_error($this->email->print_debugger());

}}

 

How to Send Email using CodeIgniter

Sending email in CodeIgniter is easier and you set preferences with your needs. CodeIgniter supplies an Email library for sending emails in the program. Codeigniter supported the inbuilt Mail library sending process.

 

Codeigniter supports multiple, Mail, Protocols, and SMTP, send mail, multiple recipients, CC and BCCs, HTML or Plain text email, Attachments, Word wrapping and many more.

As you know email is quite important in web applications. When a user signs up, we may want to send them an email to verify their email address and allow the user to confirm a subscription. We also use email to reset forgotten passwords, send invoices and receipts to clients, etc. CodeIgniter makes it super simple for us to send emails from our application utilizing a variety of options.

CodeIgniter includes an integrated email library called the Codeigniter email library that we’re able to work together when sending mails. In this tutorial, we will demonstrate the most used email attributes like text mail, HTML email, an email with an attachment.

CodeIgnitr SMTP Email Configuration

Create an SMTP configuration file email.php inside the application/config

$config = array(

    ‘protocol’ => ‘smtp’,

    ‘smtp_host’ => ‘smtp.example.com’, 

    ‘smtp_port’ => 465,

    ‘smtp_user’ => ‘phptpoint@example.com’,

    ‘smtp_pass’ => ‘123456’,

    ‘smtp_crypto’ => ‘ssl’,

    ‘mailtype’ => ‘text’,

    ‘smtp_timeout’ => ‘4’, 

    ‘charset’ => ‘iso-8859-1’,

    ‘wordwrap’ => TRUE

);

 

Note: for sending emails to work, you should provide valid configuration parameters. Dummy parameters will not be able to send emails.

 

Create a View

Create a view file inside views/email.php

Add given below code inside the file.

<title>CodeIgniter  Email Send using SMTP</title>

<div>

<h3>Use the form below to send email</h3>

<form method=”post” >

<input type=”email” name=”to” placeholder=”Enter Receiver Email”>

<br><br>

<input type=”text” name=”subject” placeholder=”Enter Subject”>

<br><br>

<textarea rows=”6″ name=”message” placeholder=”Enter your message here”></textarea>

<br><br>

<input type=”submit” value=”Send Email” />

</form>

</div>

 

CodeIgniter Controller

Create a Controller file inside controller/EmailController .php

Add given below code inside EmailController.php file.

 

class EmailController extends CI_Controller {

    public function __construct() {

        parent:: __construct();

        $this->load->helper(‘url’);    }

    public function index(){

        $this->load->view(’email’);

   }

 function send(){

        $this->load->config(’email’);

        $this->load->library(’email’);

       $from = $this->config->item(‘smtp_user’);

        $to = $this->input->post(‘to’);

        $subject = $this->input->post(‘subject’);

        $message = $this->input->post(‘message’);

        $this->email->set_newline(“rn”);

        $this->email->from($from);

        $this->email->to($to);

        $this->email->subject($subject);

        $this->email->message($message);

  if ($this->email->send()) {

            echo ‘Email has been sent successfully’;

        } else {

            show_error($this->email->print_debugger());

        }   } 

 

Related:

 

People also ask

How to send email in CodeIgniter using PHPMailer?

How do I send an email in CI 4?

How do I load an email library?

What is Mailpath.

Leave a Comment

Your email address will not be published. Required fields are marked *