Sending email with gmail smtp with codeigniter email library

后端 未结 8 2185
情书的邮戳
情书的邮戳 2020-11-22 06:29
load->library(\'email\');
    }

    func         


        
8条回答
  •  一生所求
    2020-11-22 07:09

    According to the CI docs (CodeIgniter Email Library)...

    If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() function if you save your preferences in a config file.

    I was able to get this to work by putting all the settings into application/config/email.php.

    $config['useragent'] = 'CodeIgniter';
    $config['protocol'] = 'smtp';
    //$config['mailpath'] = '/usr/sbin/sendmail';
    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
    $config['smtp_user'] = 'YOUREMAILHERE@gmail.com';
    $config['smtp_pass'] = 'YOURPASSWORDHERE';
    $config['smtp_port'] = 465; 
    $config['smtp_timeout'] = 5;
    $config['wordwrap'] = TRUE;
    $config['wrapchars'] = 76;
    $config['mailtype'] = 'html';
    $config['charset'] = 'utf-8';
    $config['validate'] = FALSE;
    $config['priority'] = 3;
    $config['crlf'] = "\r\n";
    $config['newline'] = "\r\n";
    $config['bcc_batch_mode'] = FALSE;
    $config['bcc_batch_size'] = 200;
    

    Then, in one of the controller methods I have something like:

    $this->load->library('email'); // Note: no $config param needed
    $this->email->from('YOUREMAILHERE@gmail.com', 'YOUREMAILHERE@gmail.com');
    $this->email->to('SOMEEMAILHERE@gmail.com');
    $this->email->subject('Test email from CI and Gmail');
    $this->email->message('This is a test.');
    $this->email->send();
    

    Also, as Cerebro wrote, I had to uncomment out this line in my php.ini file and restart PHP:

    extension=php_openssl.dll
    

提交回复
热议问题