
Send Mail from a Command Prompt
Tutorial Information
|
Name: |
Send Mail from a Command Prompt |
Submitter: |
NickTheGreek |
Category: |
Windows Tutorials |
|
Submitted: |
4 Jul 2008 |
Updated: |
4 Jul 2008 |
Views: |
471 |
Rating: |
This tutorial is unrated. |
|
|
|
Description: Send Mail from a Command Prompt |
Tutorial Instructions
|
I'm often asked how to send an email from a command prompt. This would be great thing. If I just wanted to send off a quick message, why bother opening up a complex program like Outlook. Or suppose I had a batch job running overnight that built a report as an Excel spreadsheet. I could add code to email the report and any other message to all administrators and managers.
Fortunately there are several command line utilities that will send mail using SMTP (Simple Mail Transfer Protocol). One of the most popular is POSTIE which you can find at http://www.infradig.com. The command line version is free for personal use and not much more for commercial use. There is also a full version with support for IMAP4, LDAP, List Server processing, HTML-mail and SSL/TLS.
When you look at the README file, the number of options for sending mail seem overwhelming. But you really only need a few basic items to get most jobs done. After you download postie and extract it to your directory of choice, to send your first message open a command prompt and type:
postie -host:YourSMTPMailServer -to:billg@microsoft.com -from:buzzbreath@brainbuzz.com -s:"Put your subject here" -msg:"What do you have to say for yourself?"
and press Enter. Your message will be mailed to Bill G. with a subject of "Put your subject here" and the body will say "What do you have to say for yourself?" What could be easier? Remember to use "" around phrases or sentences, generally in your subject and message.
To attach a file, use the -a switch like this:
postie -host:YourSMTPMailServer -to:billg@microsoft.com -from:buzzbreath@brainbuzz.com -s:"Put your subject here" -msg:"What do you have to say for yourself?" -a:"e:\my docs\stock\monopoly.zip"
If you want to send your message to multiple people, you can use separate -to: switches or a single switch separating addresses with commas. You can also use -cc: and -bcc: just as you normally would in any other email program.
Suppose you wanted to make your overnight report the body of the message and not just an attachment? Use the -file: switch. Just be sure to use the full path.
postie -host:YourSMTPMailServer -to:billg@microsoft.com -from:buzzbreath@brainbuzz.com -s:"Put your subject here" -file:"e:\my docs\stock\net worth.txt"
Finally, there are probably many settings, such as your email host and from address that aren't going to change. Why bother typing them out everytime? Postie includes a text configuration file that you can edit to reflect your specific configuration. Here is a stripped down version we might use:
############### POSTIE Configuration File ##################### # # Uncomment and edit lines to suit... # # Put NO in font of a keyword to negate it. # POSTIE_PATH e:\postie FROM jhicks@2dogs.com SMTP_HOST mail.2dogs.com NORMAL REPLY_TO: LANAdmin@2dogs.com LOG e:\logs\postie.log # End of file
Save file file as POSTIE.TXT in the same directory as POSTIE.EXE. To take advantage of it, we will use the -config switch. Here's how we might use it in our overnight batch file.
@echo off ::Overnight.bat set POSTIE=e:\utils\mail\postie\postie.exe set REPORT=\\LANSRV01\report$\nightly.xls set SUBJ="Overnight Report" set SENDTO=AManager@2dogs.com,BABoss@2dogs.com,SuperTech@2dogs.com set MSG="Here is the overnight progress report. Please report any errors to me as soon as possible."
REM Run overnight processes Call f:\overnight\BackIt.bat Call f:\overnight\CheckIt.bat Call f:\overnight\CleanIt.bat
%POSTIE% -to:%SENDTO% -s:%SUBJ% -msg:%MSG% -a:%REPORT% -config
REM Clear out variables set POSTIE= set REPORT= set SUBJ= set SENDTO= set MSG=
:EOF |
Comments
|
There have been no comments made as of yet. Why not be the first?
|