Technical Gibberish (and an SMTP Function for PHP)

For the past few years, the bread and butter of my web development career—presuming that you could call it a career anyway—has been PHP coding. I know a lot of so-called “real programmers” will boo and hiss and whatever, but it’s served me well enough. When I made my initial exodus from ASP in 2000, PHP was a breath of fresh air. I preferred the syntax to that of ASP (and I was one of those “mavericks” coding it in JScript and not VBScript because I hace class) plus it was free and ran on my home grown Linux server. Those were good days.

In the past eight years, I’ve done more programming and gotten better at it. Some of my projects have been larger in scale and PHP becomes much less fun to work with. I’ve been working with Python in my free time and really like the Django. In fact, if any of you out there are fledgling programmers and are looking for a place to start, I can’t help but recommend Learning Python by Mark Lutz. Python has a lot of good free tools, runs on many platforms and this book is very, very good in my opinion—not just as a primer on Python itself, but on beginning programming in general. I even picked up a few things from it. (And, if you’re a Monty Python buff, you get a bunch of extra bonuses.) I much preferred to Programming Ruby by Dave Thomas.

(As a side note, that’s not interesting to anyone, I happen to love Ruby. Right now it’s my favorite language to dabble in, but Rails leaves a lot to be desired—particularly in terms of performance and system requirements. Also, Ruby feels like its in the midst of a large mutation from obscure to not-so-obscure whereas Python seems more stabilized. I still like Ruby better, syntactically in particular, but Python has a real world edge and I’ve been very happy with Django. So there. Now you know.)

Was all of this necessary? No. However, that’s what you get for reading a personal blog. Serves you right. Anyway, I recently migrated to Webfaction as my new host from a locally collocated dedicated server. Migrations are always trying since different people use different configurations and such. (Before I go on, let me say I’ve very satisfied with Webfaction thus far.) The biggest bite I found with Webfaction is that you cannot use the builtin mail() function from PHP for sending mail. This causes problems everywhere from Wordpress (which has a plugin to get around it) to custom applications.

The reason you can’t use mail() is the reason I went into this whole history about my recent programming habits and explains why I’m using PHP less and less. The function assumes you have local access to sendmail, and there’s simply no good way to configure it otherwise. Lame. Plain lame. I’ve always had local access, so this is the first time I’ve run into this problem, but if you’re writing a program for general application (*ahem* Wordpress guys, I’m looking at you) you should avoid using mail() like the plague.

So, what should you do? First, in one way or another, you’ll be using Pear::Mail to use SMTP instead of sendmail. It works well. However, if you’re lazy like me, you’ll probably want a drop-in replacement or or something pretty close. What I have is something pretty close. Here’s the code for all you junkies out there (I’ve also attached it as a file with comments, so you might want to check that out if you’re lost):

require_once "Mail.php";

function smtp($to, $subject, $message, $additional_headers = "") {
	$smtp_server = "smtp.example.com";
	$smtp_username = "username";
	$smtp_password = "password";
	$smtp_default_from = "default@example.com";

	$to = (string) $to;
	$subject = (string) $subject;
	$message = (string) $message;
	$additional_headers = (string) $additional_headers;

	$raw_headers = str_replace("\r", "", $raw_headers);
	$raw_headers = explode("\n", $additional_headers);
	$headers = Array("To" => $to, "Subject" => $subject);
	$recipients = $to;

	foreach($raw_headers as $raw_header) {
		$header = explode(":", $raw_header, 2);

		if (count($header) != 2)
			continue;

		$header_key = ucfirst(trim($header[0]));
		$header_value = trim($header[1]);

		if ($header_key == "To" || $header_key == "Subject")
			continue;

		if($header_key == "Cc" || $header_key == "Bcc")
			$recipients .= ", " . $header_value;

		$headers[$header_key] = $header_value;
	}

	if (!array_key_exists("From", $headers))
		$headers["From"] = $smtp_default_from;

	$smtp = Mail::factory("smtp",
		Array("host" => $smtp_server,
			"auth" => true,
			"username" => $smtp_username,
			"password" => $smtp_password
			)
		);

	$result = $smtp->send($recipients, $headers, $message);

	if (PEAR::IsError($result)) {
		// echo $result->getMessage();
		return false;
	} else {
		return true;
	}
}

I didn’t write all of this. I modified it but I really don’t know who the original author is as it was never cited. I did, however, add the Cc and Bcc functionality that the original version I used was lacking. So much for being lazy! Anyway, if this helps you out, enjoy.

Source code with comments.

2 Responses to “Technical Gibberish (and an SMTP Function for PHP)”

  1. Frodo Says:

    While I can understand (and agree with) the view that WP should allow you to pick your mailing method, it seems that their design philosophy is to do things one way really, really well and let plugins pick up the slack. I think it provides for a lot of design flexibility, but it often leaves WP feeling like it’s somewhat unfinished right out of the box.

    There’s also the issue with reliance on third-party code that isn’t always updated for the latest release of WP. I’m stuck with using WP 2.3.3 at the maximum because the author of our image gallery plugin has decided not to update it for 2.5.x compatibility. Could I crack it open and try and figure out why it doesn’t work? Sure, but that’s a big hassle. Plugin-breaking is one of my biggest complaints with the WP platform (not to mention cumbersome upgrades).

  2. K.L. Says:

    You’re right, Numberless, this posting is purely for techies. But for the record, you’re still a weirdo, and that trait IS mutually exclusive between us.

Leave a Reply