2011-06-08 62 views
6

我在通過電子郵件發送的電子郵件類中發送電子郵件問題,在電子郵件中顯示源HTML代碼而不是呈現的HTML視圖。爲了進行測試,我目前在Windows上的XAMPP上使用CI,並使用Gmail SMTP發送到相同的Gmail地址。發送HTML電子郵件結果顯示HTML源代碼(Codeigniter電子郵件類)

發送電子郵件的功能如下:

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

    $this->load->library('email', $config); 
    $this->email->from($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth')); 
    $this->email->reply_to($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth')); 
    $this->email->to($email); 
    $this->email->subject(sprintf($this->lang->line('auth_subject_'.$type), $this->config->item('website_name', 'tank_auth'))); 
    $this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE)); 
    $this->email->set_alt_message($this->load->view('email/'.$type.'-txt', $data, TRUE)); 

    $this->email->set_newline("\r\n"); // require this, otherwise sending via gmail times out 

    $this->email->send(); 

是沒有問題越來越發送的文字版本。加載的視圖是一個將通過電子郵件發送出去的html文件。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head><title>Welcome to <?php echo $site_name; ?>!</title></head> 
<body> 
<div style="max-width: 800px; margin: 0; padding: 30px 0;"> 
<table width="80%" border="0" cellpadding="0" cellspacing="0"> 
<tr> 
<td width="5%"></td> 
<td align="left" width="95%" style="font: 13px/18px Arial, Helvetica, sans-serif;"> 
<h2 style="font: normal 20px/23px Arial, Helvetica, sans-serif; margin: 0; padding: 0 0 18px; color: black;">Welcome to <?php echo $site_name; ?>!</h2> 
Thanks for joining <?php echo $site_name; ?>. We listed your sign in details below, make sure you keep them safe.<br /> 
To verify your email address, please follow this link:<br /> 
<br /> 
<big style="font: 16px/18px Arial, Helvetica, sans-serif;"><b><a href="<?php echo site_url('/auth/activate/'.$user_id.'/'.$new_email_key); ?>" style="color: #3366cc;">Finish your registration...</a></b></big><br /> 
<br /> 
Link doesn't work? Copy the following link to your browser address bar:<br /> 
<nobr><a href="<?php echo site_url('/auth/activate/'.$user_id.'/'.$new_email_key); ?>" style="color: #3366cc;"><?php echo site_url('/auth/activate/'.$user_id.'/'.$new_email_key); ?></a></nobr><br /> 
<br /> 
Please verify your email within <?php echo $activation_period; ?> hours, otherwise your registration will become invalid and you will have to register again.<br /> 
<br /> 
<br /> 
<?php if (strlen($username) > 0) { ?>Your username: <?php echo $username; ?><br /><?php } ?> 
Your email address: <?php echo $email; ?><br /> 
<?php if (isset($password)) { /* ?>Your password: <?php echo $password; ?><br /><?php */ } ?> 
<br /> 
<br /> 
Have fun!<br /> 
The <?php echo $site_name; ?> Team 
</td> 
</tr> 
</table> 
</div> 
</body> 
</html> 

任何想法如何讓HTML電子郵件發送呈現,而不是顯示其源代碼?

+0

你不能有PHP在一封電子郵件中身體。我也不確定你需要或標籤。 – Nik 2011-06-08 04:13:13

+0

@Nik:PHP是*生成*發送的HTML,它實際上不在電子郵件正文中。 – 2011-06-08 04:16:41

+0

不是電子郵件中HTML的第二個代碼段嗎?那裏有PHP。顯然,第一個代碼段是一代。 – Nik 2011-06-08 04:18:41

回答

1
Set $config['protocol'] = "sendmail"; 

已經固定,對我

15

試試這個

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

這適用於,謝謝 – 2016-09-16 08:11:42

+0

謝謝你,真的是extrange。需要設置兩次?或者在$ this-> load-> library('email',$ config)中; $ config它不起作用。再次感謝 – 2016-11-24 13:54:46

3

追加線com mented在下面的例子:

$config['protocol'] = 'sendmail'; 
$config['mailpath'] = '/usr/sbin/sendmail'; 
$config['charset'] = 'iso-8859-1'; 
$config['wordwrap'] = TRUE; 
$config['mailtype'] = 'html'; // Append This Line 
$this->email->initialize($config); 
4

添加這兩行加載你笨郵件庫

$this->email->set_header('MIME-Version', '1.0; charset=utf-8'); 
$this->email->set_header('Content-type', 'text/html'); 

完整的配置後:https://stackoverflow.com/a/38740292/4376484

+2

調用未定義的方法CI_Email :: set_header() – 2016-11-24 13:52:53