Skip to content
Advertisement

Send HTML in email via PHP

How can I send an HTML-formatted email with pictures using PHP?

I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?

The main problem is to attach files. How can I do that?

Advertisement

Answer

It is pretty simple. Leave the images on the server and send the PHP + CSS to them…

$to = 'bob@example.com';

$subject = 'Website Change Request';

$headers  = "From: " . strip_tags($_POST['req-email']) . "rn";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "rn";
$headers .= "CC: susan@example.comrn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=UTF-8rn";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';


mail($to, $subject, $message, $headers);

It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:

$headers .= "Content-Type: text/html; charset=UTF-8rn";

Here is the link I got the information from… (link)

You will need security though…

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement