How can I improve this email script?

Bruce Gust
Bruce Gust used Ask the Experts™
I'm running a contest where a user has to validate their email address. While the code works fine when I test it and the vast majority of voters have been able to successfully validate their email address, I get a number of folks who insist that the email never arrived and I have seen on, for example,, some inconsistencies as far as the timeliness of the delivery.

All that to say, that I'm confident there's room for improvement, but it's exasperating when I can't duplicate the problem and / or have no idea why it will work for some, but not for others.

My code is below. Any suggestions are welcome and I do appreciate it.

$key = "qP9wXOx+Dk0iVCmUQDEkLCf5";
$str= $voter_email.''.$key;
$digest = sha1($str, true);
$the_digest =  base64_encode($digest);

$the_first_name = stripslashes($_POST['voter_first_name']);
$to = $voter_email;
$subject = "voter_validation";
$headers = 'MIME-Version: 1.0'. PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1'. PHP_EOL;
$headers .= 'FROM:'. PHP_EOL;
$subject = "voter validation";
$message = "&nbsp;<BR>&nbsp;<BR>Hello, $the_first_name !<P>To validate your email and to ensure that your votes count, click on the link below.";
$message .= "<P>$voter_email&chk=$the_digest <P>If you have any questions, you can contact you Bruce Gust, the Showdown webmaster by emailing him at<P>Thanks!";
mail($to, $subject , $message, $headers);
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Sorry, but the simple fact of the matter is that there is no accountability in email.  There is no innate architecture to distinguish legitimate email from spam.  There is no standard for timely delivery.  There is no legal penalty for failing to deliver email.  There are bizarre and unaccountable "blacklists" that get things wrong all the time. There is competition between ISP companies, and I have actually seen AOL mark all emails from Verizon as spam.  There are a variety of spam filters, many of which are technically incompetent.  And your hosting provider may be adding anti-span headers to the messages, which some email reader programs automatically assume to be spam.

I've used the design pattern in this article for email confirmation.  It works most of the time ,and it looks like you're doing the same thing.  If you're getting 90% success with email, you've got a winning formula.  Be happy.  There will never be a perfect solution.
Bruce GustPHP Developer


Hey, Ray! Thanks for weighing in. I kind of figured I was at least on the right path, it's just that with those dynamics that I've interacted with like downloadable software etc., I've never noticed a discrepancy. The email always arrived with the proper links so that's why I was thinking that there was more to this. I'll look at the article you referenced and we'll go from there.

Most Valuable Expert 2011
Top Expert 2016

Thanks for the points and best of luck with it.  It's a thorny mess sometimes!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial