Simple perl based local mail delivery agent to an IMAP server
If you host your mail accounts at IONOS, you are also affected by a recent change (Jan 2024) in their policy of only accepting mails with a sender that matches the login on the mail server. Other mails will be rejected with Sender address is not allowed.
While this policy is perfectly reasonable on its own, it breaks the delivery of mail from fetchmail. The mails fetchmail collects almost never have a sender that matches the new policy. After discussing this back and forth with IONOS support, they could not offer me a simple solution. (One would have been to accept not only a sender matching the domain, but also if the recipient is from the given domain).
Example
- Fetchmail fetches a mail from
someone@somewhere.comtomymail@web.defromimap.web.de - and then tries to forward this mail to
me@mydomain.comusing the local MTA (in this case postfix). - Postfix is configured to deliver mail to the
smtpserver withuser@mydomain.comas login. - But with the new policy, the
smtpserver will reject such a mail withSender address is not allowed.- because
@somewhere.comdoes not match@mydomain.com.
- because
My workaround is now not to use SMTP for forwarding, but to append the mails directly to a users' IMAP folder.
- have Perl installed
- have
libmail-imapclient-perlinstalled
- Copy the
imapmda.plto a folder (e. g./usr/local/bin) - Make it executable for the same user that runs the fetchmail daemon (
fetchmailin my case)
chown fetchmail.root /usr/local/bin/imapmda.pl
chmod 700 /usr/local/bin/imapmda.pl
- Create a
/etc/imapmda/localuserfile (directory configured inimapmda.pl) for each userlocaluseryou want to deliver mails to. - These files have the following content:
{
server => 'your.imap.server.de',
user => 'username-on-imap-server',
password => 'password-on-imap-server',
folder => 'INBOX',
}
- Make
/etc/imapmda/and the files in it readable only by the user running the fetchmail daemon (fetchmailin my case).
chown fetchmail.root /etc/imapmda/ /etc/imapmda/*
chmod 700 /etc/imapmda/
chmod 600 /etc/imapmda/*
- Add
mda "/usr/local/bin/imapmda.pl %T"to your/etc/fetchmailrc. - Replace the local mail addresses with the
localusernames
After this, my /etc/fetchmailrc basically looks like this
defaults
fetchall
ssl
mda "/usr/local/bin/imapmda.pl %T"
poll imap.web.de protocol IMAP:
user "mymail@web.de" with password "my-password-at-web-de" is localuser here
- Fetchmail calls the script with the
localuseras a command line parameter (%Tin themdaconfiguration) - and passes the downloaded mail to
STDIN. imapmda.plreads the configuration file,- joins the lines of the mail into a single
$mailvariable, - connects to the IMAP server,
- and appends the mail to the configured folder.
- When done, it disconnects from the server.
- The script will fail with an error if the mail could not be delivered.
- This should stop fetchmail from flushing the mail from the source.
- Fetchmail will log the (error) output of the script to its usual logging target (
/var/log/mail.login my case). - Look there first to see what went wrong.
- Obviously this only works if you know the user's IMAP credentials.
- However, in a fetchmail scenario you already know all the credentials of the source accounts.
- So knowing the credentials of the target account should not make much difference.
Hope this still helps someone!