2012-02-01 39 views
19

在codeigniter中使用SMTP的郵件內容錯誤 實際上,我的郵件與HTML標籤一起發送,它顯示HTML標籤不正確。使用codeigniter發送html郵件

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

    ); 
    $this->load->library('email', $config); 
    $this->email->set_newline("\r\n"); 
    $email_body ="<div>hello world</div>"; 
    $this->email->from('[email protected]', 'ddd'); 

    $list = array('[email protected]'); 
    $this->email->to($list); 
    $this->email->subject('Testing Email'); 
    $this->email->message($email_body); 

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

如果我不使用SMTP發送郵件,它工作正常。我的錯誤是什麼?

+0

我不認爲'ssl://'是主機名的一部分。也許還有另一種設置打開SSL? – 2012-02-03 21:00:05

+0

codeigniter完整的html電子郵件配置:http://stackoverflow.com/a/38740292/4376484 – Pran 2016-08-03 14:26:30

回答

66

你可以試試這行代碼將設置郵件類型是HTML

$this->email->set_mailtype("html"); 
+0

謝謝!這工作。 – RY35 2014-08-26 07:08:43

0

我的問題是笨的全球XSS過濾被編碼一些HTML標籤,如<html>這樣的電子郵件客戶端可以不再認識他們。

爲了解決這個問題,請查看我的其他post

3

設置郵件類型HTML作品對我來說:

$email_setting = array('mailtype'=>'html'); 
$this->email->initialize($email_setting); 
16

由於CodeIgniter 3.x。添加了許多功能。這個例子與早期版本幾乎相同,但你可以做更多。

請按照鏈接獲取文檔。

// load email library 
$this->load->library('email'); 

// prepare email 
$this->email 
    ->from('[email protected]', 'Example Inc.') 
    ->to('[email protected]') 
    ->subject('Hello from Example Inc.') 
    ->message('Hello, We are <strong>Example Inc.</strong>') 
    ->set_mailtype('html'); 

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

如果您有模板設計。您還可以包括message方法類似這樣的模板......

->message($this->load->view('email_template', $data, true)) 

在這裏,第一個參數是email_template.php在views目錄,數據被髮送到電子郵件模板第二個參數,你可以設置它''array()[]如果不傳遞任何動態數據,並且最後一個參數true確保您抓取模板數據,而不是輸出。

希望這是有幫助的。

0

你可以請用這個代碼嘗試,B'z我可以發送HTML電子郵件與此代碼。

$configemail = Array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.gmail.com', //mail.webconsort.com 
     'smtp_port' => 465, //5074 
     'smtp_user' => '[email protected]', //[email protected] 
     'smtp_pass' => 'XXXXXXXX', //'T0r0r1d3' 
     'mailtype' => 'html', 
     'charset' => 'iso-8859-1' 
    ); 

    $CI =& get_instance(); 

    $CI->load->library('email', $configemail); 

    $CI->email->initialize($configemail); 
    $CI->email->set_newline("\r\n"); 

    $CI->email->from($from, $fromName); 
    $CI->email->to($to); 

    $CI->email->subject($subject); 
    $CI->email->message($body); 
    if ($attachments != null && !empty($attachments)){ 
     foreach($attachments as $a){ 
      $CI->email->attach($a); 
     } 
    } 

    try{ 
     $CI->email->send(); 
     return true; 
    } 
    catch (Exception $e){ 
     //var_dump($e); 
    } 
0

使用它像這樣..它對我很好。

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

$config['charset'] = 'iso-8859-1'; 

$config['wordwrap'] = TRUE; 

$config['mailtype'] = 'html'; 

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

$this->email->from($fromemail); 

$this->email->to($toemail); 

$this->email->subject('Subject'); 

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

$success=$this->email->send(); 
1

Gmail禁止訪問您的帳戶。您需要在Gmail的一些變化: -

第1個:

某些應用和設備使用安全性較低的登錄技術,這使得 您的帳戶更加脆弱。您可以關閉我們推薦的這些應用程序的訪問權限, ,或者打開訪問權限,如果您想要使用這些應用程序,儘管存在 的風險。

Turn on less secure app

第2個:

啓用IMAP狀態
啓用POP狀態

Enable IMAP and POP Status