通过codeigniter发送电子邮件

时间:2015-12-12 14:25:45

标签: codeigniter email

我使用codeigniter发送电子邮件。

function forget_pw() {
    $this->load->library('email');

    $config['smtp_host'] = 'host address';
    $config['smtp_user'] = 'username';
    $config['smtp_pass'] = 'password';

    $this->email->initialize($config);

    $this->email->from('admin@email.com', 'Admin');
    $this->email->to('test@yahoo.com.hk'); 

    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');  

    $this->email->send();

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

控制台中没有显示任何错误。但电子邮件没有发出。

1 个答案:

答案 0 :(得分:0)

您可以创建如下函数:

public function sendEmail($subject,$message)
{

    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'xxxxx.xxxxxx.xxxxx',
        'smtp_port' => 465,
        'smtp_user' => 'xxxxx.xxxxxx.xxxxx',
        'smtp_pass' => 'xxxxx.xxxxxx.xxxxx',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
    );

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('xxxxx.xxxxxx.xxxxx');
    $this->email->to('xxxxx.xxxxxx.xxxxx');
    $this->email->subject($subject);
    $this->email->message($message);
    if($this->email->send())
    {
        echo 'Email send.';
    }
    else
    {
        show_error($this->email->print_debugger());
    }
}

并像这样使用它$this->sendEmail('Title', 'Message');祝你好运