Send HTML in email via PHP

I have this code and it will run perfectly for my site:

public function forgotpassword($pass, $name, $to)
{
    $body  = "<table width=100% border=0><tr><td>";
    $body .= "<img width=200 src='";
    $body .= $this->imageUrl();
    $body .= "'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
    $body .= '<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
    $body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
    $body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:[email protected]" target="_blank">[email protected]</a></td></tr>';
    $body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
    $subject = "Forgot Password";
    $this->sendmail($body, $to, $subject);
}

Mail function

function sendmail($body, $to, $subject)
{
    //require_once 'init.php';

    $from = '[email protected]';
    $headersfrom = '';
    $headersfrom .= 'MIME-Version: 1.0' . "\r\n";
    $headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headersfrom .= 'From: ' . $from . ' ' . "\r\n";
    mail($to, $subject, $body, $headersfrom);
}

The image URL function is used for if you want to change the image. You have to it change in only one function. I have many mail functions, like for forgot password or create user. Therefore I am using the image URL function. You can directly set the path.

function imageUrl()
{
    return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}

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);
?>

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...

Tags:

Html

Php

Email

Send