user2175394
user2175394

Reputation: 83

Cant send email with bash script

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

Upvotes: 1

Views: 1002

Answers (1)

paxdiablo
paxdiablo

Reputation: 882566

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).

Upvotes: 1

Related Questions