postfix | Eric's Drupal Blog

Content tagged with: postfix

Eric's picture

Configuring a server to parse email via a PHP script

In this tutorial I'll show how you can setup a server to parse email with a PHP script. This tutorial assumes that your server is configured to receive email (I wrote this using a virtual machine running postfix).

The first thing you'll need to do is configure an alias to direct email to a PHP script (instead of an email box). I added the following entry to the bottom of my /etc/aliases file and then ran the "newaliases" command to refresh my aliases database:

phpscript: "|php -q /usr/local/bin/email.php"

The above entry will pipe email sent to phpscript@MYDOMAIN to the designated PHP script.

And here's the script:

#!/usr/bin/php
<?php

// fetch data from stdin
$data = file_get_contents("php://stdin");

// extract the body
// NOTE: a properly formatted email's first empty line defines the separation between the headers and the message body
list($data, $body) = explode("\n\n", $data, 2);

// explode on new line
$data = explode("\n", $data);

// define a variable map of known headers
$patterns = array(
 
'Return-Path',
 
'X-Original-To',
 
'Delivered-To',
 
'Received',
 
'To',
 
'Message-Id',
 
'Date',
 
'From',
 
'Subject',
);

// define a variable to hold parsed headers
$headers = array();

// loop through data
foreach ($data as $data_line) {

 
// for each line, assume a match does not exist yet
 
$pattern_match_exists = false;

 
// check for lines that start with white space
  // NOTE: if a line starts with a white space, it signifies a continuation of the previous header
 
if ((substr($data_line,0,1)==' ' || substr($data_line,0,1)=="\t") && $last_match) {

   
// append to last header
   
$headers[$last_match][] = $data_line;
    continue;

  }

 
// loop through patterns
 
foreach ($patterns as $key => $pattern) {

   
// create preg regex
   
$preg_pattern = '/^' . $pattern .': (.*)$/';

   
// execute preg
   
preg_match($preg_pattern, $data_line, $matches);

   
// check if preg matches exist
   
if (count($matches)) {

     
$headers[$pattern][] = $matches[1];
     
$pattern_match_exists = true;
     
$last_match = $pattern;

    }

  }

 
// check if a pattern did not match for this line
 
if (!$pattern_match_exists) {
   
$headers['UNMATCHED'][] = $data_line;
  }

}

?>

At this point in the code, the body of the message will be contained in the $body variable and the headers will be in $headers.

Here is an example of the parsed headers (using print_r()):

Array
(
    [UNMATCHED] => Array
        (
            [0] => From root@Eric-Centos.localdomain  Sun Jan 10 21:49:50 2010
        )

    [Return-Path] => Array
        (
            [0] => <root@Eric-Centos.localdomain>
        )

    [X-Original-To] => Array
        (
            [0] => phpscript
        )

    [Delivered-To] => Array
        (
            [0] => phpscript@Eric-Centos.localdomain
        )

    [Received] => Array
        (
            [0] => by Eric-Centos.localdomain (Postfix, from userid 0)
            [1] => id 4D03F30131; Sun, 10 Jan 2010 21:49:50 -0500 (EST)
        )

    [To] => Array
        (
            [0] => phpscript@Eric-Centos.localdomain
        )

    [Subject] => Array
        (
            [0] => This is the subject
        )

    [Message-Id] => Array
        (
            [0] => <20100111024950.4D03F30131@Eric-Centos.localdomain>
        )

    [Date] => Array
        (
            [0] => Sun, 10 Jan 2010 21:49:50 -0500 (EST)
        )

    [From] => Array
        (
            [0] => root@Eric-Centos.localdomain (root)
        )

)

Now, you have all the email headers and message body parsed. You can do whatever your heart desires with the data, like insert it into a database or even create nodes!

gradient spacer
Eric's picture

Configure your development server to deliver all mail locally

Back in September 2008, I wrote an article on how to configure your virtual machine to deliver email locally (using postfix, cyrus, imap, and sasl). I've had to revisit this article recently to test some bulk emailing functionality. I wanted to change my email server configuration to deliver all email locally to ensure clients and coworkers do not receive test emails. After reading a bunch of web articles, I decided to use Postfix's transport functionality (located /etc/postfix/transport). This configuration file allows you to map email addresses and hostnames to message delivery transports.

I edited this file (/etc/postfix/transport) and added the following to the end of the file:

* discard:

I then edited my postfix configuration file (/etc/postfix/main.cf) and added the following:

transport_maps = hash:/etc/postfix/transport
always_bcc = eric

Reload the transport and restart postfix using the following commands:

postmap /etc/postfix/transport
/etc/init.d/postfix restart

The first configuration change discards all outgoing email, and the second automatically BCC's my user. Although this is a drastic configuration change, it does exactly what I want: it ensures that email will never be delivered to real world addresses, and any email sent from my development server will end up in my local inbox.

You may have to tweak these settings to find a configuration that works with your development situation. For instance, if you wanted to continue delivery to a certain domain, you could add the following transport:

your.domain :

gradient spacer
Eric's picture

Configure your virtual machine to deliver email locally (using postfix, cyrus, imap, and sasl)

I do almost all of my development on Centos virtual machines. This allows me to keep my filesystem, databases, and server configurations contained in a separate environment without harming my host operating system (OSX). This morning, I encountered a problem with my email not being delivered properly. I'm assuming this happened because my virtual machine is not a full qualified email server. I figured the easiest solution would be to setup my virtual machine as an IMAP server instead of trying to fight with DNS, static IPs, spam filters, etc. When it comes down to it, all I need is for my emails sent from my web applications to end up in my email client inbox. Here's what I did to get this to work...

1. Ditch sendmail and install postfix. Postfix is much easier to configure; Sendmail is jibberish.

yum remove sendmail
yum install postfix
chkconfig --level 2345 postfix on

2. Install cyrus, my preferred IMAP service. NOTE: SASL will be the authentication method service

yum install cyrus-imapd cyrus-sasl
chkconfig --level 2345 cyrus-imapd on
chkconfig --level 2345 saslauthd on

3. Configure Postfix service

# edit /etc/postfix/main.cf and change this line:
mailbox_transport = cyrus

# most importantly, I added a few domains (work, personal, etc) to the mydestination variable to ensure that mail sent from my server will be delivered locally, instead of leaving my virtual machine. Make sure you add all the corresponding aliases in /etc/aliases. For instance: If you want to receive email sent to wee@blah.com, you'll have to add blah.com to the list of domains in mydestination and add an alias for wee to redirect to your email account name.

4. Configure Cyrus

# set sasl passwords
saslpasswd2 cyrus
saslpasswd2 eric

# Use Cyrus admin tool to configure mailbox
cyradm --user cyrus --server localhost
> createmailbox user.eric
> setaclmailbox user.eric eric all
> quit

5. Add a new IMAP account in your favorite email client. For my setup, I used the IP address of my virtual machine as the incoming mail server address. Now, when mail is delivered from my virtual machines, it is delivered locally and my email client is used to fetch the new messages!

6. To ensure your setup is working properly, you can send a quick email from the command line (on your virtual machine)

date | mail eric@somedomain.com

gradient spacer Syndicate content