1

The terminal does not pop out any error message, but I never receive the email. this is my code:

mail -s "hello" "example@example.com" <<EOF
hello
world
EOF
  • what does `mailq` say? – John C Jun 14 '14 at 02:10
  • Try sending to your own account on the local system (just your username, no `@host`). That should work. Your code looks correct; I suspect outgoing mail isn't set up properly. Try running `mail` to check for an "outgoing mail failed" message. – Tom Zych Jun 14 '14 at 02:25
  • `mailq` is a command to view the mail queue. You really should say what operating system you are using. – John C Jun 14 '14 at 02:28
  • I tried sending it to my own account on the local system, and it works, but I can't send it to an email account – user2175394 Jun 14 '14 at 02:28
  • the command as such is fine. Do you run that script from a machine with dynamic ip address, or one with reverse DNS lookup not matching its mailname? – Deleted User Jun 14 '14 at 02:32
  • Rather than using a here-doc, you can also send by piping the text to mail. For example with `echo -e "your formatted message here" | mail -s "subject" to_whom@example.com` There is no need to quote the email address, the subject only needs quoting if it is multi-word. I have always found `echo` or `printf` easier than here-docs – David C. Rankin Jun 14 '14 at 02:36
  • This is blatantly offtopic – mlt Jun 14 '14 at 02:40

1 Answers1

1

Works fine for me:

pax> mail -s "hello" "pax" <<EOF
hi there
EOF

pax> mailx
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/pax": 1 message 1 new
>N  1 pax@paxbox.com  Sat Jun 14 10:25   16/629   hello
& _

You should try it with a local address first (as I have) to see if a mail is being created.

Beyond that, you should realise that mail simply adds mail messages into the mail system. If you want to find out what happens after that, you'll need to look into whatever MTAs (mail transfer agents) you have set up on your system.

If the MTA itself fails, you'll almost certainly get a mail back to the sending account stating so (you can use mailx as I have above, to discover this).

Since you haven't specified your systems, I'll give advice below based on Debian since that's what I'm used to.

On my Debian box, exim is the MTA but, by default, it does not support sending to remote domains. You can modify this by running:

sudo dpkg-reconfigure exim4-config

but you need to be careful not to relay emails lest you unknowingly become a spam-bot. More details can be found here.

You may find, if you want them to go to the outside world, that it's better to send them to your ISP via SMTP rather than trying to configure mail on your local box to do it.

However, if you want to go the mail route, simply run dpkg-reconfigure as above, select "Internet site; mail is sent and received directly using SMTP" as the answer to the first question, then accept defaults for all the other questions (checking to ensure you only accept mail from your local addresses 127.0.0.1 and ::1).

Then wait for exim to restart and try send the mail again.

Just be aware that exim typically starts queue runners (the processes that actually send out your email) on a schedule (30 minutes for me) so it may take some time for the message to go out.

You can examine the files in /var/log/exim4 to see what's happening (such as, in my case, my ISP rejecting the attempt since it knows nothing about pax@paxbox.com but you may be able to find an open SMTP relay somewhere or spoof your sending details to something your ISP will allow).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • how should /etc/mail.rc should look like? – user2175394 Jun 14 '14 at 02:34
  • how do I send them to my ISP via SMTP? – user2175394 Jun 14 '14 at 02:43
  • @user2175394, you can find a program capable of direct SMTP connection (such as Python https://docs.python.org/2/library/smtplib.html), modify your `bash` stuff (see http://stackoverflow.com/questions/9998710/can-it-possible-to-send-mails-by-bash-script-via-smtp) or configure your MTA to make it easier. – paxdiablo Jun 14 '14 at 02:46