Codeigniter电子邮件无法正常工作

时间:2015-08-11 10:42:00

标签: php codeigniter email

$this->load->library('email');
// set email data
$this->email->from($this->input->post('sender_email'), $this->input->post('sender_name'));
$this -> email -> set_mailtype("html");
$this->email->to('');
$this->email->reply_to($this->input->post('sender_email'), $this->input->post('sender_name'));
$this->email->subject('From: '.$this->input->post('sender_name'));

$this->email->message("<table><tr><td>&nbsp;</td><td>".$this->input->post('sender_name')."</td></tr><tr><td>&nbsp;</td><td>".$this->input->post('sender_email')."</td></tr><tr><td>&nbsp;</td><td>".$this->input->post('phone')."</td></tr><tr><td>Message:  </td><td>".$this->input->post('message')."</td></tr></table>");
$this->email->send();
// create a view named "succes_view" and load it at the end
redirect('contact?email_succ=1');
exit();

这是我用来从CI中的表单发送电子邮件的代码,它仅在我使用SMTP登录时才有效。

$this->load->library('email');
$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = 'myemail@example.com';
$config['smtp_pass']    = 'emailpassword';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'html'; // or html
$config['validation'] = TRUE; // bool whether to validate email or     not      
$this->email->initialize($config);

加载库后立即使用此代码可以解决问题。但是,默认代码一直在工作但突然停止了,这有什么问题?

更新:只添加此行$this->email->initialize($config);即可解决问题,但为什么?。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。

$config = array();
$config['useragent']           = "CodeIgniter";
$config['mailpath']            = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
$config['protocol']            = "smtp";
$config['smtp_host']           = "localhost";
$config['smtp_port']           = "25";
$config['mailtype'] = 'html';
$config['charset']  = 'utf-8';
$config['newline']  = "\r\n";
$config['wordwrap'] = TRUE;

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

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

$this->email->message = "Your Message";
$this->email->subject("Message Subject");
$this->email->from("user@gmail.com");
$this->email->to("admin@gmail.com");
$this->email->send();