Redirecting output to email

3

I am aware that in Windows you can use the following snippet to redirect the output of a command line tool to the clipboard:

mytool | clip

Also, it can be done with files:

clip < myFile.txt

I also know Windows has several other devices/programs you can use as output / input for these redirections (probably the printer is the most famous of them).

However, is there something to send the text to the default email program?

I would love to find that there is something like: mytool | email. Is there something alike?

Alpha

Posted 2012-09-11T15:40:24.340

Reputation: 306

2Nitpicking: clip in your examples is not a device, it's a program named clip.exe – wmz – 2012-09-11T16:25:23.770

@wmz Everything counts! Thanks, I'll correct it. – Alpha – 2012-09-11T18:48:01.497

Answers

3

You can use the mailto URL scheme to construct an email link containing a predefined message body, then call it from the command line, which should open it in your default email client. For example:

start mailto:?body=Your%20message%20here

Just customise the part after body= at the end. I've read that the text needs to be URL-encoded, but at least in Windows Live Mail 2012 that doesn't seem to be necessary. I didn't try with longer texts or special symbols, though, and I don't have any other email clients (Outlook, Thunderbird, etc.) to test with.

For constructing that command, you could write a simple batch file, such as the following one:

@echo off
set /p text=
if "%text%" == "" goto error
start mailto:?body=%text%
goto end
:error
echo No argument provided!
:end

However, with that you do need the text to be URL-encoded, so you might want to do the whole thing in PowerShell. To URL-encode a string, you can use this:

[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
[System.Web.HttpUtility]::UrlEncode("Your message here")

(The first line is needed to load the System.Web namespace where the HttpUtility.UrlEncode function lives.)

In fact, if your command (or whatever you have that produces the text you want to redirect) isn't too complex, you can just write the following one-liner in PowerShell (replacing <your-command-here> with the actual command you're using):

start ("mailto:?body={0}" -f [System.Web.HttpUtility]::UrlEncode(<your-command-here>))

Note: this assumes that you just want to redirect text to your default email client. If you want to actually send emails from the command line, then look at @allquixotic's answer.

Indrek

Posted 2012-09-11T15:40:24.340

Reputation: 21 756

This is awesome! Would this work with multiple-lined outputs? (Maybe the new lines are URL encoded... is each line processed in different calls?) – Alpha – 2012-09-11T18:50:35.557

1

Here is a possible software solution although be aware that you can, in principle, write a script of any sort (Ruby, VBscript, Bash, or a program from scratch) to do something similar. There are a variety of ways you can do it, including command line options for the To: and Subject lines, and so on. But that "fake sendmail" just might do what you want.

allquixotic

Posted 2012-09-11T15:40:24.340

Reputation: 32 256