How is mail actually sent when I use the Linux "mail" command?

17

6

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.

user001

Posted 2012-01-31T20:43:04.763

Reputation: 2 074

Answers

12

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.

user1686

Posted 2012-01-31T20:43:04.763

Reputation: 283 655

Thanks for all the help. The pastebin command you suggested ended up meeting my needs. I will work on setting up a smarthost for mail relaying. – user001 – 2012-01-31T22:03:03.137

4

The mail command sends mails under the current account, i.e. username@domain ; however, usually internet service providers prevent users from running their own mail servers (by blocking the relevant ports) to avoid spam, and therefore your email is not sent.

But the sending problem could also be related to the configuration of your machine.

Edit: enabling mail to send emails from e.g. gmail. Put the following information into your ~/.mailrc file, which provides basing configuration to mail. However, make sure that this file is only accessible to you (using chmod 600 ~/.mailrc)

account gmail {
set smtp-use-starttls
set ssl-verify=ignore
set smtp=smtp.gmail.com:587
#set smtp-auth=login
set smtp-auth-user=name@gmail.com
set smtp-auth-password=pass
set from=name@gmail.com
}

In which case the email will be coming from "name@gmail.com".

Karolos

Posted 2012-01-31T20:43:04.763

Reputation: 2 304

@user001: Do you want just to be able to send mails (e.g. from scripts) or also to receive them ? – Karolos – 2012-01-31T20:56:26.053

Just to be able to send a message from a script. Thanks. – user001 – 2012-01-31T20:57:38.927

2Note that this mailrc syntax is specific to heirloom-mailx. Some systems may come with GNU mailutils or BSD mailx, both of which would complain about syntax errors. (If that happens, I suggest installing heirloom-mailx - or even better, mutt or re-alpine.) – user1686 – 2012-01-31T21:07:42.480

4

It's the account name and host name of the sending user, for example root@myserver. It's not necessarily an actual mailbox that can receive email from the outside.

Daniel Beck

Posted 2012-01-31T20:43:04.763

Reputation: 98 421

2

@user001 Does this help?

– Daniel Beck – 2012-01-31T20:57:27.707

Thanks DanielBeck. That looks very relevant. I will try that. I currently have a broken Debian system and am trying to mail myself some error logs, so I may be stuck in that dpkg may fail. – user001 – 2012-01-31T21:00:21.217

2@user001: In such cases, you might take a look at pastebins, some of which are easy to use from command-line. For example, curl -Fsprunge=\<- http://sprunge.us < myfile. – user1686 – 2012-01-31T21:09:42.163

Hi DanielBeck. The exim4 reconfigure eliminated the error message about sending to remote domains and the send appears successful from my localhost. However, I never actually receive the mail, so it seems to be getting lost in Internet limbo. Per @grawity's suggestion, dpkg -S /usr/sbin/sendmail gives exim4-daemon-light. Where do you think the sent emails are going now that I do not receive error messages when I send them using mail? I will look into the paste bin as was suggested. – user001 – 2012-01-31T21:24:21.907

1@user001: Do you have the exim4 daemon running? Does sudo mailq list the message? Have you checked the logs of the sending server (/var/log/exim4/mainlog) and the recipient (if possible)? – user1686 – 2012-01-31T21:28:31.417

Hi @grawity. sudo mailq does list the messages. The main log indicates the connection to the recipient (gmail-smtp-in.l.google.com) timed out. I can, however, ping this address successfully. – user001 – 2012-01-31T21:35:09.957

1@user001: It could be that your ISP is blocking the connection. This is especially common on home connections - everything to port 25 simply gets dropped, in order to reduce the massive amounts of spam sent by infected PCs. (You still can configure relaying, though, which uses a different port and SMTP authentication.) – user1686 – 2012-01-31T21:39:55.157

Thanks @grawity, I am on an academic network -- I don't know if port 25 traffic is dropped. I did notice something about relaying in dpkg-reconfigure of exim, so I could enable that. Do I simply instruct my localhost to relay messages to the target email address? Thanks. – user001 – 2012-01-31T21:48:08.537

1

@user001: Relaying happens through another mail server (sometimes called a "smarthost"). Your network admins might help with this, if the academy has its own mail domain. If not, you could configure your server to send from your Gmail or similar mailbox; tutorials here.

– user1686 – 2012-01-31T21:59:38.353

0

Refer this How to send email from the Linux command line for some more information

Siva Charan

Posted 2012-01-31T20:43:04.763

Reputation: 4 026

1Thanks SivaCharan. That is one of the tutorials I had already read, but it does not address my question about the from address. – user001 – 2012-01-31T20:56:37.680