dynamically prepend a header in received emails

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

dynamically prepend a header in received emails

André Rodier
Hello all,

Is there a way to prepend a header in the received emails, according to
the FROM and TO email address, with a script?

I have a list of email addresses in a text file, which has to be
different for each user.

Is there an option, for instance in header_checks, to use something
like pcre:/etc/postfix/$recipient/headers_check.cf

Or maybe an SQLite database would be enough. With a custom SQL query, I
should be able to return the header to add, but how?

Otherwise, I will start writing a custom milter.

Thanks for your help.
André

Reply | Threaded
Open this post in threaded view
|

Re: dynamically prepend a header in received emails

Wietse Venema
Andr? Rodier:
> Hello all,
>
> Is there a way to prepend a header in the received emails, according to
> the FROM and TO email address, with a script?

You might be able to do that with http://www.postfwd.org/.
IIRC this is implemented in Perl, therefore easy to extend.

> I have a list of email addresses in a text file, which has to be
> different for each user.

In the case that postfwd cannot do this, your options are

- Custom policy server.

- Custom Milter.

> Is there an option, for instance in header_checks, to use something
> like pcre:/etc/postfix/$recipient/headers_check.cf

Each Postfix daemon opens tables before it handles requests. I would
not allow random strangers to control the pathname of files that
Postfix will open.

> Or maybe an SQLite database would be enough. With a custom SQL query, I
> should be able to return the header to add, but how?

> Otherwise, I will start writing a custom milter.

There are many options for doing that.

        Wietse
Reply | Threaded
Open this post in threaded view
|

Re: dynamically prepend a header in received emails

André Rodier
Thanks, Wietse.

I will have a look and I will post to the list with the solution.

Best regards,
André

On Sat, 2019-08-03 at 17:52 -0400, Wietse Venema wrote:

> Andr? Rodier:
> > Hello all,
> >
> > Is there a way to prepend a header in the received emails,
> > according to
> > the FROM and TO email address, with a script?
>
> You might be able to do that with http://www.postfwd.org/.
> IIRC this is implemented in Perl, therefore easy to extend.
>
> > I have a list of email addresses in a text file, which has to be
> > different for each user.
>
> In the case that postfwd cannot do this, your options are
>
> - Custom policy server.
>
> - Custom Milter.
>
> > Is there an option, for instance in header_checks, to use something
> > like pcre:/etc/postfix/$recipient/headers_check.cf
>
> Each Postfix daemon opens tables before it handles requests. I would
> not allow random strangers to control the pathname of files that
> Postfix will open.
>
> > Or maybe an SQLite database would be enough. With a custom SQL
> > query, I
> > should be able to return the header to add, but how?
> > Otherwise, I will start writing a custom milter.
>
> There are many options for doing that.
>
> Wietse

Reply | Threaded
Open this post in threaded view
|

Re: dynamically prepend a header in received emails

André Rodier
On Sun, 2019-08-04 at 07:14 +0100, André Rodier wrote:

> Thanks, Wietse.
>
> I will have a look and I will post to the list with the solution.
>
> Best regards,
> André
>
> On Sat, 2019-08-03 at 17:52 -0400, Wietse Venema wrote:
> > Andr? Rodier:
> > > Hello all,
> > >
> > > Is there a way to prepend a header in the received emails,
> > > according to
> > > the FROM and TO email address, with a script?
> >
> > You might be able to do that with http://www.postfwd.org/.
> > IIRC this is implemented in Perl, therefore easy to extend.
> >
> > > I have a list of email addresses in a text file, which has to be
> > > different for each user.
> >
> > In the case that postfwd cannot do this, your options are
> >
> > - Custom policy server.
> >
> > - Custom Milter.
> >
> > > Is there an option, for instance in header_checks, to use
> > > something
> > > like pcre:/etc/postfix/$recipient/headers_check.cf
> >
> > Each Postfix daemon opens tables before it handles requests. I
> > would
> > not allow random strangers to control the pathname of files that
> > Postfix will open.
> >
> > > Or maybe an SQLite database would be enough. With a custom SQL
> > > query, I
> > > should be able to return the header to add, but how?
> > > Otherwise, I will start writing a custom milter.
> >
> > There are many options for doing that.
> >
> > Wietse

As promised, I have written a simple milter in Python, that does
exactly what I wanted. I post it here, in case people want to use it.

This is a very simple milter, written in Python, that do the
following when a new message arrive.

1. Get the recipient uid "UID" from the email address.
2. Get the list of address books in the database for the user with uid
"UID"
3. Search the sender email address in all the user's address books
4. If found at least one, add one header "X-AddressBook" which contains
a list of all the address books found, with a prefixed syntax:

X-AddressBook: "SOGo:Personnal, SOGo:Professional"

There are some limitations, of course, but it is still functional. This
allows a lot of things, for instance with Sieve filters.

I am not a Python expert, and perhaps there are some errors in the
code, but it should be readable. I had to disable a few pylint
warnings, as I could not solve them at all, especially those related to
the inherited class.

https://github.com/progmaticltd/sogo-milters/blob/master/milter-abook/milter-abook.py

Enjoy!

--
André Rodier