Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active May 22, 2024 23:49
Show Gist options
  • Save drmalex07/d63348dc9d26d1349309 to your computer and use it in GitHub Desktop.
Save drmalex07/d63348dc9d26d1349309 to your computer and use it in GitHub Desktop.
Configure sendmail as an SMTP smart host (relay). #smtp #sendmail #relay #mail

Note: Prefer Exim4 over Sendmail

Configure sendmail

Append the following directives into /etc/mail/sendmail.mc. These lines should come before mailer definitions.

define(`SMART_HOST',`mail.example.com')
define(`RELAY_MAILER_ARGS', `TCP $h 587')
define(`ESMTP_MAILER_ARGS', `TCP $h 587')
FEATURE(`authinfo',`Hash -o /etc/mail/authinfo.db')
TRUST_AUTH_MECH(`PLAIN LOGIN') 
define(`confAUTH_MECHANISMS', `PLAIN LOGIN')  
define(`confAUTH_OPTIONS', `A')

MASQUERADE_AS(`example.com')
MASQUERADE_DOMAIN(`localdomain')
MASQUERADE_DOMAIN(`other-example.com')
FEATURE(`genericstable',`hash -o /etc/mail/genericstable.db')
GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')
FEATURE(masquerade_envelope)
FEATURE(masquerade_entire_domain)

Create authinfo.db for credentials

Edit (create if not exists) credentials file at /etc/mail/authinfo and supply authentication settings (a line for each SMTP host):

AuthInfo:mail.example.com "U:username" "P:password"

Protect the file from other users

chmod 0600 /etc/mail/authinfo

Create authentication database from plaintext file:

makemap hash /etc/mail/authinfo.db < /etc/mail/authinfo

Configure masquerading for users/domains

Create a /etc/mail/generics-domains file that is just a list of all the domains that should be inspected. Make sure the file includes your server's canonical domain name, which you can obtain using the command:

sendmail -bt -d0.1 </dev/null

An example /etc/mail/generics-domains could be:

foo.example.com
baz.example.com

Create your /etc/mail/genericstable file. First sendmail searches the /etc/mail/generics-domains file for a list of domains to reverse map. It then looks at the /etc/mail/genericstable file for an individual email address from a matching domain. An example is:

www-data no-reply@example.com
root     admin@example.com

See also https://sites.google.com/site/syscookbook/miscellaneous/sendmail

Finally, create the database for genericstable:

makemap -r hash genericstable.db < genericstable

Build sendmail configuration and restart

Since sendmail's configuration has changed, we need to rebuild it (inside /etc/mail). Afterwards (if everything went ok) restart the daemon:

make
invoke-rc.d sendmail restart

Test sendmail

Test your configuration, use sendmail in verbose mode to contact someone:

echo -e "Subject: Hello\r\n\r\nI say hello"| sendmail -v -F 'Contact' someone@foo.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment