Send Mail from Script

How can I send an e-mail message from a script?
There are a few simple ways to send e-mail messages from the command prompt or from within a script. Here are 5 of my favorites (listed at no particular order):

Method #1: MAILSEND

If you want to send a single e-mail message or hundreds of them and do it all from a single batch file or command you should use the MAILSEND program written by Muhammad A Muquit.
This is extremely useful in E2K. Whenever you create new mailbox-enabled users you need to send them an e-mail message before you can begin to manipulate mailbox permissions etc. I use MAILSEND to automate this task.
MAILSEND is a simple program to send mail via SMTP for MS Windows NT/2000/XP. To use it, you must know the address or IP address of the SMTP server you wish to use.
MAILSEND is a console application, you can run it from command shell or by clicking on the app from file explorer. You can use it in many ways. I’m using 192.168.0.200 as the IP address of the SMTP server as an example, change this to your valid one. Some example are shown below:

Interactive usage

​MAILSEND
SMTP server address/IP: 192.168.0.200
Domain: mydomain.com
From: [email protected]
To: you@yourdomain,[email protected]
Carbon copy: [email protected]
Blind Carbon copy:
Subject: This is a test
================================================
Type . in a new line and press Enter to end the message, CTRL+C to abort
================================================
this is a test
this is a test
.

Non-interactive usage

Everything the program needs can be fed from stdin or from a file.
Examples:

​MAILSEND -d mydomain.com -smtp 192.168.0.200 -t [email protected] -f [email protected] -sub "test" -m msg.txt

message can be passed from stdin as well:

​MAILSEND -d mydomain.com -smtp 192.168.0.200 -t [email protected] -f [email protected] -sub "test" < msg.txt

If no message file is specified, it will ask to type the message:

​MAILSEND -d mydomain.com -smtp 192.168.0.200 -t [email protected] -f [email protected] -sub "test"
================================================
Type . in a new line and press Enter to end the message, CTRL+C to abort
================================================
test. test.
.

Also a file can be created with SMTP server, from, to, cc, bcc, subject and mail body which can be used as a single input to the program. For example, if you create a file like below:

​192.168.0.200
mydomain.com
[email protected]
[email protected]
Mail body starts here.
This file can be used to feed to the MAILSEND program.
  • The first line is the SMTP server address or IP address.
  • The second line is the domain used in SMTP HELO.
  • The third line is the From address.
  • The fourth line the To address/es. They can be command separated.
  • Right after the To the mail body starts.

Now this file can be fed as follows:

​MAILSEND -v < msg.txt

You can see the mail getting delivered if you specify the -v flag. Note: in non-interactive mode, you can not specify Cc, Bcc or Subject in the file. If you need any of them specify with appropriate flags.
Download MAILSEND
Or from HERE (local download if author’s site ever goes dead).

Method #2: A short VBS script

I thank Yuval Sinai for the quick tip.

Open Notepad and enter the following text:

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "[email protected]"
objEmail.To = "[email protected]"
objEmail.Subject = "Server is down!"
objEmail.Textbody = "Server100 is no longer accessible over the network."
objEmail.Send

Lamer note: You should enter your own sender and recipient e-mail addresses. Same goes for the subject and text body.
Save the file as something.vbs
Double-clicking it or calling it from another script will activate it.

Method #3: Another short VBS script

Open Notepad and enter the following text:

Set Msg = CreateObject("CDO.Message")
With Msg
 .To = "[email protected]"
 .From = "[email protected]"
 .Subject = "Hello"
 .TextBody = "Just wanted to say hi."
 .Send
End With
MsgBox "Script Complete"

Another Lamer note: Again, you should enter your own sender and recipient e-mail addresses. Same goes for the subject and text body.
Save the file as something.vbs
Double-clicking it or calling it from another script will activate it.

Method #4: Using a Send Mail

You could also download and use my own Send Mail which is quite easy to use (perhaps easier than all the above).

Method #5: Bmail

Bmail is a free but lean command line SMTP mail sender. Don’t get fooled into playing $$$ for huge executables. Bmail allows the user to automate the sending of email messages containing log files, data downloads or error messages on Win32 based computers. Together with the freeware utility mpack, you can also send MIME encoded attachments.

​C:'>bmail -s zeus -t [email protected] -f [email protected] -h -a "Script
is not Working Correctly" -b "The script on zeus has stopped getting data"
Command Line SMTP Emailer V1.07
Copyright(C) 2002-2004 [email protected]
Opening connection to zeus [192.168.0.200] on port 25
220 zeus Postfix Sat, 27 May 2006 23:29:58 +0300
250 Ok: queued as 05162181A7

Bmail has the following options:

​C:'>bmail /?
Command Line SMTP Emailer V1.07
Copyright(C) 2002-2004 [email protected]
Usage: bmail [options]
-s SMTP Server Name
-p SMTP Port Number (optional, defaults to 25)
-t To: Address
-f From: Address
-b Text Body of Message (optional)
-h Generate Headers
-a Subject (optional)
-m Filename (optional) Use file as Body of Message
-c Prefix above file with CR/LF to separate body from header
-d Debug (Show all mail server communications)

The mail server name, To: Address and From: Address fields are mandatory. Multiple recipients can be specified after the -t by separating them by a comma. The SMTP port number is seldomly used, but can be used to force mail to be sent to a non standard port. The user has two choices for specifying the body of the message. If a single line body is all that is required, it can be specified on the command line using the -b option. In this case you may want to also use -a to specify a subject and -h to add TO: and FROM: headers to the body of your message.
However if a larger message body is required, such as a pre-formatted text email, log file, data file or a mime encoded attachment etc then the body of the message can be read from a file. The file can be specified by the -m switch. In this case the user can choose to use the -h, -a and -b switches in conjunction with the file, or manually enter these headers to the top of your file.
Download Bmail + Instructions