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?

  • 2
    Nitpicking: clip in your examples is not a device, it's a program named clip.exe – wmz Sep 11 '12 at 16:25
  • @wmz Everything counts! Thanks, I'll correct it. – Alpha Sep 11 '12 at 18:48

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
echo No argument provided!

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.

| improve this answer | |
  • 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 Sep 11 '12 at 18:50

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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