Category Archives: Programming

troubleshooting obscure php email issues

One of my current web development/programming projects is being built on a server that I have no access to. It’s quite challenging, to say the least! One of my issues, and the hardest to resolve, was email.

I use the ordinary mail() PHP function. No classes, no cgi, just the function with HTML headers. But it seemed that certain emails I sent from the server were going nowhere, and no failure was being reported either. For example, I could receive mail at my “theconnorswebsite.com” domain, but not my “whitewavedesigns.com” domain.

Well, first I learned that there was no formal MX record, and my server followed that protocol. One was set up on this particular server, but still, I could not receive email at my wwd domain.

I then tried to specify the smtp server, port and from address:

ini_set("smtp","mail.website.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","info@website.com");

That didn’t work either. I also made sure that the server would allow “from” emails to be from any domain by substituting various emails at different domains as the “from”. I even double-checked my HTML headers:

MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
From: Website <info@website.com>

Then, one of the programmers who works on this mystery server told me that their MTA (mail transfer agent) was Postfix, and a quirky thing about Postfix on a Linux box was that it would not tolerate the “r” tag when adding additional headers such as “CC” or BCC”. For example, this is how I was writing my headers:

$headers.= "rnCC:".$postformAR['Email'];
$headers.= "rnBCC:".$postformAR['Webmaster'];

(I put the carriage-return/line feed in front of the cc and bcc so it can be optional.) I had heard that Windows servers don’t especially like the “r” carriage-return tag, but a Linux server? Well, okay, I removed it.

Then, as if by miracle, some of the emails were coming through! But still not all of them… I finally sat down today and line-by-line tried to figure out why some of my code was working and some wasn’t. Eventually, I got down to the actual mail() function.

Now, according to php.net, this is the mail() function description:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Note the last “additional_parameters”. Sometimes, I use this last parameter “-finfo@website.com” for failed messages, bouncebacks, etc. I don’t tend to use it when someone submits an email to my site, but I do use it when sending out bulk emails like newsletters. As I found out today, for whatever reason, that last parameter was actually required!

So, my fellow programmers, should you ever have email that is sent from your serve simply vanish into cyber-space, I hope this gives you a few more tricks up your sleeve to resolve the issue.

quick way to print an array in php

Printing arrays in PHP can be maddening if you don’t know the shortcuts! There is always the standard:

foreach ($array as $key => $value)
{
echo $
key.' is key<br>';
echo $value.' is value<br>';
}

but if it’s not an array (and you have error checking on), you’ll get an error:

Warning: Invalid argument supplied for foreach() in /home/username/public_html/file.php on line x

And unfortunately, putting the ‘@’ in front of the ‘foreach’ only yields another error:

Parse error: syntax error, unexpected T_FOREACH in /home/username/public_html/file.php on line x

Of course, you could do a check to see if it *is* an array, but that’s more lines of code, and we want it quick.

Here’s a clever way to get what I call in my Dreamweaver snippets panel a “quick print of array”:

print '<pre>'; print_r($_POST); print '</pre>';

Here’s what it will look like:

Array
(
[asso_key_name_1] => value 1
[asso_key_name_2] => value 2
)

That’s great, but suppose you have several arrays on one page you want to print the results for, how can you keep them apart? Simple! Put the name of the array after the first <pre> tag, like this:

print '<pre>_POST '; print_r($_POST); print '</pre>';

The results will be like this:

_POST Array
(
[asso_key_name_1] => value 1
[asso_key_name_2] => value 2
)

You can use this quick printing of arrays to print POST vars, SESSION vars, recordsets, any kind of PHP array!

php + remove chars from front of string

I had a need the other day to remove a char from the beginning of a string in PHP. Rather than fiddle around with the existing function substr(), I whipped up my own function that will remove X number of characters from the beginning of a string.

function removeXnumCharFromFront($instring,$x)
{
$length = strlen($instring);
for($i=$x; $i<$length; $i++) $temp[] = $instring[$i];
$comma_separated = implode(",", $temp);
$outstring = eregi_replace(",","",$comma_separated);
return $outstring;
}

Enjoy!