35

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?

33

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
23
  • 1
    how to find out my MTA on linux? May 4 '10 at 17:03
  • 1
    @iamrohitbanga 1) Check the list of installed packages. (Not all distros come with a MTA by default.)
    – user1686
    May 4 '10 at 17:10
  • 1
    @iamrohitbanga 2) I already answered that. Outlook is often used on a personal computer at home, and many mailservers reject messages received from home users' addresses (because of a high spam rate from those). That's why relaying through a corporate server is needed.
    – user1686
    May 4 '10 at 17:12
  • 1
    @iamrohitbanga 3) "or" means "either one of", not "both". Those who use Gmail as their primary mail account send mail through Gmail's servers. Those who have a mailbox at their ISP use their ISP's servers.
    – user1686
    May 4 '10 at 17:13
  • 1
    @iamrohitbanga 4) That's because mailx does not use the "third method". It uses a MTA as described on the top of my answer. And once again, if you're not on a corporate Internet connection, mail sent directly from your PC (without a relay) is very likely to be discarded.
    – user1686
    May 4 '10 at 17:14
36

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

7
  • Note that this depends on heirloom-mailx which is not the default mailx.
    – Scott
    Apr 5 '12 at 2:37
  • @Scott: Yes. But depends on the Linux distribution. On some systems, the default is not heirloom (e.g. Ubuntu: fclose.com/b/linux/1411/… . seems there are 3 mailx versions). On some other ones such as Fedora, OpenSUSE, the default one is the "feature riched" "heirloom-mailx".
    – ericzma
    Apr 16 '12 at 9:41
  • @ericzma I guess heirloom-mailx is the best / is heirloom the only mailx that can do it(specifying from and smtp server at command line)? It works nicely on Debian though is not installed by default. On Debian mailx links to /etc/alternatives/mailx which links to /usr/bin/bsd-mailx After installing heirloom-mailx to debian, /etc/alternatives/mailx links to /usr/bin/heirloom-mailx and worked nicely
    – barlop
    Sep 26 '14 at 10:59
  • @barlop Your finding is consistent with mine: heirloom-mailx works while bsd-mailx does not. Not aware about other working implementations yet.
    – ericzma
    Oct 1 '14 at 6:49
  • CentOS 6.7 uses Heirloom mailx 12.4 Jan 26 '16 at 0:39
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.
2
  • this confused me a bit. can you be more elaborate. May 4 '10 at 14:18
  • Uhh... it uses sendmail unless this option is set. May 4 '10 at 14:41
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.

1
  • 2
    Almost... ssmtp is a MTA-like SMTP client. It behaves like /usr/bin/sendmail but instead of connecting directly to the MX record of a particular domain, it delegates this task to an SMTP server accessible (usually via username/password) to the machine running ssmtp. This is particularly useful for those system sending email from high spam IP ranges like ADSL dynamic IP ranges, dodgy hosting providers, etc. Jan 27 '14 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.