1

I amusing the code below to send an email using codeigniter but I keep getting the error

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

        $this->email->from("kariuki.john@gmail.com", "Name");
        $this->email->to('johnkariukin@gmail.com');
        $this->email->cc('contact@johnkariuki.co.ke');
        $this->email->subject("New Email from on johnkariuki.co.ke");
        $this->email->message("abdfdfj\nfdgdfgdf");

        if($this->email->send())
        {
            echo "works";

        } else {

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

What could be the issue? I cannot find a working solution online. What could be the issue? I cannot find a working solution online. I have loaded the email library in the constructor

John Kariuki
  • 4,966
  • 5
  • 21
  • 30

3 Answers3

0

By default, your email protocol is set to 'mail', but you may need to switch it to 'sendmail' instead.

$config['protocol'] = 'sendmail';
$this->email->initialize($config);

This will require you to install sendmail on your server (sudo apt-get install sendmail if you're on Ubuntu).

$this->email->send() uses PHP's builtin mail() function, and according to this post it may already be trying to use sendmail even if your protocol isn't explicitly set to 'sendmail'.

Community
  • 1
  • 1
seane
  • 589
  • 1
  • 4
  • 15
  • I am working from a hosted site so I do not think I can install anything as you suggested.. Actuaally when I use the some code on my xampp it works.. but on a hosted site it does not. – John Kariuki Jul 13 '15 at 16:42
  • 1
    It's possible that the server already has sendmail on it. Have you tried switching your protocol to 'sendmail' to see if it works? – seane Jul 13 '15 at 16:47
0

the PHP mail function requires a mailing server in order to work, if the system running the web application doesn't have one set up and configured then you'll receive this error. Luckily you can change the default protocol to use free mailing servers like gmail through the use of SMTP. Look into CodeIgniter's email class docs and see if you can set up the SMTP connection to your email host.

Here's an example of how I've set up my localhost mail sending:

private function mail($to, $subject, $message){
    $this->load->library('email');
    if(!IN_PRODUCTION){ //Defined in the environment switch in index.php
        $this->email->initialize(
            array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.gmail.com',
                'smtp_user' => '******@gmail.com',
                'smtp_pass' => '******',
                'smtp_port' => 465,
                'smtp_timeout' => 7,
                'mailtype' => 'html',
                'crlf' => "\r\n",
                'newline' => "\r\n",
                'validation' => TRUE
            )
        );
    }
    $this->email->from('******@gmail.com', 'iam-decoder');
    $this->email->to($to);
    $this->email->subject($subject);
    $this->email->message($message);
    return $this->email->send();
}
iam-decoder
  • 2,554
  • 1
  • 13
  • 28
0

Before send mail Config these

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'xxx',
    'smtp_pass' => 'xxx',
    'mailtype'  => 'html', 
    'charset'   => 'iso-8859-1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

// Set to, from, message, etc.

$result = $this->email->send();

Then add the rest of mail

$this->email->from("kariuki.john@gmail.com", "Name");
$this->email->to('johnkariukin@gmail.com');
$this->email->cc('contact@johnkariuki.co.ke');
$this->email->subject("New Email from on johnkariuki.co.ke");
$this->email->message("abdfdfj\nfdgdfgdf");

if($this->email->send())
   {
     echo "works";

   } else {

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

Read More about Codeigniter E-Mail Helper class

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85