Score:2

Postfix: Bounce incoming email, but still deliver

cn flag

I run my own mailserver but I am getting "spammed" by a certain individual. In order to try to discourage this person from sending me any more emails, I would like to bounce the incoming email (based on his email address). But I still want the email to be delivered because it might serve as "evidence" for online harassment.

My thought was to have the incoming email send to two accounts (in virt-user-table). One which accepts the email, the other that bounces it by sending an email back that delivery failed.

Can this be done? And how would I go about setting this up in Postfix?

Thank you in advance.

jp flag
This is far from a recommended configuration. To prevent *backscatter*, SMTP servers should *connection-stage reject* instead of sending bounces.
Zippy1970 avatar
cn flag
Yes I understand that. But I still need the incoming mail as "proof" of digital harassment. I don't want to actually block the incoming mail. I know how to do *that*. I want to try to discourage this person from sending me mail. Trust me, this person is far from being technically inclined. If he gets a bounce email, he for sure will think the email has never arrived.
Score:1
za flag

Basically you don't want mail to be bounced here, but for some senders your server should still generate additional message.

You can write a script that can build a message that looks like a bounce (e.g. creating a "fake bounce"), and arrange things so that messages from this sender trigger this script, in addition to normal delivery.

One way to do that is to set always_bcc to some alias, and the target of that alias should be the script path prefixed with the pipe, so the script will be run and fed with the message to parse. The message will appear at stdin. The script could then parse the message, check the sender address and either exit succesfully doing nothing or create a fake bounce. Since this is just an additional receiver of the message, it won't change the standard mail reception path; the mail still be delivered to whoever it was originally destined too. But make sure the script always exists successfully (e.g. never fails with any non-zero error code), else the sender will have real bounce, quite cryptic one.

In /etc/aliases add:

bcc_script: |/usr/local/bin/bcc_script.py

(don't forget to run newaliases after editing this file).

In /etc/postfix/main.cf add:

always_bcc = bcc_script

The /usr/local/bin/bcc_script.py will begin similar to this:

#!/bin/env python3

import sys, email

try:
    msg = email.message_from_bytes(sys.stdin.read()) 

    if msg['From'] != '[email protected]':
        sys.exit(0)

    # creating of the fake bounce here
    ...

except Exception:
    pass
    # do nothing, fail silently to avoid bounces if the code above throws runtime error

Note that I didn't tested this code and it might not work right away. You can find other examples on the Internet.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.