如何使用codeigniter从localhost发送电子邮件?

时间:2015-06-08 05:48:20

标签: php codeigniter email



    function sendMail() {

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

        $message = 'test';
        $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from('xxx@gmail.com');
      $this->email->to('xyz@gmail.com');
      $this->email->subject('testing');
      $this->email->message($message);
      if($this->email->send())
     {
      echo 'Email sent.';    
     }
     else
    {
     show_error($this->email->print_debugger());  
    }

}




  

这是我的代码,我正在尝试使用codeigniter从localhost发送电子邮件,我收到消息"发送电子邮件。"但我没有在Gmail帐户中收到任何邮件。

4 个答案:

答案 0 :(得分:4)

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

然后

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

XAMPP中的邮件设置(无效)

$this->email->from('mygmail@gmail.com', 'myname');//your mail address and name
$this->email->to('target@gmail.com'); //receiver mail

$this->email->subject('testing');
$this->email->message($message);

$this->email->send(); //sending mail

sendmail.ini

中的配置

路径c:\xampp\sendmail\sendmail.ini

配置

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=myemail@gmail.com
auth_password=yourgmailpassword
force_sender=myemail@gmail.com
php.ini

中的

路径c:\xampp\xampp\php\php.ini

[mail function]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

答案 1 :(得分:0)

我发现您遗失$this->email->initialize($config);另外请确保您已在本地主机上设置了您的电子邮件设置,否则其他设置无效。

同样$config = Array(更改为$config = array(

替换Google Mail“gmail”

'smtp_host' => 'ssl://smtp.googlemail.com'

使用

'smtp_host' => 'ssl://smtp.gmail.com'

代码

function send_mail() {

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


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

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

$this->email->from('xxx@gmail.com');
$this->email->to('xyz@gmail.com');
$this->email->subject('testing');

$message = 'test';
$this->email->message($message);

if($this->email->send()) {
  echo 'Email sent.';    
} else {
  print_r($this->email->print_debugger());  
}

}

答案 2 :(得分:0)

电子邮件无法在本地发送。这个方法可以在测试服务器上进行测试..

答案 3 :(得分:0)

    $CI =& get_instance();
    $CI->load->library('email');
    $CI->email->initialize(get_email_config());

    $CI->email->from($data['email_from'],WEBSITE_TITLE); // Constant is define which exist email
    $CI->email->to($data['email_to']);
    if(isset($data['bcc']) && !empty($data['bcc'])){
        $CI->email->bcc($data['bcc']);
    }else{
        $CI->email->bcc('other_email');
    }
    if(isset($data['upload'])&& !empty($data['upload'])){
        $CI->email->attach($data['upload']);
    }
    $CI->email->subject($data['subject']);
    $CI->email->message($data['body']);
    $CI->email->send(TRUE);