无法使用CODEIGNITER发送MAIL

时间:2017-05-09 20:25:20

标签: php codeigniter email ssl smtp

我一直在搜索关于此的信息,但找不到任何信息。这是我的代码(来自控制器):

public function send_mail(){
    $config = Array(
        'protocol' => 'smtp',
        'smt_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => '465',
        'smtp_user' => 'mymail@gmail.com',
        'smtp_pass' => 'password',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
    );

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

    $this->email->set_newline("\r\n");

        $this->email->from('mymail@gmail.com', 'MyName');
        $this->email->to('mymail@gmail.com');

        $this->email->subject('Message');
        $this->email->message('Some text.');

        if ($this->email->send())
            echo "twas sent";
        else show_error ($this->email->print_debugger());      
}

现在我从调试器得到的错误是:

You did not specify a SMTP hostname.
Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

Date: Tue, 9 May 2017 22:16:37 +0200
From: "MyName" <mymail@gmail.com>
Return-Path: <mymail@gmail.com>
To: mymail@gmail.com
Subject: =?ISO-8859-1?Q?=47=65=74=47=61=69=6E=73=20=4D=65=73=73=61=67=65?=
Reply-To: <mymail@gmail.com>
User-Agent: CodeIgniter
X-Sender: mymail@gmail.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <591223a537e10@gmail.com>
Mime-Version: 1.0


Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

Some text.

事情是我无法理解我怎么没有指定SMTP主机名?我尝试了像465和587这样的端口,尝试过像'ssl://smtp.gmail.com','ssl://smtp.googlemail.com','smtp.gmail.com'等主机。

P.S。而不是mymail@gmail.com和密码代表我真正的Gmail密码。

1 个答案:

答案 0 :(得分:1)

试试这个代码,它对我有用

$from_email = " "; 
         $to_email = $this->input->post('email'); 

         //Load email library 

                $config['charset']    = 'utf-8';
                $config['newline']    = "\r\n";
                $config['mailtype'] = 'html'; 
                $config['validation'] = TRUE;      
        $this->email->initialize($config);
         $this->email->from($from_email, $this->input->post('fname')); 
         $this->email->to($to_email);
         $this->email->subject('Registration Success');
        $first_name=$this->input->post('fname');
        $email=$this->input->post('email');
            $email_subject = 'User Registration';                       

            $email_body = '';


         $this->email->message($email_body); 
            $this->email->send();
         //Send mail