1

I am trying to write a shell script, where emails get piped to (by an email forward in cpanel).

The shell script will then post the entire email to a url using curl.

The script looks like this:

curl -d "param=$1" http://localhost/stuff/

And the forward looks like this:

|/home/usr/script/curlthis.sh

This is only sort of working.

The email gets bounced back even though the curl posts to the url successfully. (it looks like only part of the email is getting posted, but I am not 100% sure)

I have been told the email bounces because I am not reading the stdin, but I am not sure why I need to do that and why I cannot use $1?

How can I read the entire contents of the pipe (then post it using curl), and will that stop the mail server from bouncing it back?

EDIT

Using the answer below here is what I came up with:

#!/bin/bash
m=$(cat -) 
escapedm="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$m")"
curl -silent -G -d "param=$escapedm" http://localhost/stuff/ 2>&1 >/dev/null

This part:

2>&1 >/dev/null

is shockingly important. If you don't redirect the stdout/err to null then the email gets bounced back for whatever reason.

jhnclvr
  • 9,137
  • 5
  • 50
  • 55

1 Answers1

2

Your mail is being passed to the script as a stream on stdin, and not as a parameter ($1). Note that your forward script begins with a pipe, and that's the mechanism passing the mail into your script.

So you should be able to read this in your shell (bash?) using the read statement. See this SO answer for more details.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440