Solving ‘From’ Address Issues in Email Forwarding with DirectAdmin and EximDirectAdmin TroubleshootingSolving ‘From’ Address Issues in Email Forwarding with DirectAdmin and Exim

Solving ‘From’ Address Issues in Email Forwarding with DirectAdmin and Exim

Managing email servers in a DirectAdmin environment can be challenging, especially when dealing with email forwarding and external SMTP providers. A common issue is maintaining the correct ‘From’ address in forwarded emails, which is crucial for compatibility with popular SMTP services like Postmark, SendGrid, Mailgun, and Amazon SES. This article will guide you through solving this problem specifically for DirectAdmin environments using Exim as the Mail Transfer Agent (MTA).

The Problem

When emails are forwarded in a DirectAdmin setup, the original ‘From’ address often changes to the forwarder’s email address. This can cause issues with SMTP providers that have strict rules about allowed sender addresses. For instance, if [email protected] forwards their email to [email protected], the ‘From’ address might change, potentially leading to rejection by SMTP providers like Postmark.

The Solution

We’ll modify the Exim configuration in DirectAdmin to preserve the original recipient address as the ‘From’ address while still forwarding the email. This solution ensures compatibility with external SMTP providers while maintaining proper email forwarding functionality. We’ll also ensure these custom configurations persist across updates using DirectAdmin’s CustomBuild hooks.

How to Configure Exim in DirectAdmin to Preserve ‘From’ Addresses in Forwarded Emails

1. Access Your DirectAdmin Server

Log in to your DirectAdmin server via SSH.

2. Locate the Correct Exim Configuration File

In DirectAdmin, the file we need to modify is:

/etc/exim.routers.pre.conf

This file is specifically used for custom router configurations in DirectAdmin’s Exim setup.

3. Add the New Router Configuration

Open the file with a text editor:

nano /etc/exim.routers.pre.conf

Add the following router configuration:

forward_preserve_from:
driver = redirect
domains = +local_domains
data = ${perl{find_forward}{$local_part}{$domain}}
headers_add = "X-Original-From: $h_from:"
headers_add = "Reply-To: $h_from:"
headers_remove = From
headers_add = "From: $local_part@$domain"
condition = ${if exists{/etc/virtual/$domain/aliases}{yes}{no}}
allow_fail
allow_defer
pipe_transport = address_pipe
file_transport = address_file
reply_transport = address_reply
no_verify
no_expn

4. Create and Configure Custom Perl Script

  1. Create a custom Perl script that contains the find_forward subroutine:
nano /etc/exim_custom.pl

Add the following content:

#!/usr/bin/perl

sub find_forward {
my ($local_part, $domain) = @_;
my $aliases_file = "/etc/virtual/$domain/aliases";
if (open(my $fh, '<', $aliases_file)) {
while (my $line = <$fh>) {
chomp $line;
if ($line =~ /^\Q$local_part\E:/) {
my (undef, $target) = split(':', $line, 2);
close $fh;
return $target;
}
}
close $fh;
}
return undef;
}

1; # Ensure the script returns true
  1. Create a custom loader to include the above script in Exim’s main Perl script:
nano /etc/exim/load_custom.pl

Add the following content:

#!/usr/bin/perl

if (-e "/etc/exim_custom.pl") {
do "/etc/exim_custom.pl";
}

1; # Ensure the script returns true

5. Create a CustomBuild Hook to Ensure Persistence

Create a post-update hook script to patch exim.pl:

mkdir -p /usr/local/directadmin/custombuild/custom/hooks/exim_conf/post/
nano /usr/local/directadmin/custombuild/custom/hooks/exim_conf/post/patch_exim_conf.sh

Add the following content:

#!/bin/bash
#----------------------------------------------------------------------
# Description: A script to patch exim.pl to load /etc/exim_custom.pl
#----------------------------------------------------------------------

echo "Patching /etc/exim.pl to load /etc/exim_custom.pl"

# Add the custom loader if it is not present
if ! grep -q "/etc/exim/load_custom.pl" /etc/exim.pl; then
echo -e "\n# Load custom subroutines\nif (-e \"/etc/exim/load_custom.pl\") {\n do \"/etc/exim/load_custom.pl\";\n}\n" >> /etc/exim.pl
fi

# Ensure /etc/exim/load_custom.pl exists and is executable
if [ ! -f /etc/exim/load_custom.pl ]; then
cat << 'EOF' > /etc/exim/load_custom.pl
#!/usr/bin/perl

if (-e "/etc/exim_custom.pl") {
do "/etc/exim_custom.pl";
}

1;
EOF
chmod +x /etc/exim/load_custom.pl
fi

# Restart Exim to apply changes
service exim restart

echo "Exim configuration patched successfully."

Make the script executable:

chmod 750 /usr/local/directadmin/custombuild/custom/hooks/exim_conf/post/patch_exim_conf.sh

6. Apply the Configuration

Run the CustomBuild command to apply the changes:

cd /usr/local/directadmin/custombuild
./build exim
./build exim_conf

7. Test the Configuration

Send a test email to a forwarded address and check if the ‘From’ address is preserved correctly:

echo -e "Subject: Test Email\nFrom: [email protected]\nTo: [email protected]\n\nThis is a test email to verify forwarding with custom headers." | sendmail -f [email protected] [email protected]

Check the email logs:

tail -f /var/log/exim/mainlog

Benefits

  • Maintains compatibility with SMTP providers like Postmark, SendGrid, and others.
  • Preserves the original recipient address, improving email traceability.
  • Works seamlessly within the DirectAdmin environment.

Considerations for Different SMTP Providers

  • Postmark: Strict about sender addresses matching verified domains.
  • SendGrid: Requires domain authentication for optimal deliverability.
  • Mailgun: Offers flexible sending options but benefits from proper ‘From’ address handling.
  • Amazon SES: Requires verification of sending domains and email addresses.

Conclusion

By implementing this Exim configuration in your DirectAdmin setup, you can effectively solve the ‘From’ address issue in email forwarding. This solution ensures compatibility with various SMTP providers while maintaining the integrity of your email forwarding system. Remember to always test thoroughly after making changes to your email configuration.

By modifying the /etc/exim.routers.pre.conf file and using DirectAdmin’s CustomBuild hooks, you’re ensuring that your custom router configuration is properly integrated into the Exim setup, providing a robust solution for email forwarding issues.

Choose for the best Directadmin Hosting Provider

99,98% uptime, lastest Directadmin version, 24/ Directadmin Support.

Directadmin Hosting

Directadmin Hosting since 2012.

Copyright: © 2026 DirectadminHosting.eu