Send e-mails by batch programming
The general format is:
|to_address||The (escaped) e-mail address of the recipient; allowed formats:
|subject||The (escaped) subject field of the message|
|cc_address||The (escaped) "carbon copy" e-mail address; allowed formats:
|bcc_address||The (escaped) "blind carbon copy" e-mail address; allowed formats:
|message_body||The actual message body (escaped too)|
|Notes:||(1)||All parameters mentioned need to be "escaped", i.e. spaces should be replaced 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
|(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:
- precede the string with the START command
- replace every single percent sign ( % ) by double percent signs ( %% )
- "escape" ampersands ( & ) with carets ( ˆ )
- limit the length of the mailto string to the maximum allowable command line length minus 6 (leaving a maximum of 121 characters for MS-DOS, or 249 characters for Windows NT 4/Windows 2000, or 2035 for Windows XP)
START mailto:firstname.lastname@example.org?subject=Test%%20messageˆemail@example.comˆ&body=Hi,%%0D%%0A%%0D%%0AThis%%20is%%20an%%20automatically%%20created%%20message.%%0D%%0A%%0D%%0AByeThis command will create a message to firstname.lastname@example.org, with a carbon copy to email@example.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:firstname.lastname@example.org?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
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 \\SELimiting 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:email@example.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
Third party toolsI 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|
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 HTML5Besides sending e-mails, HTML5 allows you to send SMS (text messages) and initiate phone calls.
<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:|