php 在显示 HTML 源代码的电子邮件中发送 HTML 电子邮件结果(Codeigniter 电子邮件类)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6274117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:52:10  来源:igfitidea点击:

Sending HTML Email results in Email displaying HTML source (Codeigniter Email Class)

phpmysqlemailcodeigniter

提问by Nyxynyx

I am having problems with email sent via the email class in Codeigniter displaying the source HTML code in the email message instead of the rendered HTML view. For testing, I am currently having CI on XAMPP on Windows, and using Gmail SMTP to send to the same gmail address.

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

The function that sends the email is as follows:

发送邮件的函数如下:

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

There is no problem getting the text version sent. The view loaded is a html file that will be emailed out.

发送文本版本没有问题。加载的视图是一个 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>

Any ideas how to get HTML emails sent to be rendered instead of displaying its source code?

任何想法如何让发送的 HTML 电子邮件被呈现而不是显示其源代码?

回答by Petah

Try adding the mailtypeto the config:

尝试添加mailtype到配置:

$config = Array(
    'mailtype' => 'html',
    ...etc...
);

See http://codeigniter.com/user_guide/libraries/email.html

请参阅http://codeigniter.com/user_guide/libraries/email.html

回答by ravisoni

Try this

尝试这个

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

回答by Pran

Add these two lines after loading your codeigniter mail library

加载 codeigniter 邮件库后添加这两行

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

full configuration: https://stackoverflow.com/a/38740292/4376484

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

回答by Prabhagaran

Append the line commented in the example below:

附加下面示例中注释的行:

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

回答by Jeff

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

has fixed that for me

已经为我修好了