[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Email from a batch file

Posted on 2011-10-26
8
Medium Priority
?
362 Views
Last Modified: 2012-05-12
I wrote a batch file that will back up certain users files on their computer.  Is it possible to have in the batch file a way to email the user that the backup took place?  If so could I get some examples?
0
Comment
Question by:HBMI
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:exile1337
ID: 37030766
try this one


@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

Open in new window

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 37030898
That relies on the registered email client to send a message, e.g. Outlook, Lotus Notes, etc.  Much better is to use a tool such as BLAT or a short VBScript to send a message direct to an SMTP server:

http://scripts.dragon-it.co.uk/links/email-from-batch

The above is a page of mine showing one method using the free BLAT tool and then at the bottom you've got a VBScript which you can amend in notepad, put it in the same dir and then call from batch file with

cscript //nologo sendmail.vbs

hth

Steve
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37031698
A recent question just addressed this, take a look at the link below for approaches.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27403190.html

~bp
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 43

Expert Comment

by:Steve Knight
ID: 37033836
@billprew. Yes though it was a bit messy and drawn out and never really had any actual specific conclusion!
@exile1337.  In http:#37030766 why do you set DATE and TIME to blank?!


Anyway you have got some options now for how to do the emails.... a mailto: link, blat, or VBScript.  Do you need help with identifying the errors, in which case please let us see your script if you want help integrsating it.  Easiest option is to download sendmail.vbs file above (and attached here), edit it notepad for your address, text and smtp server and add  cscript //nologo sendmail.vbs to your script relating to finding an error.

Steve

 sendmail.vbs
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37055209
HBMI - did that help with what you needed?

Steve
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37067562
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37123459
Need any additional help on this question?

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37207280
Glad it helped.
thanks

Steve
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question