0

I want to pipe output from the curl command to the email however I receive an empty attachment file when I run this script:

#!/bin/bash

STATUS=$(curl -I example.com)

echo $STATUS | mail -s "Test" [email protected]

I've also tried to group into a block but this also didn't work:

#!/bin/bash    
{
curl -I example.com
} | mail -s "Test" [email protected]

OS: Red Hat 6.3

# mail -V
12.4 7/29/08
2
  • Curriously, your commands seem correct (both). What version of mail are you using, try this in a freed environnement. Commented Dec 15, 2012 at 8:58
  • Question updated with the mail version Commented Dec 15, 2012 at 9:19

2 Answers 2

2

Assuming your mail binary is this one, try adding the option -a -.

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

1 Comment

Thanks for reply but it didn't help I'm afraid
1

Edited:

Try simply

curl -SIo /dev/stdout example.com 2>/dev/null | mail -s "Test" [email protected]

Well, if it's a mail problem. Your description seem strange, which version of mail are you using?

or maybe asking for sendmail directly:

/usr/sbin/sendmail [email protected] < <(
    echo $'From: [email protected]\nTo: [email protected]\nDate: '$(
        env LANG=C date +%c)$'\nSubject: Test'
    echo
    curl -SIo /dev/stdout 2>/dev/null example.com)

You could even build some more sophisticated job:

MyVar="$(curl -SIo /dev/stdout 2>/dev/null example.com)"
/usr/sbin/sendmail [email protected] < <(
    echo $'From: [email protected]\nTo: [email protected]\nDate: '$(
        env LANG=C date +%c)$'\nSubject: Server resp: '${MyVar%%$'\r'*}
    echo
    echo "$MyVar")

So you could have the initial server response in the subject.

This is not a reserved sendmail feature!

I was using this kind of method from many years, first under sendmail, but this work with the same syntax (ie calling ../sbin/sendmail binary or wrapper directly, with a formated mail as header + empty line + body ) with all MTA I've ever used upto now (sendmail, qmail, postfix, exim).

Nota Some (old) MTA complain about presence (or not) of a CR or \r at end of lines, maybe some sed 's/$/\r/' could help.

8 Comments

Hi, this also attached an empty file. I would like to pipe this into message body.
Did you need only the header or the output of curl (transfer stats) too?
This also creates an attachment. Basically I just monitor a website and if it's down I would like to know the header output from curl but it looks like it's not easy to pipe it into message body and I'm not sure why.
Yes, curl -SIo /dev/stdout 2>/dev/null www.test.com | mail -s test myself work fine, but I'm Debian user, on squeeze, with bash 4.1.5(1)-release and mail is coming with my version of Exim (not postfix). At all, I know this work under postfix too!
@HTF I'v tried now, under postfix exactly the same syntax as my last sample whith MyVar... and using sendmail sbin command. This work perfectly : I've recieved a mail with subject: Server resp: HTTP/1.1 200 OK (postfix version 2.7.1-1+squeeze1)
|

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.