简体   繁体   中英

CodeIgniter generates error when sending email

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

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'.

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM