<p>I would like to quickly send email from the command line.  I realize there are probably a number of different ways to do this.</p>

<p>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.</p>

[{'answer_id': 36499, 'author': 'aryeh', 'author_id': 3288, 'author_profile': 'https://Stackoverflow.com/users/3288', 'pm_score': 4, 'selected': False, 'text': '<pre><code>$ echo "This is the email body" | mail -s "This is the subject" me@email.com\n</code></pre>\n\n<p>Alternatively:</p>\n\n<pre><code>$ cat | mail -s "A few lines off the top of my head" me@here.com\nThis is where my\nmultiline\nmessage would go\n^D\n</code></pre>\n\n<p>^D - means press <kbd>ctrl</kbd>+<kbd>d</kbd></p>\n'}, {'answer_id': 36500, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 4, 'selected': True, 'text': '<p>You can use mail:</p>\n\n<pre><code>$mail -s &lt;subject&gt; &lt;recipients&gt;\n</code></pre>\n\n<p>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.</p>\n\n<p>You can also pipe your email in from STDIN and it will be sent as the text of an email:</p>\n\n<pre><code>$&lt;mail-generating-program&gt; | mail -s &lt;subject&gt; &lt;recipients&gt;\n</code></pre>\n\n<p>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 <a href="http://www.washington.edu/pine/" rel="noreferrer">pine</a> or <a href="http://www.mutt.org/" rel="noreferrer">mutt</a>.</p>\n'}, {'answer_id': 36517, 'author': 'Steven Murawski', 'author_id': 1233, 'author_profile': 'https://Stackoverflow.com/users/1233', 'pm_score': 2, 'selected': False, 'text': '<p>If you are looking to do this from a Windows command line, there is a tool called <a href="http://www.blat.net/" rel="nofollow noreferrer">blat</a> that can be used from a CMD prompt.</p>\n\n<p>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 <a href="http://poshcode.org/180" rel="nofollow noreferrer">PowerShell Community Script Repository</a>.</p>\n'}, {'answer_id': 36548, 'author': 'Brendan', 'author_id': 199, 'author_profile': 'https://Stackoverflow.com/users/199', 'pm_score': 2, 'selected': False, 'text': '<p>IIRC you\'ll also have to configure a mail transfer agent (MTA) to use <code>mail</code> or most email libraries. <a href="http://www.sendmail.org/" rel="nofollow noreferrer">Sendmail</a> is the most well known but is a real pig when it comes to configuration. <a href="http://www.exim.org/" rel="nofollow noreferrer">Exim</a>, <a href="http://www.qmail.org/" rel="nofollow noreferrer">Qmail</a> and <a href="http://www.postfix.org/" rel="nofollow noreferrer">Postfix</a> are all popular alternatives that are a bit more modern.</p>\n\n<p>There are also more lightweight MTAs that are only able to send out mail, not receive it: nullmailer, mstmp, ssmtp, etc.</p>\n\n<p>Postfix is default for Ubuntu. <a href="https://help.ubuntu.com/community/Postfix" rel="nofollow noreferrer">This wiki article</a> describes how to configure it - be sure to only allow forwarding from your local address!</p>\n'}, {'answer_id': 36560, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 1, 'selected': False, 'text': '<p>If you want to invoke an email program, then see this article:</p>\n\n<p><a href="https://stackoverflow.com/questions/17373/how-do-i-open-the-default-mail-program-with-a-subject-and-body-in-a-cross-platf">How do I open the default mail program with a Subject and Body in a cross-platform way?</a></p>\n'}, {'answer_id': 804411, 'author': 'Philibert Perusse', 'author_id': 7984, 'author_profile': 'https://Stackoverflow.com/users/7984', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a Power Shell example of a script to send email:</p>\n\n<pre><code>$smtp = new-object Net.Mail.SmtpClient("mail.example.com")\n\nif( $Env:SmtpUseCredentials -eq "true" ) {\n    $credentials = new-object Net.NetworkCredential("username","password")\n    $smtp.Credentials = $credentials\n}\n$objMailMessage = New-Object System.Net.Mail.MailMessage\n$objMailMessage.From = "script@mycompany.com"\n$objMailMessage.To.Add("you@yourcompany.com")\n$objMailMessage.Subject = "eMail subject Notification"\n$objMailMessage.Body = "Hello world!"\n\n$smtp.send($objMailMessage)\n</code></pre>\n'}, {'answer_id': 2477883, 'author': 'Philibert Perusse', 'author_id': 7984, 'author_profile': 'https://Stackoverflow.com/users/7984', 'pm_score': 2, 'selected': False, 'text': '<p>You can also use this <a href="http://glob.com.au/sendmail/" rel="nofollow noreferrer">sendmail</a> 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.</p>\n\n<p>Composing the email:</p>\n\n<pre><code>echo To: you@example.com, me@example.com &gt;&gt; the.mail\necho From: them@example.com &gt;&gt; the.mail\necho Subject: This is a SENDMAIL notification &gt;&gt; the.mail\necho Hello World! &gt;&gt; the.mail\necho This is simple enough. &gt;&gt; the.mail\necho .&gt;&gt; the.mail\n</code></pre>\n\n<p>Sending the file:</p>\n\n<pre><code>\\usr\\lib\\sendmail.exe -t &lt; the.mail\n\ntype the.mail | C:\\Projects\\Tools\\sendmail.exe -t\n</code></pre>\n'}, {'answer_id': 6938756, 'author': 'jimmy', 'author_id': 878221, 'author_profile': 'https://Stackoverflow.com/users/878221', 'pm_score': 1, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>#! /usr/bin/php\n&lt;?php\n\nif ($argc &lt; 3) {\n    echo "Usage: " . basename($argv[0]) . " TO SUBJECT [CC]\\n";\n    exit(1);\n}\n\n$message = file_get_contents(\'php://stdin\', \'r\');\n$headers = $argc &gt;= 4 ? "Cc: $argv[3]\\r\\n" : null;\n\n$ret = mail($argv[1], $argv[2], $message, $headers);\n\nexit($ret ? 0 : 1);\n</code></pre>\n\n<p>Then invoke as follows:</p>\n\n<pre><code>mail me@example.com test &lt; message\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/36498', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/792/']