PHP Mail Function Not Sending

Okay, today, I have a treat for you. If you are having issues with PHP mail, I probably have a solution for you somewhere in my head, so I am going to attempt to put it down here in my blog so it is documented. I work on a lot of different client’s websites and most of them have unique servers. It would be nice and easy if everyone used the same server, but that is never the case. There are different things you have to do to get a broken PHP mail function working for different servers, so a good test suite for email is what you need. That is what I’ll attempt to provide here. I hope it helps.

Dynamic PHP eMail Testing Tool

screenshot

If you are interested in the professional version of this PHP mail testing tool seen in the above screenshot, contact me, Ian L., by email at linian11@yahoo.com and I’ll give you my professional download version for only $20. Otherwise read on to build your own free version. – Ian L

When I discovered that it was a reoccurring issue to fix peoples email sending ability from various servers, I decided it is time to build a dynamic email tester in PHP. So open up your favorite notepad or editing tool and copy and paste the following code. This is my custom PHP email testing tool I made to test various attributes of the PHP mail function for use with different web servers The tool consists of two very basic PHP files. I’ll call them email_form.php and email_sender.php. Put both files in the directory you want to test and start your trial and error testing until you get an email to go through to the recipient that wasn’t receiving your messages before.

email-form.php

<html>
<head>
<title>Jafty eMail Tester</title>
</head>
<body>
<h1>Jafty eMail Tester</h1>
<p>A simple dynamic testing tool for the PHP mail function. Allows web developers to test sending emails from any server using the PHP mail function with different settings and parameters until they get it working. To start, simply edit the values in the form below and submit to send a test email to the “Send To” address. Good luck!</p>
<?php
//Set timezone:
date_default_timezone_set(‘America/New_York’);

//format date:
$start = getdate();
$smon = $start[‘mon’];
$sday = $start[‘mday’];
$syear = $start[‘year’];
$shr = $start[‘hours’];
$smin = $start[‘minutes’];
$ssec = $start[‘seconds’];
$formatted_date =  “$smon/$sday/$syear”.” “.”$shr:$smin:$ssec”;
echo “Current Date/Time: “.$formatted_date.”<br /><span style=’font-size:10px;color:#343434′>(If above time is not correct, change the setting for date_default_timezone_set in email_form.php)</span><br />”;

//Set default form field values:
$message = “Test eMail from Jafty PHP mail Tester sent at $formatted_date”;
$to = “linian11@yahoo.com”;
$subject = “PHP Mail Test”;
$headers = “From: peter@petermichaelsonjewellery.com.au\r\n”;
$param = “-fpeter@petermichaelsonjewellery.com.au”;
?>

<form id=”emlform” method=”post” action=”email_sender.php”>
<table><tr><td>
Email Header:
</td><td>
<input type=”text” name=”h” style=”width:450px;” value=”<?php echo $headers; ?>” />
</td></tr><tr><td>
Sent To:
</td><td>
<input type=”text” name=”e” style=”width:450px;” value=”<?php echo $to; ?>” /></td></tr><tr><td>
Subject:
</td><td>
<input type=”text” name=”s” style=”width:450px;” value=”<?php echo $subject; ?>” /></td></tr><tr><td>
Message:
</td><td>
<input type=”text” name=”m” style=”width:450px;” value=”<?php echo $message; ?>” /></td></tr><tr><td colspan=”2″>
<input style=”float:right” type=”submit” />
</td></tr></table>
<hr><h3>
Here are email accounts for testing with:</h3>
<table><tr><th>
Email
</th><th>
Password
</th></tr><tr><td>
example@hotmail.com
</td><td>
yourpass
</td></tr><tr><td>
example@gmail.com
</td><td>
yourpassword
</td></tr><tr><td>
some-email@example.com
</td><td>
add pass here
</td></tr><tr><td>
test@gmail.com
</td><td>
passhere
</td></tr></table>

<hr /><p>&copy; Copyright 2014-2024 Jafty Interactive Web Development & <a href=”http://jafty.com”>Jafty.com</a></p>
</body>
</html>

Once you copy and paste the contents of email-form.php, then do the same in a new file for email-sender.php below.

email-sender.php

<?php
$h = $_POST[‘h’];
$e = $_POST[‘e’];
$s = $_POST[‘s’];
$m = $_POST[‘m’];
$f = “-fpeter@petermichaelsonjewellery.com.au”;
//print form values for debugging:
echo “email: $e<br>”;
echo “subj: $s<br>”;
echo “msg: $m<br>”;
echo “header: $h<br>”;
echo “optional: $f<hr>”;
//send mail:
if(mail($e, $s, $m, $h,$f)){//if mail sent according to mail function:
echo “mail sent!<br>”;
}else{//else email failed
echo “ERROR sending email. Details follow if available:<br />”;
print_r(error_get_last());
}//end else email failed.
?>

Summary

That is all there is to the tool, but there are other things to learn about php mail, so check the trusty php.net page for php mail at: http://php.net/manual/en/function.mail.php

Leave a Reply

Your email address will not be published. Required fields are marked *