Rob van der Woude's Scripting Pages

Send e-mails by batch

Batch generated e-mail messages were mentioned before in my Solutions found in alt.msdos.batch page.

The January 2000 issue of Blaise, the Dutch HCC Pascal User Group's magazine, contained an article about sending e-mail with Delphi. The article contained more information on the command strings used to send e-mail.

The general format is:

mailto:to?subject=subject&cc=cc_address&bcc=bcc_address&body=message_body
to_address The (escaped) e-mail address of the recipient; allowed formats:
• someone@somedomain.com
• Full%20Name<someone@somedomain.com>
subject The (escaped) subject field of the message
cc_address The (escaped) "carbon copy" e-mail address; allowed formats:
• someone@somedomain.com
• Full%20Name<someone@somedomain.com>
bcc_address The (escaped) "blind carbon copy" e-mail address; allowed formats:
• someone@somedomain.com
• Full%20Name<someone@somedomain.com>
message_body The actual message body (escaped too)

 

Notes: (1) All parameters mentioned need to be "escaped", i.e. spaces should be replaced by %20, carriage return/line feeds by %0D%0A, double quotes by %22, single quotes by %27, backslashes by %5C, less than by <, greater than by >, and ampersands by &.
For batch files and command line use, unless the entire string is enclosed in doublequotes, the percent signs in the "escape sequences" themselves must also be replaced by double percent signs: so in the end spaces should be replaced by %%20, carriage return/line feeds by %%0D%%0A, etcetera.
  (2) The commands shown here only create a message.
To send it you still need to press the Send button yourself.
Check out the links to third party command line e-mail utilities at the bottom of this page if you need to send e-mail unattended.

 

To use this type of command in batch files we need to:

 

Examples:

 

START mailto:dummy@nuldevice.com?subject=Test%%20messageˆ&cc=info@nuldevice.comˆ&body=Hi,%%0D%%0A%%0D%%0AThis%%20is%%20an%%20automatically%%20created%%20message.%%0D%%0A%%0D%%0ABye

This command will create a message to dummy@nuldevice.com, with a carbon copy to info@nuldevice.com, with the words "Test message" in the subject field.
The message itself will consist of the following text:

Hi,

This is an automatically created  message.

Bye

 

The following NT only batch file will check if the correct network drive mappings have been made.
If not, an e-mail message to the helpdesk is generated (assuming the e-mail software is available without the mappings). The user can add more information before actually sending it.

@ECHO OFF
SET ERRORS=0
:: Use your own mappings and search strings here
NET USE P: | FIND /I "%username%" >NUL 2>NUL
IF ERRORLEVEL 1 CALL :ErrorMsg P:
NET USE S: | FIND /I "system" >NUL 2>NUL
IF ERRORLEVEL 1 CALL :ErrorMsg S:
:: Create an e-mail message if any errors were detected
IF %ERRORS% GEQ 1 CALL :Mail
:: Clear variables used in this batch file
FOR %%A IN (BODY DATE ERRORS LINE MESSAGE TIME) DO SET %%A=
GOTO :EOF

:ErrorMsg
SET /A ERRORS = %ERRORS% + 1
NET USE %1 >NUL 2>NUL
IF ERRORLEVEL 1 (
	SET BODY=%BODY%%%0D%%0ADrive%%20%1%%20not%%20mapped
	GOTO :EOF
)
FOR /F "TOKENS=3* DELIMS= " %%A IN ('NET USE %1 ˆ| FIND "Local name"') DO SET MESSAGE=Drive%%20%%A%%20mapped%%20to%%20
:: Replacement of backslashes "\" by "%%5C"
:: prevents interpretation of "\n" as CR/LF
FOR /F "TOKENS=3,4* DELIMS=\ " %%A IN ('NET USE %1 ˆ| FIND "Remote name"') DO SET MESSAGE=%MESSAGE%%%5C%%5C%%A%%5C%%B
SET BODY=%BODY%%%0D%%0A%MESSAGE%
GOTO :EOF

:Mail
FOR /F "TOKENS=2*" %%A IN ('DATE/T') DO SET DATE=%%A
FOR /F %%A IN ('TIME/T') DO SET TIME=%%A
START mailto:helpdesk@ourdomain.com?subject=Drive%%20mapping%%20errorˆ&body=At%%20%DATE%,%%20%TIME%,%%20user%%20%USERNAME%%%20encountered%%20the%%20following%%20drive%%20mapping%%20error(s)%%20on%%20%COMPUTERNAME%:%%0D%%0A%BODY%
GOTO :EOF
Click to view source Click to download the ZIPped sources

 

As the resulting message content already indicates, using this batch file you will probably run into command length limitations real soon:

At 15/01/2000, 13:43, user ROB encountered the following drive mapping error(s) on SERVER:

Drive P: not mapped
Drive S: mapped to \\SE

Limiting the length of the first line may help a little, but the limit will be about two or maybe three error messages.

 

Assuming your log files aren't too big, you could use the following NT only batch procedure to create a message to the system administrator, containing the (ASCII) log file:

@ECHO OFF
FOR /F "TOKENS=*" %%A IN (LOGFILE.LOG) DO CALL :AddLine %%A
:: The ":~6" in "%BODY:~6%" removes the "%0D%0A" at the start of the body
START mailto:sysadm@ourdomain.com?subject=Log%%20fileˆ&body=%BODY:~6%
GOTO :EOF

:AddLine
SET LINE=
FOR %%Z IN (%*) DO CALL :AddWord %%Z
:: The ":~3" in "%LINE:~3%" removes the "%20" at the start of the line
SET BODY=%BODY%%%0D%%0A%LINE:~3%
GOTO :EOF

:AddWord
SET LINE=%LINE%%%20%1
GOTO :EOF
Click to view source Click to download the ZIPped sources

 

I wouldn't go as far as saying that if it isn't mentioned here it isn't possible.
However, I often get questions about how to add attachments, or how to read the message body from a file.
Please believe me, if it were possible in "pure" batch (and if I were aware of that) I would have mentioned it here!

So this is where the third party tools come into view.

 

Scribe is a small footprint e-mail program (fits on a floppy disk) that can be used as an "ordinary" mail program, plus it can be used to send mail from the command line.

 

Command line e-mail programs:

Blat:      Send mail via SMTP. Free
GBMailer:   Command-line mailer. GPL
MailSend:   Command-line Internet mailer. Shareware
GetMail:   Receive mail through POP3. Free

If you use an Exchange mail server, consider using MAPISEND from the Exchange Resource Kit for command line mailing.

Mozilla Thunderbird users can compose messages on the command line, similar to the "mailto:" technique described above, but including attachments, using its -compose switch.

 

WSH based e-mail:

Use CDOSYS to send e-mails in VBScript or HTAs.

 

GUI to dynamically generate unattended e-mail:

Use Fileaze to create tasks that include anything from capturing web pages to sending files per e-mail, and from compressing to encrypting, plus anything that can be run from a "normal" command line.

 

New features in HTML5

Besides sending e-mails, HTML5 allows you to send SMS (text messages) and initiate phone calls.

Phone call:

<a href="tel:0123456789">Give us a call!</a>

SMS:

<a href="sms:0123456789?body=This is my text message">Send us a text message!</a>
Notes: (3) As is the case with e-mail messages, you cannot actually send text messages, nor make a phone call, all you can do is open the SMS or phone interface with the number and (for SMS) text already filled in
  (4) Avoid ampersands and doublequotes in the SMS body, or escape them (see Note 1)
  (5) For text messages to multiple cell phones, separate the numbers by commas:
<a href="sms:0123456789,0214365879?body=Text message for both of you">Send us both a text message!</a>

page last modified: 2022-10-26; loaded in 0.0012 seconds