37

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

asked Nov 13, 2012 at 15:54
7
  • 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. Commented Nov 13, 2012 at 16:09
  • Well if you add this as an answer I might accept it... :) Commented Nov 13, 2012 at 16:18
  • ah, no I don't, sorry. Please regard my edit. Commented Nov 13, 2012 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. Commented Nov 13, 2012 at 16:48
  • 1
    A slightly different approach would be to use the mail command (sometimes called bsdmail) instead of sendmail directly. mail takes an optional -s subject option, e.g.: ifconfig -a | mail -s 'Current ifconfig output' [email protected] Commented Aug 16, 2015 at 21:41

5 Answers 5

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

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

answered Nov 13, 2012 at 16:51
4
  • 3
    Simple, functional and demonstrated. +1 Commented Dec 30, 2013 at 14:14
  • Is there an easy way to do it without the break return? Commented Jul 21, 2015 at 16:18
  • 3
    @VenomFangs Use printf instead of the second echo and you should be fine Commented Jul 24, 2015 at 9:47
  • 2
    >Is there an easy way to do it without the break return? - echo -n Commented Apr 6, 2018 at 11:00
9

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!
answered Nov 19, 2012 at 4:39
0
6

This is inspired by Claudius answer.

If you don't want a break return between your outputs, add the -n param. This will look like:

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

Which will give:

21

answered Jul 21, 2015 at 16:21
2
  • 1
    Actually, no it won't. It gives "2\n1". You have to attach the "-n" to the second echo. Still +1. If you Correct that, I might accept it. I am not sure what's fair, since Claudius contributed the most to the solution. Commented Jul 23, 2015 at 14:36
  • Sorry, think I put the -n in the wrong place. Anyway, The update should give "21\n" if you want to get rid of the \n then just add a -n before the 1. Commented Jul 23, 2015 at 15:20
3

You can prepend to piped output using process substitution:

$ echo "mail content" | cat <(echo "Subject: All that matters") -
Subject: All that matters
mail content
answered Sep 12, 2019 at 1:05
1

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.

answered Nov 13, 2012 at 16:54
2
  • 1
    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. Commented Nov 13, 2012 at 16:56
  • Immediately after posting my answer... I read yours & like it better. Good points. Commented Nov 13, 2012 at 16:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.