Oracle DBA Tips Corner |
|
Sending and Receiving Email in Windows From Scripts
by Jeff Hunter, Sr. Database Administrator
Contents
Overview
Blat provides the ability to send the contents of a file in an
e-mail message using the SMTP or NNTP protocol. Blat is useful for
creating scripts where mail has to be sent automatically (CGI,
backups, etc.), or just as a quick way to send a file or message
quickly from the command line. It will store relevant configuration
details in the registry for ease of use. Optionally, blat can also
attach multiple binary files to your message.
Getmail for Windows is a command line tool used to automatically
download mail, including the ability to automatically extract
binary attachments. Getmail can be used to automatically extract
MIME encoded or UU encoded binary files as it downloads your mail.
It's the perfect companion to blat!
Downloading
As of June 2003, Blat management has moved to
SourceForge.net
Configuring SMTP
For detailed instructions on installing and configuring SMTP
on the Microsoft Windows platform, see the following:
Using Blat
Usage
NOTE: You must specify a filename/pattern,
"-attach .\*.*" will send all files in current folder.
"-attach ." will not send anything AND WILL NOT GIVE YOU ANY WARNING.
Using Getmail for Windows
For all of the examples in this section, I created a folder named "C:\>Inbox"
that will be used for all downloaded (and extracted) email:
Usage
I now simply include the profile (-p) parameter:
All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter
and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express,
prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.
I have made every effort and taken great care in making sure that the material included on my web site is technically accurate,
but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from
relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.
This articles presents two easy-to-use methods for sending and
receiving email using scripts in the Microsoft Windows platform.
This is made possible with two public domain console utilities
for Windows named "Blat" and "Getmail".
Blat
The official homepage for Blat can be found at
http://www.blat.net/.
Blat v2.6.2 - (03/30/2007)
Blat v2.5.0 - (09/18/2005)
Blat v2.4.0 - (01/18/2005)
Blat v1.9.4 - (12/11/2001)
Getmail
The official homepage for Getmail for Windows can be found at
http://www.interlog.com/~tcharron/getmail.html.
Getmail v1.33 - (04/12/2002)
Updating your PATH Variable
Both Blat and Getmail do not provide (nor does it need) a GUI installation program. Simply unzip
the files to a directory and make sure to include those directories in your
PATH
environment variable:
PATH=%PATH%;C:\Blat240\full;C:\Getmail
Blat requires access to a Simple Mail Transfer Protocol (SMTP) outgoing mail server. I performed all of my
testing on a Windows XP laptop which does not come configured with an SMTP server
by default. There are several ways around this.
The Blat utility is run by using the blat.exe command-line
utility. For all of the examples in this section, I will be using
an SMTP (outgoing) server that is already configured on my local laptop. While
the name of my laptop is melody.idevelopment.info (named after my wife
Melody Hunter) I will continue to use localhost when identify
my SMTP server.
To start, we can simply execute blat with no arguments:
C:\> blat
Blat v2.4 w/GSS encryption (build : Jan 15 2005 08:32:11)
Win32 console utility to send mail via SMTP or post to usenet via NNTP
by P.Mendes,M.Neal,G.Vollant,T.Charron,T.Musson,H.Pesonen,A.Donchey,C.Hyde
http://www.blat.net
syntax:
Blat <filename> -to <recipient> [optional switches (see below)]
Blat -install <server addr> <sender's addr> [<try>[<port>[<profile>]]] [-q]
Blat -profile [-delete | "<default>"] [profile1] [profileN] [-q]
Blat -h
Simple Example
Let's start with a simple example. Here, I want to simply
email a text file giving all required parameters on the command-line:
C:\> blat readme.txt -to jhunter@idevelopment.info -server localhost -f dba@bar.com
Blat v2.4 w/GSS encryption (build : Jan 15 2005 08:32:11)
Sending readme.txt to jhunter@idevelopment.info
Login name is dba@bar.com
Storing Default values in the Windows Registry
From the example above, I needed to provide the SMTP server (using the -server option) as well
as the sender's email address (the -f option). I can instead store these values
in the Windows registry and have them provided by default. To do this, I first need to update
the values in Windows registry using the -install option as follows:
C:\> blat -installSMTP localhost dba@bar.com
Blat v2.4 w/GSS encryption (build : Jan 15 2005 08:32:11)
SMTP server set to localhost on port 25 with user dba@bar.com, retry 1 time(s)
I can now reduce the number of arguments to just the file I want to send and
who I want to send it to:
C:\> blat readme.txt -to jhunter@idevelopment.info
Blat v2.4 w/GSS encryption (build : Jan 15 2005 08:32:11)
Sending readme.txt to jhunter@idevelopment.info
Login name is dba@bar.com
Binary Files
OK, so you want to send a binary file as an attachment. Not a problem.
The first example uses MIME Base 64 format while the second
example uses old UUEncode format:
C:\> blat check_green.gif -subject "Binary GIF File" -to jhunter@idevelopment.info -base64
C:\> blat check_green.gif -subject "Binary GIF File" -to jhunter@idevelopment.info -uuencode
Adding Subject Line
Need to add a subject line:
C:\> blat readme.txt -subject "A simple text file example" -to jhunter@idevelopment.info
Getting Rid of the Console Output
Need to get rid of that console output when sending a message?
C:\> blat readme.txt -subject "A simple text file example" -to jhunter@idevelopment.info -q
Override the Default Sender
Although I saved the default sender (and default SMTP server) in the registry,
I can easily override them:
C:\> blat readme.txt -subject "A simple text file example" -to jhunter@idevelopment.info -f TheNewMe@bar.com
Override the Default Sender But Leave Reply-To: and Sender: Lines
Use the -i parameter to replace the From: line, but leave
the Reply-To: and Sender: lines
C:\> blat readme.txt -subject "A simple text file example" -to jhunter@idevelopment.info -i TheNewMe@bar.com
Carbon Copies
Send carbon copies:
C:\> blat readme.txt -subject "Text File" -to jhunter@idevelopment.info -cc "admin@idevelopment.info,dba@idevelopment.info"
Sending Multiple File Attachments
Attach all files with extension "TXT" in folder "Reports"
on drive "C:".
C:\> blat readme.txt -subject "Report Files" -to jhunter@idevelopment.info -attach c:\Reports\*.txt
Identifing SMTP Server as Another Name
Tells the SMTP that this computer is called "proddb1":
C:\> blat readme.txt -subject "Production Files" -to jhunter@idevelopment.info -hostname proddb1
Getmail is a Windows 95/NT console utility used to download the contents of
a POP3 mailbox. It's ideal for scripts which need to get input from a mail message.
Using your language of choice, it allows you to add email capability to any
process (mail responders, or even lists). Configuration information can optionally be
saved in the registry.
C:\> mkdir C:\Inbox
C:\> cd Inbox
To start, we can simply execute Getmail with no arguments:
C:\Inbox> getmail
GetMail v1.33: WinNT console utility to download a mailbox's mail.
syntax:
Getmail -u <user> -pw <password> -s <server> [optional switches (see below)]
Getmail -install [ see install details below ]
Getmail -profile [-delete | "<default>"] [profile1] [profileN] [-q]
Getmail -h [-q]
Getmail -forceextract filename
-install <server> <userid> <password> [<delete> [<xtract> [<try> [<port> [<profile>]]]]]
: set's POP3 server, login, password, whether to delete or not (Yes/No),
whether to automatically extract base64/7bit/UU encoded files or not (Yes/No),
number of tries and port for profile
(<delete> <xtract> <try> and <port> may be replaced by '-').
-u <userid> : Specify userid on remote pop3 host
-pw <password>: Specify password for userid on remote mail host
-s <server> : Specify mail server (pop3) to contact
-nodelete : Do not delete messages after downloading (default)
-delete : Delete messages after downloading
-noxtract : Do not extract base64/7bit/UU files after downloading (default)
-xtract [defname]: Extract base64/7bit/UU encoded files after downloading messages
defname is an optional default filename for the extracted file
-domainstamp : Prepend sender's domain name to extracted attachments
-headersonly : Download only the headers of the message
-port <port> : port to be used on the server, defaults to POP3 (110)
-p <profile> : send with SMTP server, user and port defined in <profile>.
-q : supresses *all* output.
-n <n> : Only get 'n' messages
-m <n> : Only get message # n
-b <n> : Retrieve messages beginning from # n
-plain : Extract text/plain segments too (usually ignored)
-h : displays this help.
-try <n times>: how many attempts to access mail. from '1' to 'INFINITE'
-ti <n> : Set timeout to 'n' seconds.
-forceextract fn: Attempt to extract any encoded messages in 'fn'
Simple Example
Let's start with a simple example. Here, I want to simply retrieve
my email (from a POP3) server giving all required parameters on the command-line.
C:\Inbox> getmail -s mail2.idevelopment.info -u jhunter -pw ForMeToKnow
Failed to open registry key for GetMail profile , using default.
Failed to open registry key for GetMail
Getting jhunter's mailbox contents from server mail2.idevelopment.info:110
There are 41 messages on the server.
Retrieving Message #1 (0 bytes) into 'MSG1.TXT'...ok
Retrieving Message #2 (0 bytes) into 'MSG2.TXT'...ok
... < SNIP > ...
Retrieving Message #40 (0 bytes) into 'MSG40.TXT'...ok
Retrieving Message #41 (0 bytes) into 'MSG41.TXT'...ok
Storing Default values in the Windows Registry
From the example above, I needed to provide my POP3 server (using the -s option) as well
as the user account (the -u option) and my password (the -pw option). I can instead store these values
in the Windows registry and have them provided by default. To do this, I first need to update
the values in Windows registry using the -install option as follows:
C:\Inbox> getmail -install mail2.idevelopment.info jhunter ForMeToKnow
Failed to open registry key for GetMail profile , using default.
Failed to open registry key for GetMail
POP3 server set to mail2.idevelopment.info on port 110.
User/password jhunter/ForMeToKnow, Deletion: No, Xtraction: No, retry 1 time(s)
It's recommended to put a fake password in here, and then provide the real password each
time you run Getmail, for obvious security reasons. After putting in a fake password to
install, you would then use your real password as one of the parameters:
C:\Inbox> getmail -install mail2.idevelopment.info jhunter MyFakePassword
C:\Inbox> getmail -pw MyRealPassword
C:\Inbox> getmail -p default
Extracting Binary Attachments
The following example overrides by fake password (see above) and
automatically extracts any binary attachments:
C:\Inbox> getmail -pw MyRealPassword -xtract
Wednesday, 02-Apr-2008 11:22:38 EDT
Page Count: 13305