这就是我使用 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/