Does mailx send mail using an SMTP relay or does it directly connect to the target SMTP server?

31

16

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

Posted 2010-05-04T13:17:09.170

Reputation: 1 814

Answers

29

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

user1686

Posted 2010-05-04T13:17:09.170

Reputation: 283 655

1how to find out my MTA on linux? – Rohit Banga – 2010-05-04T17:03:49.360

why should outlook send through a relay SMTP server? If i am sending a mail to iamrohitbanga@gmail.com then it should directly contact mail server for gmail which is the destination mail server and not the relay server? right or wrong? – Rohit Banga – 2010-05-04T17:06:29.993

why do you use Gmail or ISP/work servers together? suppose I am sending from a@b.com to c@d.com shouldn't the sequence be app->b->d->recipient inbox. – Rohit Banga – 2010-05-04T17:08:19.723

did not understand third method. when i send using mailx iamrohitbanga@gmail.com, no mail lands up in my inbox and no mailer daemon notification is returned. why did my message disappear? For my organizations mail server however i do get a message saying that mail was not delivered as name could not be resolved. Why is that so? I remember that I used mailx to send messages this way on another system. Is there a configuration problem? – Rohit Banga – 2010-05-04T17:09:43.653

1@iamrohitbanga 1) Check the list of installed packages. (Not all distros come with a MTA by default.) – user1686 – 2010-05-04T17:10:29.890

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 – 2010-05-04T17:12:25.767

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 – 2010-05-04T17:13:38.730

rpm -qa *post* tells me postfix-2.5.1-28.1 is installed. – Rohit Banga – 2010-05-04T17:13:57.370

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 – 2010-05-04T17:14:15.030

ok so when outlook is configured using my gmail account. it relays the message through gmail server. i got it now. – Rohit Banga – 2010-05-04T17:16:37.613

where does my mail disappear? btw found this http://www.postfix.org/OVERVIEW.html useful.

– Rohit Banga – 2010-05-04T17:28:10.673

i remember i sent mail using mailx on a different system. – Rohit Banga – 2010-05-04T17:37:04.570

now are there any changes in firewall rules that are preventing me or something else? – Rohit Banga – 2010-05-04T17:38:49.650

1@iamrohitbanga That "different system" had a properly configured MTA. This includes opening port 25/tcp for incoming connections (otherwise you won't receive any incoming mail). – user1686 – 2010-05-04T17:43:29.373

i am sending a mail to x@gmail.com from my system. so does that mean mailer daemon is sending a message telling about the discarded message to my computer which is not listening on port 25. – Rohit Banga – 2010-05-04T17:45:26.513

but that different system did deliver mail to my gmail account. Infact I had small program for backing up all my important data in compressed format to my gmail account. all sent using mailx. now mailx is just not delivering my mail. how can I find the problem. – Rohit Banga – 2010-05-04T17:46:52.907

OK may be my MTA that is postfix is not forwarding the mail to the proper relay server. now i understand. i will try and see the postfix configuration. Thanks a lot. – Rohit Banga – 2010-05-04T17:52:47.417

Can we use mailx from our server, compose the body, attach files, but then connect to a remote Sendmail server just for the "sending" but without SMTP? We've been whitelisted on the remote Sendmail server, but we want to compose the email on our side. – PKHunter – 2018-09-25T17:19:18.913

1@PKHunter: Sure, it would be easy to write a shell script with ssh mymailserver /usr/sbin/sendmail. Most modern mail/mailx clients let you specify a custom path to the sendmail tool; point it at the shell script. (But why don't you want to use SMTP for injecting the message? That's exactly what the 'Message Submission' service on port 587 is for.) – user1686 – 2018-09-25T18:50:30.920

@grawity thank you for this. We can't use shell scripts. We have to use mailx on our server, compose a body with attachments on our server via our Node app, but finally do the actual 'sending' with a SendMail server that's running on a remote IP without smtp-auth enabled. Do we need mailx for this purpose at all? Basically if the remote server had SMTP enabled with usual auth (server, port, username, pass) it would solve all these problems. The issue we don't have a username/pass enabled on that side. – PKHunter – 2018-09-25T23:44:51.097

Sorry, I meant smtp-auth is disabled. We need to use sendmail without any password. The remote server with Sendmail has whitelisted our IPs. – PKHunter – 2018-09-25T23:52:35.570

If you are using SMTP, then SendMail on the remote side is completely irrelevant. It's not the same thing as a local /usr/lib/sendmail API; it's just a service that provides SMTP. And if it provides SMTP, then you use one of the dozen Node modules which can talk SMTP. Honestly, comments of an unrelated thread aren't the place for this... – user1686 – 2018-09-26T04:28:51.400

@RohitBanga Technically looking at what's installed is not going to work always for something like that. You can have both sendmail and postfix installed. In Linux of course there is the alternatives system so one can specify if they want to use one or the other as a default. But the point here is that that's not enough to figure out if your default MTA is postfix. And neither necessarily is checking for just those two since there are more MTAs out there. If you configured one then you probably know. Just as a FY >1 decade later (it's the slowest of slow snail mail? :) ) – Pryftan – 2019-07-25T15:00:24.793

31

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

Posted 2010-05-04T13:17:09.170

Reputation: 538

@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 – 2014-09-26T10:59:29.977

@barlop Your finding is consistent with mine: heirloom-mailx works while bsd-mailx does not. Not aware about other working implementations yet. – ericzma – 2014-10-01T06:49:59.307

CentOS 6.7 uses Heirloom mailx 12.4 – Joshua Grigonis – 2016-01-26T00:39:05.970

The two links are the same link. Perhaps you wanted to suggest this one where the suggested smtp server is smtp.ust.hk. I do not know where did you get smtp.server.com: is it a real server or a mere example (fake server)? Ping returns unknown host.

– Asarluhi – 2018-03-18T14:14:33.417

@Asarluhi Yes. Thanks for pointing this out. The smtp.server.com (just changed to smtp.example.com) is just an example. – ericzma – 2018-03-19T15:10:18.797

Note that this depends on heirloom-mailx which is not the default mailx. – Scott – 2012-04-05T02:37:22.793

@Scott: Yes. But depends on the Linux distribution. On some systems, the default is not heirloom (e.g. Ubuntu: http://fclose.com/b/linux/1411/sending-email-from-mailx-command-in-linux-using-gmails-smtp/comment-page-3/#comment-487 . seems there are 3 mailx versions). On some other ones such as Fedora, OpenSUSE, the default one is the "feature riched" "heirloom-mailx".

– ericzma – 2012-04-16T09:41:37.733

3

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.

Ignacio Vazquez-Abrams

Posted 2010-05-04T13:17:09.170

Reputation: 100 516

this confused me a bit. can you be more elaborate. – Rohit Banga – 2010-05-04T14:18:07.270

Uhh... it uses sendmail unless this option is set. – Ignacio Vazquez-Abrams – 2010-05-04T14:41:22.207

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.

StefanKaerst

Posted 2010-05-04T13:17:09.170

Reputation: 51

1Almost... 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. – Andre de Miranda – 2014-01-27T10:19:08.257