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.
我正在寻找一种简单的方法来从Linux终端(可能是bash shell,但任何事情都应该这样做)以及在Windows上执行此操作的另一种方法。我希望能够在命令行上发送电子邮件,或者可以灵活地将消息传递到命令行程序中。你会怎么做呢?如果你有小脚本也可以。
8 个解决方案
#1
8
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:
您也可以从STDIN管道发送电子邮件,它将作为电子邮件的文本发送:
$<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.
使用此方法的一个小注意事项 - 除非您的计算机已连接到互联网并且您的DNS设置设置正确,否则您将无法收到对邮件的回复。要获得更强大的命令行程序,您可以链接到您的POP或IMAP电子邮件帐户,请查看pine或mutt。
#2
11
$ echo "This is the email body" | mail -s "This is the subject" me@email.com
Alternatively:
$ cat | mail -s "A few lines off the top of my head" me@here.com
This is where my
multiline
message would go
^D
^D - means press ctrl+d
^ D - 表示按ctrl + d
#3
4
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.
您也可以将此sendmail版本用于Windows。它使用非常简单,类似于UNIX的标准行为。快速。不需要任何安装,只需在系统上的任何位置调用EXE即可。
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
#4
3
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.
如果您希望从Windows命令行执行此操作,则可以在CMD提示符中使用名为blat的工具。
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.
从PowerShell获得更多乐趣。由于PowerShell可以访问.NET Framework,因此您可以使用System.Net.Mail中的类来发送电子邮件。 PowerShell社区脚本存储库上有一个示例脚本。
#5
2
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.
IIRC您还必须配置邮件传输代理(MTA)以使用邮件或大多数电子邮件库。 Sendmail是最知名的,但在配置方面却是真正的猪。 Exim,Qmail和Postfix都是比较现代的流行替代品。
There are also more lightweight MTAs that are only able to send out mail, not receive it: nullmailer, mstmp, ssmtp, etc.
还有更轻量级的MTA只能发送邮件,而不是接收邮件:nullmailer,mstmp,ssmtp等。
Postfix is default for Ubuntu. This wiki article describes how to configure it - be sure to only allow forwarding from your local address!
Postfix是Ubuntu的默认设置。这篇wiki文章介绍了如何配置它 - 确保只允许从您的本地地址转发!
#6
2
Here is a Power Shell example of a script to send email:
以下是发送电子邮件的脚本的Power Shell示例:
$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)
#7
1
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?
如何以跨平台方式使用主题和正文打开默认邮件程序?
#8
1
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:
如果您在Linux服务器上,但邮件不可用(共享服务器上可能是这种情况),您可以编写一个简单的PHP / Perl / Ruby(取决于可用的)脚本来执行相同的操作,例如:像这样的东西:
#! /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
#1
8
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:
您也可以从STDIN管道发送电子邮件,它将作为电子邮件的文本发送:
$<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.
使用此方法的一个小注意事项 - 除非您的计算机已连接到互联网并且您的DNS设置设置正确,否则您将无法收到对邮件的回复。要获得更强大的命令行程序,您可以链接到您的POP或IMAP电子邮件帐户,请查看pine或mutt。
#2
11
$ echo "This is the email body" | mail -s "This is the subject" me@email.com
Alternatively:
$ cat | mail -s "A few lines off the top of my head" me@here.com
This is where my
multiline
message would go
^D
^D - means press ctrl+d
^ D - 表示按ctrl + d
#3
4
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.
您也可以将此sendmail版本用于Windows。它使用非常简单,类似于UNIX的标准行为。快速。不需要任何安装,只需在系统上的任何位置调用EXE即可。
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
#4
3
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.
如果您希望从Windows命令行执行此操作,则可以在CMD提示符中使用名为blat的工具。
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.
从PowerShell获得更多乐趣。由于PowerShell可以访问.NET Framework,因此您可以使用System.Net.Mail中的类来发送电子邮件。 PowerShell社区脚本存储库上有一个示例脚本。
#5
2
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.
IIRC您还必须配置邮件传输代理(MTA)以使用邮件或大多数电子邮件库。 Sendmail是最知名的,但在配置方面却是真正的猪。 Exim,Qmail和Postfix都是比较现代的流行替代品。
There are also more lightweight MTAs that are only able to send out mail, not receive it: nullmailer, mstmp, ssmtp, etc.
还有更轻量级的MTA只能发送邮件,而不是接收邮件:nullmailer,mstmp,ssmtp等。
Postfix is default for Ubuntu. This wiki article describes how to configure it - be sure to only allow forwarding from your local address!
Postfix是Ubuntu的默认设置。这篇wiki文章介绍了如何配置它 - 确保只允许从您的本地地址转发!
#6
2
Here is a Power Shell example of a script to send email:
以下是发送电子邮件的脚本的Power Shell示例:
$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)
#7
1
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?
如何以跨平台方式使用主题和正文打开默认邮件程序?
#8
1
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:
如果您在Linux服务器上,但邮件不可用(共享服务器上可能是这种情况),您可以编写一个简单的PHP / Perl / Ruby(取决于可用的)脚本来执行相同的操作,例如:像这样的东西:
#! /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