1
votes

I am trying to send email through Codeigniter email library form localhost. Below is my code.

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => '[email protected]',
    'smtp_pass' => 'xxxxxx',
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('[email protected]');
    $this->email->to('[email protected]');
    $this->email->subject('Subject');
    $this->email->message('Sample message');
    if (!$this->email->send()) 
    {
        show_error($this->email->print_debugger()); 
    }
    else 
    {
        echo 'Your e-mail has been sent!';
    }

I am getting following errors:-

Unable to send email using PHP mail(). Your server might not be configured to send mail using this method. &

Severity: Warning

Message: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Filename: libraries/Email.php

Line Number: 1896

Backtrace:

File: D:\xampp\htdocs\ajax\application\controllers\angular_http.php Line: 29 Function: send

File: D:\xampp\htdocs\ajax\index.php Line: 315 Function: require_once

2

2 Answers

0
votes

I think you are not configured mail server yet. if you are using XAMPP then you can easily send mail from localhost.

for example you can configure (considering 'C:\xampp' as installation directory) C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function] and change

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=my-gmail-password
[email protected]

First, make sure you PHP installation has SSL support (look for an "openssl" section in the output from phpinfo()).

You can set the following settings in your PHP.ini:

ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");

Please come up with stack you are using on localhost, If you are not using XAMPP or WAMP.

0
votes

The only thing you are doing wrong is email library loading and some config options: you are loading email library as $this->load->library('email', $config);.

which is wrong so load it as $this->load->library('email'); and initialize it as $this->email->initialize($config); see below for full code:

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => '[email protected]', // change it to yours
    'smtp_pass' => 'yourpassword', // change it to yours
    'mailtype' => 'text',// it can be text or html
    'wordwrap' => TRUE,
    'newline' => "\r\n",
    'charset' => 'utf-8',
    );
    $this->load->library('email');
    $this->email->initialize($config);
    $this->email->from('[email protected]',"Your Name");
    $this->email->to('[email protected]');
    $this->email->subject('Subject');
    $this->email->message('Sample message');
    if (!$this->email->send()) 
    {
        show_error($this->email->print_debugger()); 
    }
    else 
    {
        echo 'Your e-mail has been sent!';
    }