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
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);
?>
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 DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course