Broad Network


Sending Web Page as Email with PHP

Sending Email with PHP – Part 5

Forward: You can send a web page as email. That is what this article is about.

By: Chrysanthus Date Published: 29 Jul 2012

Introduction

This is part 5 of my series, Sending Email with PHP. Hey, you can send a web page as email. That is what this article is about. You will like it. Many emails today are displayed as a web page. I assume you have read the previous parts of this series before reading this one.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Secret
A web page is an HTML document. PHP uses the mail function to send email. This function needs the arguments, $to, $subject, $message and $additional_headers. The secret is, your $message content is the HTML document and the $additional_headers should include the following fields:

    MIME-Version: 1.0
    Content-type: text/html; charset=iso-8859-1

Do not worry much about the meaning of these fields for now. Of course, $to should have the recipient email, $subject should have the subject and $additional_headers should also have the From email address.

Example
Let us assume that me@me.com is sending an HTML document to him@him.com by email and the subject is, Web Page Email Illustration. Let us also assume that the HTML document is,

<html>
<head>
    <title>Title here</title>
</head>
<body>
    Your personal message goes here.
</body>
</html>

The personal information and any HTML tags go into the HTML BODY element. The following PHP code will send the web page as email.

<?php

$to = "him@him.com";
$subject = "Web Page Email Illustration";
$additional_headers = "From: me@me.com\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1";

$message = "
<html>
<head>
    <title>Title here</title>
</head>
<body>
    Your personal message goes here.
</body>
</html>
";

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

?>

Everything in the code should be self-explanatory apart from the $message content. The $message content is the HTML document. No line in the $message (and header fields) should be longer than 70 characters. In the above case no line is longer than 70 characters because of the following reason: The content of the $message variable is a string. In PHP you can either end a line in a string with the n character as we saw in part 1 and part 2 of this series, or you can press the enter key as you type (in a text editor). When you press the enter key while typing your PHP in a text editor (or equivalent application) the n character is inserted without you seeing. So the above $message string is OK and no line in it is longer than 70 characters (spaces included).  

User Agent of Recipient
The user agent (software) of the recipient will have to display the $message content at a browser, when he requests to read his email. The user agent can have ways to fit the To, From and other email header fields in the displayed (message) web page.

Images
What you have sent to the recipient is an HTML document. So if you want the recipient to see images in the email, you simply have to type the image tags in the HTML BODY element of the HTML document sting in $message.

Note; You must know in advance that the recipient’s user agent can display emails as web pages at a browser, before you sent web page as email.

We have come to the end of the series. I hope after designing a web site you will be in the position to design a PHP script to send form info as email. You should also be able to do other email stuffs with PHP. I hope you appreciated the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course

Comments

Become the Writer's Fan
Send the Writer a Message