Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to use sendmail to send me stuff and want to do it in a oneliner.

echo "mail content" | sendmail emailataddres.com 

Sends it without subject.

The subject line must come before the Mail content, so I am looking for something along the lines of:

echo "mail content" | prepend "Subject: All that matters" | sendmail emailataddres.com 

sed and awk tend to be really awkward to use and remember.

EDIT:Just to clarify: echo "Mail content" is just an illustrating example. I need to be able to prepend stuff to stdout streams from any source. e.g.: ifconfig, zcat, etc..

share|improve this question
    
echo -e "Subject: All that matters\nmail content"? Or more platform agnostic: printf 'Subject: %s\n%s\n' "All that matters" "mail content". You could also write a small script that just takes the two string arguments to make an even simpler one-liner. –  Daniel Andersson Nov 13 '12 at 16:09
    
Well if you add this as an answer I might accept it... :) –  AndreasT Nov 13 '12 at 16:18
    
ah, no I don't, sorry. Please regard my edit. –  AndreasT Nov 13 '12 at 16:22
    
What should prepend do? In order to know how to prepend something, you have to wait until the upstream command (e.g. echo) sends the EOF, so that you can insert your data ahead of it in the stream before passing it to stdout to get piped to sendmail. Sounds like a task for a shortish Ruby or Python script. –  ÃŁŁǫǛȉЖΦΤїҪ Nov 13 '12 at 16:48
    
I don't have to wait. It can be inserted at the beginning of the stream. –  AndreasT Nov 13 '12 at 16:56

3 Answers 3

$ echo 1 | (echo 2 && cat)
2
1

I am pretty sure that there is a nicer solution, but this should do.

share|improve this answer
    
Simple, functional and demonstrated. +1 –  Hennes Dec 30 '13 at 14:14

Either use what Claudius said or make your own:

~/bin/prepend:

#!/bin/sh
echo -en "$@"
cat -

e.g.:

$ echo "Splendid SUPANINJA! Let's do it!" |\
     prepend "Subject: Venetian Snares\n"

Subject: Venetian Snares
Splendid SUPANINJA! Lets do it!

share|improve this answer

From the pieces I've gathered... you could do something like this:

echo "Subject: All that matters
`echo "mail content"`" | sendmail blah@blahblah

Notice that I did not close the quotes on the first line... because not all shells translate the \n into a newline character... but I have yet to find one that wont process an actual newline inside the quotes.

When a command is enclosed in the ` character, it will be executed and the output will be injected in-place. Keep in mind that this bit of code is a bit dangerous as it is possible to inject additional commands inline that could easily compromise your system...

*edit* Following advise of Claudius, a cleaner option would look like this:

echo -e "Subject: All that matters \n $(echo "mail content") |sendmail blah@blahblah

Even with that template, it could be exploited.

share|improve this answer
    
It is usually nicer to use $() rather than `` as it is supposed to be more portable (and also more readable). Furthermore, you can use echo’s -e option to make it translate newlines (echo -e "1\n2") or just use printf as suggested by Daniel in the first comment. –  Claudius Nov 13 '12 at 16:56
    
Immediately after posting my answer... I read yours & like it better. Good points. –  TheCompWiz Nov 13 '12 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.