08 May, 2012

How to Test PHP Emails on Localhost

4:46 AM

While developing web applications on localhost you must have always missed the functionality to send mails. As localhost does not support email service, so it has always been a bottleneck for email testing on localhost. But I have found a simple way to test emails on localhost using PHP. I have overridden the default mail() function of PHP to create a text/html file based on the email format. If the email is in html format, an HTML file is created otherwise a text file is created. You can see in this file what exactly is the content sent in your mail. See how does it work.

Live demo Download Script

Overriding the mail() function

To override the PHP mail() function, we need to create our namespace and then define the function in that namespace.
#Create your own namespace
namespace localMail;

#Override the default mail function
function mail( $to, $subject, $message, $additional_headers='', $additional_parameters='' ) {
if (empty($to) || empty($subject) || empty($message)) {
return false;
}

#Check if email is sent as HTML or plaintext
if (isHTML($additional_headers)) {
$ext = 'html';
}else{
$ext = 'txt';
}

#Write the file
$myFile = "mail_".time().".".$ext;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $message);
fclose($fh);
}

#Function to detect HTML headers
function isHTML($isithtml) {
if (strpos($isithtml, 'html')) {
return true;
} else {
return false;
}
}

Usage

<html>
<head>
<title>Test Emails on localhost ! WebSpeaks.in</title>
</head>
<body>
<form method="post" action="send_mail.php">
<table align="center">
<tr>
<td>
Type your message<br />
<textarea name="message" cols="50" rows="5"><div style="color:#c3c;">This <i>email</i> is <u>sent</u> through <a href="#">localhost</a>.</div></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" class="button" name="send_text" value="Send as Plain Text" />
<input type="submit" class="button" name="send_html" value="Send as HTML" />
</td>
</tr>
</table>
</form>
</body>
</html>

send_mail.php

<?php
namespace localMail;
#Override the default mail function
function mail( $to, $subject, $message, $additional_headers='', $additional_parameters='' ) {
if (empty($to) || empty($subject) || empty($message)) {
return false;
}

#Check if email is sent as HTML or plaintext
if (isHTML($additional_headers)) {
$ext = 'html';
}else{
$ext = 'txt';
}

#Write the file
$myFile = "mail_".time().".".$ext;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $message);
fclose($fh);

header('Content-disposition: attachment; filename='.$myFile);
if (isHTML($additional_headers)) {
header('Content-type: application/html');
} else {
header('Content-type: application/text');
}
readfile($myFile);
unlink($myFile);
die();
}

#Function to detect HTML headers
function isHTML($isithtml) {
if (strpos($isithtml, 'html')) {
return true;
} else {
return false;
}
}

if (isset($_POST) && !empty($_POST)) {
$to="bhardwajs.on.height@gmail.com";
$subject="Test Local Mail on WebSpeaks";
$message = $_POST['message'];
if (isset($_POST['send_text']) && !empty($_POST['send_text'])) {
mail($to, $subject, $message);
} else {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
}
}
?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Jquery Giants. All rights resevered. Designed by Templateism

Back To Top