Sendmail

From Indie IT Wiki
Revision as of 14:35, 2 July 2020 by imported>Plittlefield (→‎Find All Email Forward Files)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Sendmail is a general purpose internetwork email routing facility that supports many kinds of mail-transfer and -delivery methods, including the Simple Mail Transfer Protocol (SMTP) used for email transport over the Internet.

Send Mail Via Mailertable

If you want to send email for a particular domain direct to a server rather than using the default smart host you can use the mailertable.

cd /etc/mail

nano mailertable
otherdomain.com smtp:mailserver.otherdomain.com

makemap hash mailertable < mailertable

nano sendmail.mc
FEATURE(`mailertable')

make -C /etc/mail

service sendmail restart

Send Message Via Command Line

cat test.msg
To: me@domain.com
Subject: sendmail test
From: me@domain.com

This is a test email message.

sendmail -vt < test.msg

Vacation

vacation -i
nano ~/.vacation.msg
From: James Smith <james@domain.co.uk>
Subject: Out Of Office (Re: $SUBJECT)

Thank you for your email.

I am now on holiday.

Regards,

James
nano ~/.forward
\jsmith, "|/usr/bin/vacation -a james -a james.smith -r1d jsmith"

Generate SMTP AUTH Username Password

perl -MMIME::Base64 -e 'print encode_base64("username\0username\0mypassword");'

Flush The Mail Queue

/usr/lib/sendmail -v -q -C/etc/mail/sendmail.cf

Set A Catchall Account

/etc/mail/virtusertable

@yourdomain.com   user

Then, rebuild the hash and restart sendmail...

makemap hash virtusertable < virtusertable
newaliases
/etc/init.d/sendmail restart

Set Different SMTP Port

/etc/mail/sendmail.mc

define(`RELAY_MAILER_ARGS', `IPC $h 525')

Set Maximum Message Size

/etc/mail/sendmail.mc

define(`confMAX_MESSAGE_SIZE', `10485760')

Change Root User Sender/From Email Address

See Do Not Expose Root section below as well...

/etc/mail/sendmail.mc

FEATURE(`genericstable',`hash -o /etc/mail/genericstable.db')
GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')

/etc/mail/generics-domains

server1.domain.co.uk
domain.co.uk

/etc/mail/genericstable

root    server1@domain.co.uk
cd /etc/mail/
makemap hash genericstable < genericstable
m4 sendmail.mc > sendmail.cf
/etc/init.d/sendmail restart

http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch21_:_Configuring_Linux_Mail_Servers

Do Not Expose Root

nano /usr/share/sendmail-cf/domain/generic.m4
dnl EXPOSED_USER(`root')

What Features Is Sendmail Compiled With?

sendmail -d0.1

ERROR: mailx sendmail bfcommit permission denied

chown root /var/spool/mqueue

alias List - Editing: How To

In this example there is an alias entitled 'staff'...

  1. log in via ssh to server
  2. su - root
  3. screen -r
  4. nano /etc/mail/aliases
  5. [page down]
  6. find staff: line
  7. edit as needed
  8. save
  9. newaliases
  10. CTRL+A D
  11. logout

ERROR: sendmail[14870]: gethostbyaddr(192.168.1.1) failed: 1

Add entries in your DNS or /etc/hosts (or both!) for this ip address.

Add A Custom Macro Definition File

cd /usr/share/sendmail-cf/domain
cp -av generic.m4 mydomain.co.uk.m4

Find All Dovecot Sieve Out of Office Links

find /home/ -maxdepth 2 -type l -name '.dovecot.sieve' -print |sort

Find All Email Forward Files

find /home/ -maxdepth 2 -type f -name '.forward'

and to see them...

find /home/ -maxdepth 2 -type f -name '.forward' -printf "\n%p\n" -exec cat {} \;

Example Forward File With Copy Left On Server

me@anotheremail.com
\me

Edit The Custom Macro Definition File

Remove the line to expose root, otherwise emergency emails will not be accepted by ISP.

EXPOSED_USER(`root')   <-- delete this line

Typical Server Macro File

divert(-1)
divert(0)
include(`/usr/share/sendmail-cf/m4/cf.m4')
VERSIONID(`$Id: sendmail-procmail.mc 2010-10-28 plittlefield Exp $')
OSTYPE(linux)
DOMAIN(mydomain.co.uk)
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1,Name=MTA')
DAEMON_OPTIONS(`Port=smtp,Addr=192.168.0.200,Name=MTA')
define(`ALIAS_FILE', `/etc/mail/aliases')
define(`STATUS_FILE',`/etc/mail/statistics')
define(`SMART_HOST',`[smtp.myisp.co.uk]')
FEATURE(`authinfo',`hash -o /etc/mail/authinfo.db')
FEATURE(`access_db')
FEATURE(`genericstable')
FEATURE(`relay_entire_domain')
FEATURE(`accept_unresolvable_domains')
FEATURE(`smrsh',`/usr/sbin/smrsh')
FEATURE(`local_lmtp',`/usr/sbin/mail.local')
FEATURE(`local_procmail')
FEATURE(`no_default_msa')
FEATURE(`limited_masquerade')
FEATURE(`masquerade_envelope')
MASQUERADE_AS(`mydomain.co.uk')
MASQUERADE_DOMAIN(`server1.mydomain.co.uk')
EXPOSED_USER(`root')
MAILER(local)
MAILER(smtp)
MAILER(procmail)

Multiple SMTP Smart Hosts

Add colon separated server hostnames and update your authinfo file.

define(`SMART_HOST',`[smtp.company1.com]:[auth.smtp.company2.co.uk]')

ERROR: sm-mta SYSERR(root): collect: Cannot write

Fix: change the ownership of /var/spool/mqueue to root.

chown root /var/spool/mqueue

Using Telnet To Test SMTP With Authentication

echo -ne '\000username\000password' | openssl base64

or

perl -MMIME::Base64 -e 'print encode_base64("username\0username\0password");'

OR generate your AUTH PLAIN encrypted string using the gen-auth command from the web site link below.

Choose PLAIN and then your username and password.

./gen-auth

Now log in via telnet...

telnet smtp.company.co.uk smtp
Trying 212.227.15.163...
Connected to smtp.company.co.uk.
Escape character is '^]'.
220 smtp.company.co.uk Welcome to our SMTP server
EHLO test
250-smtp.company.co.uk
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-SIZE 120000000
250 HELP
AUTH PLAIN cGF1bGx5AHTHISISFAKEeQBjaDRybDNz
235 2.7.0 Authentication successful
MAIL FROM:<me@company.co.uk>
250 2.1.0 Ok
RCPT TO:<info@company.co.uk>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Message.
.
250 2.0.0 Ok: queued as CB3361DE15F4
quit
221 2.0.0 Bye
Connection closed by foreign host.

http://www.jetmore.org/john/code/gen-auth

http://www.freebsdwiki.net/index.php/SMTP,_testing_via_Telnet

http://qmail.jms1.net/test-auth.shtml

http://www.commandlinefu.com/commands/view/2857/create-auth-plain-string-to-test-smtp-auth-session