how to send email from localhost using codeigniter?

19k Views Asked by At

    function sendMail() {

  $config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.googlemail.com',
  'smtp_port' => 465,
  'smtp_user' => '[email protected]', 
  'smtp_pass' => 'xxx', 
  'mailtype' => 'html',
  'charset' => 'iso-8859-1',
  'wordwrap' => TRUE
);

        $message = 'test';
        $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from('[email protected]');
      $this->email->to('[email protected]');
      $this->email->subject('testing');
      $this->email->message($message);
      if($this->email->send())
     {
      echo 'Email sent.';    
     }
     else
    {
     show_error($this->email->print_debugger());  
    }

}

This is my code, i am trying to send email from localhost using codeigniter , i got message "Email sent." but i didnt got any mail in gmail account.

4

There are 4 best solutions below

0
On

I see you are missing $this->email->initialize($config); Also make sure you have set up your email settings on you localhost else will not work.

Also $config = Array( change to $config = array(

Replace Google Mail "gmail"

'smtp_host' => 'ssl://smtp.googlemail.com'

With

'smtp_host' => 'ssl://smtp.gmail.com'

Code

function send_mail() {

$config = array(
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.gmail.com',
  'smtp_port' => 465,
  'smtp_user' => '[email protected]', 
  'smtp_pass' => 'xxx', 
  'mailtype' => 'html',
  'charset' => 'iso-8859-1',
  'wordwrap' => TRUE
);


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

$this->email->initialize($config); // Add 

$this->email->from('[email protected]');
$this->email->to('[email protected]');
$this->email->subject('testing');

$message = 'test';
$this->email->message($message);

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

}
1
On
 $config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',// your mail name
'smtp_pass' => '*****',
'mailtype'  => 'html', 
'charset'   => 'iso-8859-1',
 'wordwrap' => TRUE
);

then

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

Mail Settings in XAMPP(Impotent)

$this->email->from('[email protected]', 'myname');//your mail address and name
$this->email->to('[email protected]'); //receiver mail

$this->email->subject('testing');
$this->email->message($message);

$this->email->send(); //sending mail

Configuration in sendmail.ini

path c:\xampp\sendmail\sendmail.ini

Configurations

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=yourgmailpassword
[email protected]

in php.ini

pathc:\xampp\xampp\php\php.ini

[mail function]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
3
On

Email Cannot be sent locally you should. This method can be test on test server..

1
On
    $CI =& get_instance();
    $CI->load->library('email');
    $CI->email->initialize(get_email_config());

    $CI->email->from($data['email_from'],WEBSITE_TITLE); // Constant is define which exist email
    $CI->email->to($data['email_to']);
    if(isset($data['bcc']) && !empty($data['bcc'])){
        $CI->email->bcc($data['bcc']);
    }else{
        $CI->email->bcc('other_email');
    }
    if(isset($data['upload'])&& !empty($data['upload'])){
        $CI->email->attach($data['upload']);
    }
    $CI->email->subject($data['subject']);
    $CI->email->message($data['body']);
    $CI->email->send(TRUE);