Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php mail: how to send html?

The code below is sending an email correctly but for the body. I need to show html in the body of the message and I cannot make it. The examples in the web won't send the email :(

How can I fix my code to send the email with the html in the body?

Thanks a ton!

<?php

$to = '[email protected]';

$subject = 'I need to show html'; 

$from ='[email protected]'; 

$body = '<p style=color:red;>This text should be red</p>';

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
  $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) {

  echo("<p>Sent</p>");
 } else {
  echo("<p>Error...</p>");
 }

?>
like image 673
lleoun Avatar asked Feb 04 '11 10:02

lleoun


People also ask

How can I put a HTML link inside an email body in PHP?

You need to specify a Content Type of HTML in your function. // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers . = 'Content-type: text/html; charset=iso-8859-1' .

How can I send PHP file through email?

The PHP mail() function with some MIME type headers can be used to send email with attachment in PHP. In the following example code, MIME and Content-Type headers are used with mail() function to send email with attachment using PHP. $to – Recipient email address. $from – Sender email address.


1 Answers

use this header for the mail:

 $header  = "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html; charset: utf8\r\n";

and for the content/body:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...

it's important to use inline css commands and recommanded to use tables for the interface.

...

In your Mail-Body you than have to put HTML code with head and body

like image 145
helle Avatar answered Sep 27 '22 22:09

helle