Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email sent with Mail() class shows the HTML code and not the result of it

this is how i am sending an email using the Mail() class from opencart:

$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('[email protected]');
                $mail->setFrom('[email protected]');

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

The problem is that instead of seeing the HTML result, i see the HTML code in the email,

what am I doing wrong?

like image 443
Toni Michel Caubet Avatar asked Mar 22 '23 02:03

Toni Michel Caubet


1 Answers

setText() is for text only content. Use setHtml() for HTML content:

$mail->setHtml($mailText);

You can set both, but obviously don't pass HTML to setText(). If you set both, the recipients email client will decide which to display, based on their preferences.

Snippet from the 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');
}
like image 122
MrCode Avatar answered Apr 20 '23 00:04

MrCode