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

55,154

Solution 1

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.

Solution 2

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 [email protected]
set smtp-auth-password=pass
set [email protected]
}

In which case the email will be coming from "[email protected]".

Solution 3

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.

Share:
55,154

Related videos on Youtube

user001
Author by

user001

Updated on September 18, 2022

Comments

  • user001
    user001 over 1 year

    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" [email protected]
    

    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.

  • Karolos
    Karolos over 12 years
    @user001: Do you want just to be able to send mails (e.g. from scripts) or also to receive them ?
  • user001
    user001 over 12 years
    Thanks SivaCharan. That is one of the tutorials I had already read, but it does not address my question about the from address.
  • HikeMike
    HikeMike over 12 years
    @user001 Does this help?
  • user001
    user001 over 12 years
    Just to be able to send a message from a script. Thanks.
  • user001
    user001 over 12 years
    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.
  • user1686
    user1686 over 12 years
    Note 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
    user1686 over 12 years
    @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.
  • user001
    user001 over 12 years
    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
    user001 over 12 years
    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.
  • user1686
    user1686 over 12 years
    @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.)
  • user001
    user001 over 12 years
    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.
  • user1686
    user1686 over 12 years
    @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.