配置电子邮件库,用于在codeigniter中发送电子邮件

时间:2016-06-30 10:29:25

标签: codeigniter email send

我在控制器codeigniter框架中有一个代码如下:

class Welcome extends CI_Controller {
public function index()
{
    $this->load->view('welcome_message');

    $this->load->library('email'); 
    $this->email->from('example@yahoo.com', 'example');
    $this->email->to('example@gmail.com','example');
    $this->email->subject('my Subject');
    $this->email->message('my Message');

    if ($this->email->send())
        echo "Mail Sent!";
    else
        echo "There is error in sending mail!";    
    }
}

此代码正确发送电子邮件。但是,当我配置电子邮件类设置时,此代码不起作用,不发送电子邮件。例如,关注代码不会发送电子邮件:

class Welcome extends CI_Controller {
public function index()
{
    $this->load->view('welcome_message');

    $this->load->library('email'); 
    $this->email->from('example@yahoo.com', 'example');
    $this->email->to('example@gmail.com','example');
    $this->email->subject('my Subject');
    $this->email->message('my Message');

    $config['protocol'] = 'sendmail';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $this->email->initialize($config);

    if ($this->email->send())
        echo "Mail Sent!";
    else
        echo "There is error in sending mail!";    
    }
}

请告诉我问题出在哪里。 谢谢。

1 个答案:

答案 0 :(得分:1)

协议可以是SendmailSMTPmail

您应该在配置后加载库:

$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");

$this->email->from('example@yahoo.com', 'example');
$this->email->to('example@gmail.com','example');
$this->email->subject('my Subject');
$this->email->message('my Message');

if ($this->email->send())
{
    echo "Mail Sent!";
}
else
{
    echo "There is error in sending mail!";    
}