Tag Archives: tips - 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:

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

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:

<?php
$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:

#!/usr/bin/perl
 
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 »

CakePHP mod_rewrite optimization

CakePHP is great, but the default installation has a performance issue that is very easy to resolve, and should be fixed in any production installation of a CakePHP application. The solution is very simple.

In the default install, there is a file in the webroot called .htaccess. It contains mod_rewrite rules, and you have to set you vhost up to allow overrides. This introduces a significant overhead to each request, as apache has to search for applicable htaccess files and merge their settings into the core apache config every request. On a production application this can become significant, especially if you get a significant amount of traffic.

Read more »