2017-06-12 49 views
1

我想使用CodeIgniter通過我的Gmail地址發送電子郵件。 我嘗試了很多解決方案,但都沒有工作。如何使用CodeIgniter發送Gmail電子郵件

我的代碼:

$config = array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.googlemail.com', 
     'smtp_port' => 465, 
     'smtp_user' => '[email protected]', 
     'smtp_pass' => 'yyyyy', 
     'mailtype' => 'html', 
     'charset' => 'utf-8', 
     'wordwrap' => TRUE 
    ); 


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

    $this->email->from('[email protected]', 'admin'); 
    $this->email->to('[email protected]'); 
    $this->email->subject('Email test'); 
    $msg = "Testing email."; 

    $this->email->message($msg); 

    $this->email->send(); 

    echo $this->email->print_debugger(); 

我有以下錯誤:

Message: fwrite(): send of 5 bytes failed with errno=32 Broken pipe 
Filename: libraries/Email.php 

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method. 

就目前而言,我'在本地主機的工作。

謝謝你幫助我。

+0

我想你沒有讓你的Gmail帳戶使用SMTP的。你需要檢查你的谷歌SMTP設置。 – kishor10d

回答

0

請使用以下提到的解決方案

$config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.googlemail.com', 
    'smtp_port' => 465, 
    'smtp_user' => 'xxx', 
    'smtp_pass' => 'xxx', 
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1' 
); 
$this->load->library('email', $config); 
$this->email->set_newline("\r\n"); 

// Set to, from, message, etc. 

$result = $this->email->send(); 

而且在php.ini中,請啓用OpenSSL的

// Find the line. 
;extension=php_openssl.dll 

// Remove ";" from this line to enable 
extension=php_openssl.dll 
+0

'smtp_user'地址和'from'地址應該相同嗎? – iAmoric

+0

不知何故。請在這裏找到https://stackoverflow.com/questions/1332510/how-to-change-from-address-when-using-gmail-smtp-server –

+0

我找不到'extension = php_openssl.dll'在php.ini文件中 – iAmoric

0

有關本地主機發送Gmail電子郵件,你可以在我的答案看一看here

在本地開發中測試codeigniter電子郵件功能。

+0

你在你的linux機器上使用了什麼本地網絡服務器?也許你應該在你的linux服務器上配置SMTP –

0

在CodeIgniter中,您可以通過創建email.php文件來配置電子郵件數據。 我用了很長時間。所以它也可以爲你工作。

裏面的檔案:

$config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'ssl://smtp.gmail.com', 
    'smtp_port' => 465, 
    'smtp_user' => '[email protected]', // change it to yours 
    'smtp_pass' => 'your password', // change it to yours 
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1', 
    'wordwrap' => TRUE, 
    'from' => '[email protected]', 
    'subject' => 'test email' 
); 

現在控制器,你可以這樣做:

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

    $subject = $this->config->item('subject'); 
    $from = $this->config->item('from'); 

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

    $this->email->from($from, 'Yards & Acares'); 
    $data = array(
     'some_data' => $email    
    ); 

    $this->email->to($email); // replace it with receiver mail id 
    $this->email->subject($subject); // replace it with relevant subject 

    // Here is I have added an email template. 
    $body = $this->load->view('admin/propMailer.php', $data, TRUE); 

    $this->email->message($body); 

    if ($this->email->send()) { 
     //Do whatever you want if success 
    } else { 
     //Do whatever you want if failed 
     log_message('error', 'Unable to Send Email to Customer.' . print_r($this->email->print_debugger(array('headers', 'subject')), TRUE)); 

    } 
相關問題