1

I have a Postfix server and using it with Outlook (POP/SMTP).

I have different sender addresses for an account. Now I want to configure a relay for a specific sender address. For example I have postfix for my main domain domain1.com. Now I have a sender address that is domain2.com.

For that sender address I want that postfix to relay mail to a second SMTP server. I think that should be possible without problems.

But the problem is that I have to create a SSH tunnel to the second server to connect to the SMTP server. But I cant use a persistent SSH connection so I want to know if it is possible that postfix executes some script before relaying the mail to the second SMTP server.

I already found some information for executing a script when incomming emails but I only want to establish the SSH tunnel before postfix relays the mail to the second SMTP server.

2

Exactly how you implement it is up to you, but you can create your own transport, and that transport can be a script, or daemon, etc.

If you could keep the tunnel up, it would be a breeze, you'd just set an

domain2.com smtp:127.0.0.1:2000 (or whatever port you assigned locally)

transport up.

2

Postfix doesn't support script hooking upon delivery. In order to workaround this you can setup a crontab to execute the a special script. This script will:

  1. Setting up SSH tunnel
  2. Flush queue.

First, define sender_dependent_relayhost_maps with the right-side value is your tunnel port (for example port 2525).

#/etc/main.cf
sender_dependent_relayhost_maps = hash:/etc/postfix/sshtunnel

#/etc/postfix/sshtunnel
domain2.com smtp:[127.0.0.1]:2525

When the tunnel wasn't connected, postfix will temporary defer the email because of Connection refused-error. So you have no worries about losing an email.

Then setup crontab to execute above script. Here the pseudoscript to achieve those goals. Little modification from this original script.

#!/bin/sh

#setup ssh tunnel. modify the parameter for your needs
ssh -L 2525:localhost:25 user@remote.example.com

# Start mail deliveries.
/usr/sbin/sendmail -q

# Allow deliveries to start.
sleep 10

# Loop until all messages have been tried at least once.
while mailq | grep '^[^ ]*\*' | grep domain2.com >/dev/null
do  
    sleep 10
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.