Tag Archives: programming - Page 2

More Correct Email from CakePHP

One problem a lot of people have with sending email from PHP in general is properly setting the envelope sender of the message, so that it’s correct in all the headers. This is important because it affects spam filtering rather drastically. Also, some ISPs and other email providers won’t even accept a message if the envelope header is set to an invalid address.

In regular PHP, this is simple to fix. Consider the following example, that sets the from address properly:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    "Test Message", 
    "This is a test message", 

The key here is that last parameter we pass to the function: ‘-fwebmaster@example.com’. That passes a command-line option to sendmail that properly sets the envelope sender. Most people leave that off, because they don’t even realize the need it there.

Okay, so now we know how to do it in raw PHP. How do we do this using CakePHP’s email component? After all, the documentation doesn’t mention anything about that ‘additional parameters’ argument, does it? Or maybe it does. You just have to read the full API docs very carefully. It turns out, if you set the ‘additionalParams’ property of the Email Component, it will pass it on to the mail() function when it calls it. Perfect! So, here’s how to properly initialize the Email Component in CakePHP:

$this->Email->to = 'example@example.com';
$this->Email->subject = 'Test Message';
$this->Email->replyTo = 'webmaster@example.com';
$this->Email->from = 'webmaster@example.com';
$this->Email->additionalParams = '-fwebmaster@example.com';
$this->Email->template = 'test_message';
$this->Email->sendAs = 'both';

And there you have it! The correct way to send email with proper envelope headers from CakePHP. Enjoy the lower spam score that results!

Better Forking for PHP

I recently needed to update a cron job written for CakePHP to make it multi-threaded, so that a job that would take months otherwise could finish in a reasonable amount of time. I originally planned to simply use pcntl_fork() to do this, but upon reading the comments on the function, I quickly realized it would be a major headache to write properly due to database and other socket connections involved in the process.
Read more »

Randomized .signature file

Here’s a quicky. I like to have a random quote in my signature, but most mail clients only read a static file, and trying to fool them with a command pipe socket is tricky, and often has other issues. So I wrote this little bit of magic:

my $fortune = '';
do {
	$fortune = `fortune -e -s -n 480 bofh-excuses calvin chalkboard chucknorris computers cookie definitions discworld dune firefly fortunes hitchhiker homer kids law linux magic mormon paradoxum perl pets riddles science scriptures smac startrek starwars taow work`;
} while ($fortune =~ /filter|these|words|out/);
print "Matthew Walker                      h: (xxx) xxx-xxxx\n";
print "Software Architect                  m: (xxx) xxx-xxxx\n";
print "Marketecture                        e: fake_email\@domain.com\n\n";
print $fortune;

Replace the ‘filter|these|words|out’ bit with a | seperated list of words you don’t want to show up in your fortunes, and adjust the fortune command however you want to tune the type of quotes you get. Then install it as a cron job like this:

* * * * * perl ~/.signature.pl > ~/.signature

There you go! Your .signature file will get regenerated once a minute with a new random quote. Enjoy.

CakePHP Twitter and Bitly Components

I promised more programming related posts, and here’s one for you.

As part of developing TweeterDiet.com, I needed to interface with Twitter and Bit.ly to post tweets and the links back to the public profiles on TweeterDiet. I searched around for a while, and didn’t immediately run across any good components for what I needed, so I wrote my own, and now I’d like to share them.
Read more »

TweeterDiet.com and CakePHP

First things first. As of 11:45 PM last night, my latest project launched. Go take a look at TweeterDiet.com Go ahead. I’ll still be here when you get back.

Yes, I know it’s very minimal right now. I’ll be remedying that over the next week or so, by adding in some sample screen-shots and more features. But for now, it at least functions, and I’ve been using it personally for the last couple weeks. Here’s a basic rundown of the idea for the site. As you may know, if you read my blog regularly, or happened to glance over to the right sidebar, I’m trying to lose a lot of weight. To help me out, I’m going to the gym, but most importantly, I’m tracking my weight. I’m using a method borrowed from The Hacker’s Diet, which is quite simple: Weigh yourself once a day, and calculate a trended weight from that. The trended weight smooths out the daily fluctuations, and gives you a much better picture of what’s actually happening with your body. From that trend, you can calculate your daily calorie deficit, and know if you need to change things to lose weight faster or slower.

Read more »