2012-02-01 40 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