LMTP

From Halon, SMTP software for hosting providers
Jump to: navigation, search

The Halon SMTP software supports LMTP delivery via the SetProtocol() function. It enables you to create setups with only Halon and an email storage server, such as Dovecot. Consequently, all filtering, routing, rewriting and forwarding takes place in the Halon.

Dovecot example

Normally, you have an MTA such as Postfix in front of Dovecot that does alias expansion. When delivering messages directly to Dovecot with LMTP, it's important to understand that Dovecot only accepts messages to actual mailboxes, not aliases. Therefore, the alias expansion needs to take place in the Halon. Below is an example of how this can be done, using a REST API that feeds the Halon system that information.

Create an API that returns something like

[
   {
       "email": "john@example.com",
       "server": "192.168.0.100"
   },
   {
       "email": "john.smith@gmail.com",
       "server": "external"
   }
]

as response to an "email alias and forward" query. Then add an include file with a function that perform API calls against the API

Include file
function lookup($recipient) {
    
$result cache ["ttl" => 3600"ttl_override" => ["" => 10], "size"=> 10000]
        
http("http://api.example.com/rcpt/?email=$1", ["timeout" => 10], [$recipient]);
    
$result json_decode($result); // or https://wiki.halon.se/API_calls
    
if (!is_array($result))
        
Defer("temporary error");
    return 
$result;

and use that file/function in the DATA script to create a "copy" for each alias/forward

include "file:1";
foreach (
lookup($recipient) as $rcpt) {
    
SetMetaData(["server" => $rcpt["server"]]);
    
CopyMail($rcpt["email"]);
}
Done(); 

and the following pre-delivery script to make the routing

// Make sure a "lookup-mx" transport is used
$data GetMetaData();
if (
$senderip == "") {
    
// My own bounces
    
SetProtocol("smtp");
} else if (
$data["server"] == "external") {
    
// External forwards, maybe do some SRS as well
    
SetProtocol("smtp");
} else {
    
// Local users
    
SetProtocol("lmtp");
    
SetDestination($data["server"], 24);