Sending server-side emails from FileMaker via PHP
Some context -- my new gig features a big 'ol FileMaker installation, which has a number of automated maintenance routines. Some of those routines send emails, through a convoluted process involving FileMaker calling a GUI MUA (e.g. Mail.app). There are a number of practical problems with this, such as the requirement of another computer running, always logged into an account capable of sending the emails.
We'd done a bit of research about strictly server-side alternatives, and found surprisingly little. The best resource was Graham Sprague's page about sending emails via FileMaker's XSLT Web Publishing tool. We gave that approach a try, but didn't get any results, or anything useful from FileMaker's logs to explain why things weren't working. I'm not sure what FileMaker version Graham's example was written for, perhaps something's changed with version 9?
Rather than dive into FileMaker's proprietary XSLT system to debug things, it occurred to me that this might be a job for FileMaker's PHP API. Sure enough, after about 15 minutes of consulting the API Doc, we were sending emails based on the contents of a FileMaker record.
We're still working on ironing out the details, but here's the rough proof of concept PHP file. It works with the example email database from Graham's XSLT sample, with the php permission added to the database. Plenty of missing features such as cc & bcc fields, actually checking for the 'send' flag, checking for errors, any kind of authorization or authentication, etc. In other words, you probably don't want to be running this on a publicly accessible webserver, but at least it presents the basic idea in a simple form.
<?php
require_once('FileMaker.php');
$fm = new FileMaker();
$fm->setProperty('username', 'send_email');
$fm->setProperty('password', 'whatever_the_password_is');
$fm->setProperty('database', 'Email');
$findCmd =& $fm->newFindAllCommand('Utility_Email');
$result = $findCmd->execute();
$records = $result->getRecords();
foreach($records as $record) {
$headers = array("From: " . $record->getField('from'));
mail($record->getField('to'),
$record->getField('subject'),
$record->getField('message'),
implode("\r\n", $headers)
);
}
echo "Emails sent";
June 12th, 2008 at 10:59 pm
I know this is a dumb question, but how do you call the php page to send the e-mail? Do you use an Open URL script step or something? Let me know. info@applicationarch.com
June 13th, 2008 at 9:01 am
Actually, that's one of the many details we haven't worked out for real, but using Open URL was one thought. Curl and launchd would be another option.
For development, I've just been hitting the URL with my browser.
June 13th, 2008 at 9:08 am
Having said that, it occurs to me that a launchd-triggered script should probably be run as CLI PHP, leaving the webserver & its attendant issues out of the loop altogether.