2016-06-30 94 views
0

我正在開發一個php codeigniter項目,我想從本地主機發送郵件。如何使用php發送本地主機的郵件CodeIgniter

以下是我的控制器功能。

 $config = Array(
       'protocol' => 'smtp', 
       'smtp_host' => 'ssl://smtp.google.com', 
       'smtp_port' => 465, 
       'smtp_user' => '[email protected]', 
       'smtp_pass' => 'password' 
    ); 

    $this->load->library('email',$config); 
    $this->email->set_newline("\r\n"); 

    $this->email->from("[email protected]"); 
    $this->email->to("[email protected]"); 
    $this->email->subject("Email with Codeigniter"); 
    $this->email->message("This is email has been sent with Codeigniter"); 

    if($this->email->send()) 
    { 
     echo "Your email was sent.!"; 
    } else { 
     show_error($this->email->print_debugger()); 
    } 

請注意,我已啓用php.ini中的'extension = php_openssl.dll'擴展名。我的php.ini文件位於C:/ AppServ/php5。當我運行代碼時,我的頁面加載錯誤。

這些都是錯誤的:

The following SMTP error was encountered: 1923818231 Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? Unable to send data: AUTH LOGIN Failed to send AUTH LOGIN command. Error: Unable to send data: MAIL FROM:

Severity: Warning

Message: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.

Filename: libraries/Email.php

Line Number: 705

+0

難道您激活' PHP中的php_openssl'擴展?通過編輯'php.ini'並刪除'#'註釋來激活它 – RiggsFolly

+0

我已經在php.ini中啓用了'extension = php_openssl.dll'擴展。我的php.ini文件位於C:/ AppServ/php5和C:/ AppServ/php7文件夾中 –

+0

那裏的'wherever \ apache \ bin'中的一個可能被web服務器使用的文件 – RiggsFolly

回答

1

使用PHPMailer的。它在這裏可用PHPMailer。您可以使用它像這樣:

public function send_mail() 
    { 
     require_once(APPPATH.'third_party/PHPMailer-master/PHPMailerAutoload.php'); 
     $mail = new PHPMailer(); 
     $mail->IsSMTP(); // we are going to use SMTP 
     $mail->SMTPAuth = true; // enabled SMTP authentication 
     $mail->SMTPSecure = "ssl"; // prefix for secure protocol to connect to the server 
     $mail->Host  = "smtp.gmail.com";  // setting GMail as our SMTP server 
     $mail->Port  = 465;     // SMTP port to connect to GMail 
     $mail->Username = "[email protected]"; // user email address 
     $mail->Password = "password";   // password in GMail 
     $mail->SetFrom('[email protected]', 'Mail'); //Who is sending 
     $mail->isHTML(true); 
     $mail->Subject = "Mail Subject"; 
     $mail->Body  = ' 
      <html> 
      <head> 
       <title>Title</title> 
      </head> 
      <body> 
      <h3>Heading</h3> 
      <p>Message Body</p><br> 
      <p>With Regards</p> 
      <p>Your Name</p> 
      </body> 
      </html> 
     '; 
     $destino = [email protected]; // Who is addressed the email to 
     $mail->AddAddress($destino, "Receiver"); 
     if(!$mail->Send()) { 
      return false; 
     } else { 
      return true; 
     } 
    } 

記得設置爲不甚可信的應用程序訪問Gmail帳戶

+0

但是要回答這個問題....請確保您已激活了由Apache/Apache使用的php.ini文件中的php_openssl擴展名, PHP – RiggsFolly

2

在你$config陣列,試試這個:

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