如何在localhost上的codeigniter中发送电子邮件

时间:2015-11-30 12:46:12

标签: php codeigniter email

你好我必须在localhost上发送电子邮件 以下是我的配置文件

$config['useragent']      = "CodeIgniter";
$config['mailpath']       = "/usr/sbin/sendmail -t -i";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xyz.com'; //change this
$config['smtp_port'] = '465';
$config['smtp_user'] = 'xyz.com'; //change this
$config['smtp_pass'] = '######'; //change this
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard

我为电子邮件发送创建代码以下代码

if(count($result) > 0 || $result != NULL){
                    $password=$result['password'];
                    $this->load->library('email');
                    $from='chirag@site.com';
                    $to=$email;
                    $subject='forget password';                        
                    $this->email->from($from);
                    $this->email->to($to);
                    $this->email->subject($subject);
                    $this->email->message($password);
                    if($this->email->send()){
                        $this->session->set_flashdata('email','We sent your password to your Email...!');
                        redirect('login');
                    }
                    else{            
                        echo $this->email->print_debugger();
                    }
            }

但是当我发送电子邮件时出现以下错误

The following SMTP error was encountered: 
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.
那么问题是什么? 请帮我解决

2 个答案:

答案 0 :(得分:1)

我得到了答案 在配置中缺少ssl:// 但在写完之后

$config['smtp_host'] = 'ssl://xyz.com';

邮件发送成功...

答案 1 :(得分:1)

首先确保您已为sending emails完成了正确的localhost配置。 然后在您的控制器中,通过设置适当的凭据和端口号来加载电子邮件库。像这样:

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