php - 使用 Mail() 类发送的电子邮件显示 HTML 代码而不是结果

标签 php html email opencart

这就是我使用 Mail() 发送电子邮件的方式来自 opencart 的类(class):

$mail = new Mail();
                $mail->protocol = $this->config->get('config_mail_protocol');
                $mail->parameter = $this->config->get('config_mail_parameter');
                $mail->hostname = $this->config->get('config_smtp_host');
                $mail->username = $this->config->get('config_smtp_username');
                $mail->password = $this->config->get('config_smtp_password');
                $mail->port = $this->config->get('config_smtp_port');
                $mail->timeout = $this->config->get('config_smtp_timeout');             
                //$mail->setTo($this->config->get('config_email'));
                //$mail->setFrom($this->request->post['email']);

                $mail->setTo('test1@test.com');
                $mail->setFrom('test@test.com');

                $mail->setSender($this->request->post['name']);
                $mail->setSubject(html_entity_decode('La reserva de tu alquiler ha sido correcta', ENT_QUOTES, 'UTF-8'));
                $mailText = '
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html>
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    </head>
                    <body>
                        <div class="confirmacion">
                            <h1>Su reseva se ha realizado  correctamente</h1>
                            <p> El pago del alquiler se hará en su totalidad al momento de recoger la bicicleta en Euros, o tarjeta de crédito. También se deberán aportar 50€ en forma de depósito reembolsable, el cual será devuelto al verificar que la bicicleta se devuelve limpia y en las mismas condiciones en las que se ha recodigo. Cualquier daño o perdida de la bicicleta de cualquiera de sus partes durante el periodo de alquiler son responsabilidad del cliente y deberá ser pagado acordemente.</p>
                        </div>
                    </body>
                </html>';



                $mail->setText($mailText);
                $mail->send();

问题是我没有看到 HTML 结果,而是看到了电子邮件中的 HTML 代码,

我做错了什么?

最佳答案

setText() 用于纯文本内容。对 HTML 内容使用 setHtml():

$mail->setHtml($mailText);

您可以同时设置两者,但显然不要将 HTML 传递给 setText()。如果您同时设置两者,收件人电子邮件客户端将根据他们的偏好决定显示哪个。

来自 Mail class 的片段:

public function setText($text) {
    $this->text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
}

public function setHtml($html) {
    $this->html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
}

关于php - 使用 Mail() 类发送的电子邮件显示 HTML 代码而不是结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313176/

相关文章:

php - Cakephp-3.x : How to change the data type of a selected alias?

php - 访问 xml 对象

html - &lt;input&gt; 超出 <div> block ,样式为 'float: right;'

html - 居中文本+图像CSS

email - 需要帮助优化标记电子邮件的谷歌应用程序脚本

php - 在mysql中绑定(bind)参数后是否可以更改查询?

php - MySQL 查询不适用于 avg 函数

javascript - 通过单击 JS 驱动的按钮创建 HTML 页面

不使用 javascript 的电子邮件中的 HTML 图表

powershell - Send-MailMessage 多个收件人