Sunday, November 20, 2011

How can I send an e-mail message from a script?

Send Mail from ScriptThere are a few simple ways to send e-mail messages from the command prompt or from within a script.

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.

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: me@mydomain.com
To: you@yourdomain,otherperson@otherdomain.com
Carbon copy: him@example.com
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 me@mydomain.com -f you@yourdomain.com -sub "test" -m msg.txt
***********************************************************************

message can be passed from stdin as well:
***********************************************************************
MAILSEND -d mydomain.com -smtp 192.168.0.200 -t me@mydomain.com -f you@yourdomain.com -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 me@mydomain.com -f you@yourdomain.com -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
me@mydomain.com
you@yourdomain.com
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: 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).
This command line utility sends SMTP mail through the specified SMTP server.
New version allows you to specify a user and password for authenticating to the mail server. (For use with servers that do not allow relaying).
Note: It seems that this utility can only send lower-case text. It's possible that the author will release an updated version that fixes this minor issue in the future, but for now you must live with this limitation.

Download

Download sendmail.zip (6kb)
You need to extract the tool/script found in the downloaded file by using WinZip or similar.

Syntax


Example:

SendMail /smtpserver mailsrv01 /to lamer@yourdomain.com /from me@mydomain.com /subject Message subject /body message body /attachment c:'temp'att.zip

Detailed Instructions

1.       Place Sendmail.exe in your system path folders.
2.       Make sure you know the FQDN or IP address of your mail server.
3.       Use the above syntax.

No comments:

Post a Comment