如何使用Codigniter发送邮件?

时间:2016-07-19 01:42:52

标签: php codeigniter email

我试图使用codeigniter发送电子邮件功能,但它不起作用,没有发送任何内容,有什么帮助吗?

这是我的控制器:

public function send_mail() { 

     $this->load->library('session'); 
     $this->load->helper('form'); 
     $this->load->view('admin/main/email_form.php');

    $config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'xxxxxx@gmail.com', 
    'smtp_pass' => 'xxxxxx',
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE);

     $from_email = "xxxxxxx@gmail.com"; 
     $to_email = $this->input->post('email'); 
     $subject  = $this->input->post('subject');
     $message = $this->input->post('message');

     $this->load->library('email',$config); 
     $this->email->set_newline("\r\n");

     $this->email->from($from_email); 
     $this->email->to($to_email);
     $this->email->subject($subject); 
     $this->email->message($message);

     $this->email->send();       
  }     

2 个答案:

答案 0 :(得分:1)

尝试此操作并检查服务器的日志:

$config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.gmail.com',
    'smtp_port' => 465, // or 587
    'smtp_user' => 'xxx@gmail.com',
    'smtp_pass' => 'xxx',
    'charset' => 'utf-8',
    'mailtype' => 'html'
);

$this->load->library('email', $config);

$this->email->from($from_email); 
$this->email->to($to_email);
$this->email->subject($subject); 
$this->email->message($message);

if (!$this->email->send()) {
    echo $this->email->print_debugger();
}

更多示例: Sending email with gmail smtp with codeigniter email library

答案 1 :(得分:0)

您可以尝试以下代码

        $this->load->library('email');          
        $config['protocol'] = "smtp";
        $config['smtp_host'] = 'mail.domain.com'; //smtp host name
        $config['smtp_port'] = '587'; //smtp port 
        $config['smtp_user'] = 'mail@domain.com'; //smtp username
        $config['smtp_pass'] = '12345'; //smtp password
        $config['mailtype'] = 'html';
        $config['charset'] = 'utf-8';
        $config['newline'] = "\r\n";
        $config['wordwrap'] = TRUE;
        $this->email->initialize($config);          
        $this->email->from('from@gmail.com', 'website.com');
        $this->email->to('to@gmail.com');             
        $msg="This is test message";
        $this->email->subject('Test Subject');
        $this->email->message($msg);
        $this->email->send();