views:

807

answers:

9

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.

Thanks in advance.

+7  A: 
$ echo "This is the email body" | mail -s "This is the subject" [email protected]

Alternatively:

$ cat | mail -s "A few lines off the top of my head" [email protected]
This is where my
multiline
message would go
^D

^D - means press control-D

aryeh
ridiculously useless use of cat :)
hop
+5  A: 

You can use mail:

$mail -s <subject> <recipients>

You then type your message and end it with a line that has only a period. This signals you are done and sends the message.

You can also pipe your email in from STDIN and it will be sent as the text of an email:

$<mail-generating-program> | mail -s <subject> <recipients>

One small note with this approach - unless your computer is connected to the internet and your DNS settings are set properly, you won't be able to receive replies to your message. For a more robust command-line program you can link to your POP or IMAP email account, check out either pine or mutt.

Kyle Cronin
+3  A: 

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.

Steven Murawski
A: 

@Kyle: Thanks for the update. Which would you say is simpler or easier to learn: pine or mutt? Is there any resource that outlines the pros and cons of each?

@Steve: Thanks for the Windows alternatives! At first glance it looks like blat was only for Usenets but when you look deeper into the website it is much more powerful.

Joseph Pecoraro
A: 

@Joseph Pecoraro I was kidding around, but I figured not everyone has a sense of humor... :) Cheers!

aryeh
+2  A: 

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!

Brendan
+1  A: 

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?

Frank Krueger
+2  A: 

Here is a Power Shell example of a script to send email:

$smtp = new-object Net.Mail.SmtpClient("mail.example.com")

if( $Env:SmtpUseCredentials -eq "true" ) {
    $credentials = new-object Net.NetworkCredential("username","password")
    $smtp.Credentials = $credentials
}
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "[email protected]"
$objMailMessage.To.Add("[email protected]")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"

$smtp.send($objMailMessage)
Philibert Perusse
+1  A: 

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:

echo To: [email protected], [email protected] >> the.mail
echo From: [email protected] >> the.mail
echo Subject: This is a SENDMAIL notification >> the.mail
echo Hello World! >> the.mail
echo This is simple enough. >> the.mail
echo .>> the.mail

Sending the file:

\usr\lib\sendmail.exe -t < the.mail

type the.mail | C:\Projects\Tools\sendmail.exe -t
Philibert Perusse