Code to process 3Jam Messages for WordPress Blog
March 24, 2010 Filed in: Web Technology
In this post, I talked about my method for using a SMS number to receive text messages that then were automatically forwarded to a wordpress blog. Rather than have the text messages go directly to the blog, I wrote some scripts on my mail server to process the 3Jam messages and prettify them.
Here is what I did.
First, I used a .qmail script to forward the emails coming from 3Jam to a PHP script:
Where foobar is the target of the email to which 3Jam sent my text messages (i.e., [email protected]). (And, no, this isn’t the real email address I used ... spammers - know that this email address is directly discarded by my email server!)
The PHP script looks like this:
This is not a very robust implementation (note the lack of error checking, and, I suspect to your horror, comments). I’m not even sure if the script will work correctly if called more than once at the same time. (I think that the worst that could happen is that simultaneous log entries could be mangled together, but I didn’t research this enough to verify that.) The script is designed to handle email messages from both 3Jam and users who email the master email address ([email protected]) directly. It minimally handles rich text (e.g., HTML) emails (that’s the purpose of keeping track of $contentType).
Everything that happens is captured in a log file (see the tee command in the .qmail script).
[email protected] is where you would put the special email address that you can have the wordpress blog assign to you for emailing in blog entries.
I hope that this is useful to someone.
Here is what I did.
First, I used a .qmail script to forward the emails coming from 3Jam to a PHP script:
% cat .qmail-henryflurry:com-foobar |/usr/www/users/XXXXX/cgi-bin/php4.cgi -q \ /usr/www/users/XXXXX/support/cli/index.php | /usr/bin/tee >> ~/email_test.txt %
Where foobar is the target of the email to which 3Jam sent my text messages (i.e., [email protected]). (And, no, this isn’t the real email address I used ... spammers - know that this email address is directly discarded by my email server!)
The PHP script looks like this:
<?php $_to = "[email protected]"; echo "=============================\n"; // read from stdin $fd = fopen("php://stdin", "r"); $email = ""; while (!feof($fd)) { $line = fread($fd, 1024); echo $line; $email .= $line; } fclose($fd); // handle email $lines = explode("\n", $email); // empty vars $from = ""; $subject = ""; $headers = ""; $contentType = ""; $message = ""; $splittingheaders = true; $skipRestMessage = false; $inside3jam = false; $foundFirstJamLine = false; $preMessage = ''; for ($i=0; $i < count($lines); $i++) { if ($splittingheaders) { // this is a header $headers .= $lines[$i]."\n"; // look out for special headers if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { $subject = $matches[1]; } if (preg_match("/^Content-Type: (.*)/", $lines[$i], $matches)) { $contentType = $lines[$i]; } if (preg_match("/^From: (.*)/", $lines[$i], $matches)) { $from = $matches[1]; $elements = explode(" ", $from); if (strcmp($elements[0], "3jam") == 0) { $inside3jam = true; } } } else if (false == $skipRestMessage) { if ($inside3jam) { if (0 == strcmp(trim($lines[$i]), "3jam.com")) { $skipRestMessage = true; } else { if (!$foundFirstJamLine) { $from = $lines[$i]; $foundFirstJamLine = true; } else { $message .= $lines[$i]."\n"; } } } else { // not a header, but message $message .= $lines[$i]."\n"; } } if (trim($lines[$i])=="") { // empty line, header section has ended $splittingheaders = false; } } if (!$inside3jam) { $preMessage .= $subject . "\n"; } echo "=============================\n"; echo "Subject -> $subject\n"; echo "From -> $from\n"; echo "Message BEGIN\n"; echo $message; echo "Message END\n"; // Compose a mail message of subject = from, from = from, msg = pre + msg. if (strlen($contentType)) { $rv = mail ( $_to , $from , $preMessage . $message, $contentType ); } else { $rv = mail ( $_to , $from , $preMessage . $message ); } if ($rv) { echo "Success in sending email\n"; } else { echo "Failed to send email\n"; } ?>
This is not a very robust implementation (note the lack of error checking, and, I suspect to your horror, comments). I’m not even sure if the script will work correctly if called more than once at the same time. (I think that the worst that could happen is that simultaneous log entries could be mangled together, but I didn’t research this enough to verify that.) The script is designed to handle email messages from both 3Jam and users who email the master email address ([email protected]) directly. It minimally handles rich text (e.g., HTML) emails (that’s the purpose of keeping track of $contentType).
Everything that happens is captured in a log file (see the tee command in the .qmail script).
[email protected] is where you would put the special email address that you can have the wordpress blog assign to you for emailing in blog entries.
I hope that this is useful to someone.