简体   繁体   English

发送电子邮件时,CodeIgniter生成错误

[英]CodeIgniter generates error when sending email

I amusing the code below to send an email using codeigniter but I keep getting the error 我正在使用下面的代码使用codeigniter发送电子邮件,但我不断收到错误消息

Unable to send email using PHP mail(). 无法使用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. 默认情况下,您的电子邮件协议设置为'mail',但是您可能需要将其切换为'sendmail'

$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). 这将需要安装sendmail的服务器上( sudo apt-get install sendmail ,如果你在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'. $this->email->send()使用PHP的内置mail()函数,根据这篇文章 ,即使您的协议未明确设置为'sendmail',它可能已经尝试使用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. PHP mail功能需要一台邮件服务器才能正常工作,如果运行Web应用程序的系统未进行设置和配置,则您将收到此错误。 Luckily you can change the default protocol to use free mailing servers like gmail through the use of SMTP. 幸运的是,您可以通过使用SMTP更改默认协议,以使用免费邮件服务器(例如gmail)。 Look into CodeIgniter's email class docs and see if you can set up the SMTP connection to your email host. 查看CodeIgniter's email class文档,看看是否可以建立与电子邮件主机的SMTP连接。

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 阅读有关Codeigniter 电子邮件帮助程序类的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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