I would like to quickly send email from the command line. I realize there are probably a number of different ways to do this.
I'm looking for a simple way to do this from a linux terminal (likely a bash shell but anything should do) and an alternative way to do this on Windows. I want to be able to whip up an email right on the command line or have the flexibility to pipe the message into the command line program. How would you go about doing this? If you have small scripts that would be fine as well.
If you are looking to do this from a Windows command line, there is a tool called blat that can be used from a CMD prompt.
It is a bit more fun from PowerShell. Since PowerShell has access to the .NET Framework, you can use the classes from System.Net.Mail to send email. There is an example script on the PowerShell Community Script Repository.
You can also use this sendmail version for windows. It is very simple to use, standard UNIX-like behavior. Fast. Does not need any installation, just call the EXE wherever it is located on your system.
Composing the email:
Sending the file:
Here is a Power Shell example of a script to send email:
IIRC you'll also have to configure a mail transfer agent (MTA) to use
mail
or most email libraries. Sendmail is the most well known but is a real pig when it comes to configuration. Exim, Qmail and Postfix are all popular alternatives that are a bit more modern.There are also more lightweight MTAs that are only able to send out mail, not receive it: nullmailer, mstmp, ssmtp, etc.
Postfix is default for Ubuntu. This wiki article describes how to configure it - be sure to only allow forwarding from your local address!
If you want to invoke an email program, then see this article:
How do I open the default mail program with a Subject and Body in a cross-platform way?
If you are on a Linux server, but mail isn't available (which can be the case on shared servers), you can write a simple PHP / Perl / Ruby (depending on what's available) script to do the same thing, e.g. something like this:
Then invoke as follows: