发送HTML电子邮件导致电子邮件显示HTML源代码(Codeigniter电子邮件类)

7

我在使用Codeigniter的电子邮件类发送电子邮件时遇到了问题,即在电子邮件消息中显示源HTML代码而不是呈现的HTML视图。为了测试,我目前正在Windows上使用XAMPP,并使用Gmail SMTP发送到同一个gmail地址。

发送电子邮件的函数如下:

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'myaccountname@gmail.com',
        '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邮件能够正常显示,而不是显示源代码呢?

您不能在电子邮件正文中使用PHP。我也不确定您是否需要<html>或<body>标记。 - Nik
@Nik:PHP正在生成发送的HTML,它实际上不在电子邮件正文中。 - Wesley Murch
第二个代码段不是电子邮件中的HTML吗?里面有PHP。显然,第一个代码段是生成。 - Nik
5个回答

28

20

试试这个

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

谢谢这个,真的很奇怪。需要设置两次吗?或者在 $this->load->library('email', $config); 中 $config 不起作用。再次感谢。 - Braian Mellor

6
在加载CodeIgniter邮件库后,请添加这两行代码:
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');

完整配置:https://dev59.com/-m3Xa4cB1Zd3GeqPcDaG#38740292

2
调用未定义的方法CI_Email :: set_header() - Braian Mellor

3

请添加下面示例中的注释行:

$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);

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

已经为我修复了这个问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接