2

Hey I was just wondering when I trying to create a bash script which can send mail. To start off I was testing mail out using the command line but when I run the command nothing happens. I suppose the command tries to send it but it never goes through and it just hangs and I have to send the kill letter.

These are the commands i have tried:

mail -s "Subject" [email protected]
mailx -s "Subject" [email protected]
3
  • Maybe it's waiting for you to enter the message body on standard input? Commented Nov 24, 2014 at 18:48
  • it doesnt prompt me for any additional information. i even tried sendmail command but after the subject it hung again Commented Nov 24, 2014 at 18:54
  • Those commands don't prompt for input, they assume it will be provided on the standard input. Try entering a message body and see what happens. Commented Nov 24, 2014 at 18:57

2 Answers 2

1

First, you need to send a message with your mail. We can also try verbose mode:

$ mailx -v -s "Test Message" [email protected] <<EOM
> This is my message I want to send.
> I can keep typing it and the last line ends with just "EOM"like this:
> EOM
Mail Delivery Status Report will be mailed to <foo>.
$

The <<EOM is called a Here Document. It tells your computer to expect input to direct to the command from STDIN (the keyboard), and that the input will end with the string that followed the << characters (here EOM).

You'll get a mail report emailed to you. You can use mailx to read it, or one of those fancy new email programs like elm or pine, or just read your mail from the command line via mailx:

$ mailx
Mail version 8.1 6/6/93.  Type ? for help.
"/var/mail/foo": 1 message 1 new
>N  1 MAILER-DAEMON@davebo  Mon Nov 24 14:04  67/2465  "Mail Delivery Status Report"
? s
No file specified: using MBOX.
"/home/users/foo/mbox" [New file]
? q
$

Now, you should have a file called mbox in your $HOME directory. Take a look at this file, and see what it says. I got this:

$ vi $HOME/mbox

Enclosed is the mail delivery report that you requested.

                   The mail system

<[email protected]>: delivery via
    mail.foo.com[XX.XX.XX.XX]:25: host
    mail.foo.com[XX.XX.XX.XX] refused to talk to me: 554
     -Please submit an unblock request 
    <http://x.co/rblbounce>

Looks like I'm blocked.

Sign up to request clarification or add additional context in comments.

Comments

0

With the syntax you use, mail is certainly waiting for you to type a message on standard input, as suggested in the comments. You could:

--type a message yourself after the command, terminating with a C-d

--Use syntax like this to write a string to the body:

mail -s "Subject" [email protected] <<< "Hello"

--Different syntax

echo "Hello" | mail -s "Subject" [email protected]

Especially if you are going to be sending mail in a script, you would want to consider either of the last two approaches.

5 Comments

hey alright this actually i believe submits it but i get no message to the email i am testing to. It is possible to send to a live.ca domain right?
There should be no problem sending to specific domains. You could also try adding the -v flag to the mail command and then view the generated mail delivery report.
oh ok thanks. i suppose it just takes time to send the mail. and how would i know which email it would be sent from? the machine will use its own email right? sorry if stupid question
and how do i check the mail delivery reports? i tried looking around cant seem to find it
hey i typed in mail in the command line and just noticed this: <aimsupp@domain>: Host or domain name not found. Name service error for name=domain type=MX: Host not found, try again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.