Tuesday, May 8, 2012

How to Test PHP Emails on Localhost

5/08/2012

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

Arvind is a web developer, programmer and blogger. He has expertise in PHP, Magento, WordPress, jQuery, JavaScript, HTML5 and CSS3. He loves to develop good looking websites with strong backend.

1 comments :

  1. Nice post. I was checking constantly this weblog and I am impressed!
    Very useful info specifically the closing section :) I take care of such information a lot.
    I was seeking this certain info for a very lengthy time.
    Thank you and good luck.

    You really should view my own online video about how to legitimately make money online by going right here

    ReplyDelete

We would love to hear from you...

 

© 2014 Web Speaks . All rights resevered. Designed by Templateism