Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send HTML in email via PHP

Tags:

html

php

email

send

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?

like image 971
Abadis Avatar asked Jun 28 '12 06:06

Abadis


2 Answers

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

$to = '[email protected]';

$subject = 'Website Change Request';

$headers  = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$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-8\r\n";

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

You will need security though...

like image 66
Chris Avatar answered Nov 03 '22 02:11

Chris


You need to code your HTML content using the absolute path for images. By absolute path, I mean you have to upload the images to a server and in the src attribute of images you have to give the direct path, like this <img src="http://yourdomain.com/images/example.jpg">.

Below is the PHP code for your reference: It’s taken from mail:

<?php
    // Multiple recipients
    $to  = '[email protected]' . ', '; // Note the comma
    $to .= '[email protected]';

    // Subject
    $subject = 'Birthday Reminders for August';

    // Message
    $message = '
      <p>Here are the birthdays upcoming in August!</p>
    ';

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

    // Additional headers
    $headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
    $headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";


    // Mail it
    mail($to, $subject, $message, $headers);
?>
like image 21
Subhajit Avatar answered Nov 03 '22 04:11

Subhajit