How do I Send Email from the Command Line?

2019-04-19 06:37发布

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.

8条回答
Bombasti
2楼-- · 2019-04-19 07:03

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-04-19 07:05

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: you@example.com, me@example.com >> the.mail
echo From: them@example.com >> 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
查看更多
We Are One
4楼-- · 2019-04-19 07:11

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 = "script@mycompany.com"
$objMailMessage.To.Add("you@yourcompany.com")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"

$smtp.send($objMailMessage)
查看更多
贼婆χ
5楼-- · 2019-04-19 07:21

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!

查看更多
聊天终结者
6楼-- · 2019-04-19 07:25

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?

查看更多
Emotional °昔
7楼-- · 2019-04-19 07:25

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:

#! /usr/bin/php
<?php

if ($argc < 3) {
    echo "Usage: " . basename($argv[0]) . " TO SUBJECT [CC]\n";
    exit(1);
}

$message = file_get_contents('php://stdin', 'r');
$headers = $argc >= 4 ? "Cc: $argv[3]\r\n" : null;

$ret = mail($argv[1], $argv[2], $message, $headers);

exit($ret ? 0 : 1);

Then invoke as follows:

mail me@example.com test < message
查看更多
登录 后发表回答