Advertisement
Advertisement


How do I Send Email from the Command Line?


Question

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.

2020/04/10
1
18
4/10/2020 8:43:47 AM

Accepted Answer

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.

2008/08/31
9
8/31/2008 12:37:17 AM


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
2010/03/19

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.

2008/08/31

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!

2009/02/18

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)
2009/04/29

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?

2017/05/23