codeigniter send using microsoft mail smtp code example

Example: codeigniter 3 smtp email send

CodeIgniter Email Configuration
We need to have a central place where we can manage the email settings. CodeIgniter does not come with a config file for emails so we will have to create one ourselves.

Create a file email.php in the directory application/config

Add the following code to email.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

$config = array(
    'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
    'smtp_host' => 'smtp.example.com', 
    'smtp_port' => 465,
    'smtp_user' => '[email protected]',
    'smtp_pass' => '12345!',
    'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example
    'mailtype' => 'text', //plaintext 'text' mails or 'html'
    'smtp_timeout' => '4', //in seconds
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);
?>
HERE,
'protocol' => 'smtp', specifies the protocol that you want to use when sending email. This could be Gmail smtp settings or smtp settings from your host
'smtp_host' => 'smtp.example.com',specifies the smtp host. For example, if you want to use Gmail then you would have something like smtp.gmail.com
'smtp_port' => 465, an open port on the specified smtp host that has been configured for smtp mail
'smtp_user' => '[email protected]', the email address that will be used as the sender when sending emails. This should be a valid email address that exists on the server
'smtp_pass' => '12345!', the password to the specified smtp user email
'smtp_crypto' => 'ssl', specifies the encryption method to be used i.e. ssl, tls etc.
'email type' => 'text', sets the mail type to be used. This can be either plain text or HTML depending on your needs.
'smtp_timeout' => '4', specifies the time in seconds that should elapse when trying to connect to the host before a timeout exception is thrown.
'charset' => 'iso-8859-1', defines the character set to be used when sending emails.
'wordwrap' => TRUE is set to TRUE then word-wrap is enabled. If it is set to FALSE, then word-wrap is not enabled

<?php 
  $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("\r\n");
  $this->email->from($from);
  $this->email->to($to);
  $this->email->subject($subject);
  $this->email->message($message);

  if ($this->email->send()) {
    echo 'Your Email has successfully been sent.';
  } else {
    show_error($this->email->print_debugger());
  }  
?>

Tags:

Php Example