Linux – How is mail actually sent when I use the Linux “mail” command

command lineemaillinux

What email account is used to send an email when using the mail command from the linux terminal as in:

echo "Body of email" | mail -s "Subject" abc@def.com

If the emails are not being sent, does one need to specify the user name and password of a sending email address? All the mail tutorials I have read specify nothing about the sender address.

Edit: So if I receive an error message to the effect of "Mailing to remote domains not supported", does this mean my ISP has blocked sending emails? Is there any way around this? Thanks.

Best Answer

What email account is used to send an email when using the mail command from the linux terminal as in:

In default configurations, it's the very same account you used to log in. Your computer has a mail server program (a MTA) installed; usually either Postfix or Exim4, sometimes Sendmail or qmail.

The email address of this account is your-login@hostname or your-login@fqdn, where the FQDN can be obtained from hostname -f. However, right now you cannot receive mail to this account, since the MTA is configured for "local mail only" (as the error message below says) and will not accept messages from outside.

Edit: So if I receive an error message to the effect of "Mailing to remote domains not supported", does this mean my ISP has blocked sending emails? Is there any way around this?

No; this error message is returned by your own computer. The mail server is configured to only transfer local mail – from one user to another (most commonly, from the cron daemon if a cronjob fails). Usually these messages go to /var/mail/login and are readable using mail, mutt, re-alpine, or similar programs. (IIRC, Thunderbird had the ability to import local mail spools as well.)

It is not hard to reconfigure the mail server to send and receive mail to/from other sites; depending on the MTA installed and on the Linux distribution, even a single command might be enough: for example, dpkg-reconfigure postfix if the system is Debian with Postfix.

However, I'm guessing you are using this on a personal computer, so it would be better to use an external mail account instead – for example, your Gmail address if you have one. Find out which MTA is installed (on Ubuntu/Debian, dpkg -S /usr/sbin/sendmail will tell you), then google for "program-name relay gmail" for a tutorial.

Alternatively, install a MTA specifically designed for relaying; msmtp and esmtp are good choices and easy to configure.

Related Question