37

Suppose i send a mail using the following the following command:

mailx person@x.com

then does mailx first try to find out the SMTP server of my ISP for relaying the mail or does it connect directly. Does it depend on whether my PC has a public IP address or it is behind a NAT. How do I check the settings of mailx on my PC? How can I verify this using tcpdump?

Rohit Banga
  • 2,494

6 Answers6

37

mailx can use SMTP. It's configure file is ~/.mailrc

One example is mailx using Gmail's SMTP.

The configure can even be in one command:

mailx -v -s "$EMAIL_SUBJECT" \
-S smtp-use-starttls \
-S ssl-verify=ignore \
-S smtp-auth=login \
-S smtp=smtp://smtp.gmail.com:587 \
-S from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)" \
-S smtp-auth-user=$FROM_EMAIL_ADDRESS \
-S smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD \
-S ssl-verify=ignore \
-S nss-config-dir=~/.mozilla/firefox/xxxxxxxx.default/ \
$TO_EMAIL_ADDRESS

If a normal SMTP server is used, it is much easier (see a detailed introduction here):

mailx -v -s "$EMAIL_SUBJECT" \
-S smtp=smtp://smtp.example.com
-S from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)" \
$TO_EMAIL_ADDRESS

You can also put these into mailx's configuration file ~/.mailrc

ericzma
  • 606
35

Traditionally, Unix mail and derivatives (and many other Unix tools) use the /usr/bin/sendmail interface, provided by almost all mail transfer agents (MTAs – postfix, exim, courier, and of course sendmail).

That is, the mail program doesn't speak any network protocol – it feeds the message to sendmail via stdin, and lets it handle actual delivery. (This goes back to the days when some mail used SMTP, some used UUCP, some used BITNET...)

Once a message is queued through sendmail, the MTA handles actual message transmission, whether through SMTP or something else. Depending on configuration, it may either connect directly to the destination MTA, or relay mail through another host (also called a smarthost).

Direct connection is more common on servers; relay via smarthost is more common on personal computers on home connections – relaying through your Gmail or ISP/work email account is essential to avoid the blanket "dynamic IP" anti-spam filters.

(Some MTAs such as esmtp or nullmailer are built specifically for home users and always use a relayhost. These don't support receiving mail and are a lot lighter on resources.)

mailx → [/usr/bin/sendmail] → local MTA queue → [SMTP] → recipient MTA → recipient inbox
mailx → [/usr/bin/sendmail] → local MTA queue → [SMTP] → Gmail or ISP/work servers → [SMTP] → recipient MTA → recipient inbox

Other programs, mostly the user-friendly graphical clients such as Thunderbird or Outlook, always connect directly to a relay/smarthost SMTP server (again, usually Gmail or ISP/work SMTP server), which transmits the message on your behalf.

Native SMTP support is present in heirloom-mailx, but not in the traditional bsd-mailx.

app → [SMTP] → Gmail or ISP/work servers → [SMTP] → recipient MTA → recipient inbox

The third method – connecting directly to recipient's server – is almost never used, and no MUA supports it. On personal computers, using it would cause your message to get rejected (a lot of spam is sent from infected home user IP addresses).

app → [SMTP] → recipient MTA → caught by the spam filter
grawity
  • 501,077
4

From the mailx(1) man page, DESCRIPTION section, String Options subsection:

   smtp   Normally, mailx invokes sendmail(8) directly to  transfer
          messages.  If the smtp variable is set, a SMTP connection
          to the server specified by the value of this variable  is
          used  instead.
1

I'd like to expand on what exactly mailx is. Because the question is phrased as if there's only one implementation. And I believe the other answers failed to cover this part so far.

To keep things simple on Linux there's BSD mailx (which I guess is an OpenBSD implementation at least in case of Debian and is standardized) and Heirloom mailx (which was discontinued or abandoned) and its fork S-nail. In case of Alpine Linux that would be mailx (source) and s-nail (source) packages. More on it here.

BSD mailx doesn't support SMTP. It expects /usr/sbin/sendmail (which can be overridden by the sendmail option in a command file or an environment variable by the same name) to be present, which is used to send mail.

Heirloom mailx and S-nail do support SMTP. E.g. to send an email using S-nail (yep, S-nail is an mailx implementation, but the binary is called mail):

$ echo test email \
    | mail -:/ \
        -S v15-compat \
        -S from=me@example.com \
        -S user=me@example.com \
        -S password=... \
        -S mta=smtps://mail.example.com \
        -s 'test email' \
        -vvv \
        dst@gmail.com

-:/ - don't read the resource files (/etc/mail.rc, ~/.mailrc), -S v15-compat - enable upward compatibility with S-nail version 15.0 (enable the new behavior).

In this case mail sends an email to mail.example.com (over SMTP), which in its turn sends it to a server from one of the gmail.com's MX records (also over SMTP, to make it clear).

The settings are in command/resource files (/etc/mail.rc and ~/.mailrc).

In this case S-nail connects to port 465. You can verify it with tcpdump this way:

# tcpdump port 465
x-yuri
  • 347
1

there is an alternative without local mta like sendmail/postix.

debian package ssmtp

info from rpm description:

Summary     : Extremely simple MTA to get mail off the system to a Mailhub
URL         : http://packages.debian.org/stable/mail/ssmtp
License     : GPLv2+
Description : A secure, effective and simple way of getting mail off a system to your mail
            : hub. It contains no suid-binaries or other dangerous things - no mail spool
            : to poke around in, and no daemons running in the background. Mail is simply
            : forwarded to the configured mailhost. Extremely easy configuration.

hth

Stefan K.

0

I used the follow command to specify the MTA:

msmtp --host=172.17.0.1 mydestemail@gmail.com --auto-from=on

My MTA is a postfix server that handle the gmail auth.

Zioalex
  • 121