我无法使用Codeigniter发送任何电子邮件

时间:2017-10-16 23:10:20

标签: php codeigniter email

我已经创建了一个控制器并在控制器中创建了一个测试功能来测试是否发送了电子邮件。

我查了不同的电子邮件地址,但没有成功。这是我的代码示例:

public function sendmail() {
    $config['protocol'] = 'sendmail';
    $config['mailpath'] = '/usr/sbin/sendmail';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $user_id = 1;
    $name = 'Mark Alan';
    $this->email->set_newline("\r\n");
    $this->email->from('info@domainname.com', "Test Name");
    $this->email->to('test@domainname.com');
    $this->email->subject('Test Mail');
    $this->email->message('This is a test message');
    $this->email->send();
    echo $this->email->print_debugger();    
}

2 个答案:

答案 0 :(得分:3)

使用Codeigniter(假设您已自动加载电子邮件库),您可以在配置文件中设置电子邮件首选项,名为email.php,这些首选项将从那里自动加载。它可能看起来像:

// Setting Email Preferences 
$config['useragent'] = 'CodeIgniter';              // Mail engine switcher: 'CodeIgniter' or could be 'PHPMailer'
$config['protocol']  = 'smtp';                   // 'mail', 'sendmail', or 'smtp'
$config['mailpath']  = '/usr/sbin/sendmail';

或者在您的示例中,您可以设置手动,但需要初始化它,所以在定义$ config []数组后不要忘记这一行:

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

答案 1 :(得分:0)

试试这段代码:

function sendMail()
{
    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com', //Your Host
        'smtp_port' => 465, // Add 25 port if you sending from your smtp mail server 
        'smtp_user' => 'xxx@gmail.com', // change it to yours, server email
        'smtp_pass' => 'xxx', // change it to yours
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
);

    $message = '';
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('xxx@gmail.com'); // change it to yours
    $this->email->to('xxx@gmail.com');// change it to yours
    $this->email->subject('Resume from JobsBuddy for your Job posting');
    $this->email->message($message);
    if($this->email->send())
    {
        echo 'Email sent.';
    }
    else
    {
        show_error($this->email->print_debugger());
    }

}

注意:请添加您的smtp端口和您的smtp电子邮件帐户详细信息。