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..
5 Answers 5
$ echo 1 | (echo 2 && cat)
2
1
I am pretty sure that there is a nicer solution, but this should do.
-
3Simple, functional and demonstrated. +1Hennes– Hennes2013年12月30日 14:14:21 +00:00Commented Dec 30, 2013 at 14:14
-
Is there an easy way to do it without the break return?VenomFangs– VenomFangs2015年07月21日 16:18:19 +00:00Commented Jul 21, 2015 at 16:18
-
3@VenomFangs Use
printfinstead of the secondechoand you should be fineTrendfischer– Trendfischer2015年07月24日 09:47:09 +00:00Commented Jul 24, 2015 at 9:47 -
2>Is there an easy way to do it without the break return? -
echo -nkyb– kyb2018年04月06日 11:00:37 +00:00Commented Apr 6, 2018 at 11:00
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!
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
-
1Actually, 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.AndreasT– AndreasT2015年07月23日 14:36:21 +00:00Commented Jul 23, 2015 at 14:36
-
Sorry, think I put the
-nin 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.VenomFangs– VenomFangs2015年07月23日 15:20:06 +00:00Commented Jul 23, 2015 at 15:20
You can prepend to piped output using process substitution:
$ echo "mail content" | cat <(echo "Subject: All that matters") -
Subject: All that matters
mail content
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.
-
1It is usually nicer to use
$()rather than `` as it is supposed to be more portable (and also more readable). Furthermore, you can useecho’s-eoption to make it translate newlines (echo -e "1\n2") or just use printf as suggested by Daniel in the first comment.Claudius– Claudius2012年11月13日 16:56:28 +00:00Commented Nov 13, 2012 at 16:56 -
Immediately after posting my answer... I read yours & like it better. Good points.TheCompWiz– TheCompWiz2012年11月13日 16:58:01 +00:00Commented Nov 13, 2012 at 16:58
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.prependdo? 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.mailcommand (sometimes called bsdmail) instead ofsendmaildirectly.mailtakes an optional-s subjectoption, e.g.:ifconfig -a | mail -s 'Current ifconfig output' [email protected]