How to Send Email From VBScript/Batch file

How to Send Email From VBScript/Batch file

Often I have to send emails of various logs and other details to myself and others. Just for this stuff logging on to the system ,collect the data, compose the email, Attach log file.. It take lot of time and efforts. Was wondering if a script can do the job for me.. Overall thats why scripting is ..

Later i came across a VBScript logic which can send email for me.. It helped me a lot so thought lets write an article in simple terms explaining how you can use it to fulfill your needs .

I tried to explain it in the way so anyone without knowing VB can use it..

Let’s Take some real world scenarios where we can use it….

#1: You perform a automated FTP upload job on a remote server/desktop and wondering where the upload was successful or not. For that you will have to login to the system and check per day.

You can simply append this script into the script which performs the upload and once upload completes, it will send an email to you notifying the result or with the log file attached directly into your inbox.

#2: You perform a scheduled automated Backup job on your systems in a network. Wondering whether the backup was successful or not. Deploy this script (u can customize it as you want) and where the backup was successful or not, you will receive the result in your inbox with the result and computer name in subject line itself. (U can easily Modify the script to accomplish this).

Alright..Seems interesting. Lets see the script. Its VBScript.

'Script to send email… by…...
'Configuration Field,Modify below..

SMTPServer = "yoursmtpserver_IP_OR_NAME"
Subject = "Test email"
attachment = "c:\test.txt"

'If there is no attachment needed,Remove or comment above line.
'To comment the line,just add " ' " before the line.
'DO not modify anything below this line.

GenericSendmail SMTPserver, From, Recipient, Subject, Message

Sub GenericSendmail (SMTPserver, From, Recipient, Subject, Message)

set msg = WScript.CreateObject("CDO.Message")
msg.From = From
msg.To = Recipient
msg.Subject = Subject
msg.TextBody = Message
msg.AddAttachment attachment
msg.Configuration.Fields ("") = SMTPServer
msg.Configuration.Fields("") = 2
End Sub


Alright, That’s the script. Now how to use it:

There can be many ways to use it. I will list some of them ..

Call from a batch file

Step 1. Create a text file in the same folder where the batch file is placed, Save it as sendemail.vbs

Step 2. Paste the script In that text file and modify the configuration field as per your environment.

Step 3. Call the script from a batch file like below:

Cscript sendemail.vbs

Append the script itself in the batch file so it will create the script in temp folder on the fly when you execute the batch.

Step 1. Edit your batch file and add this line somewhere before you want to call the email script.

type %0 | find /i ” ” | find /v “butnotthisline” >%temp%\sendemail.vbs

Step 2. Call the script from the batch wherever you want, the command would be

Cscript %temp%\sendemail.vbs

Step 3. Add one more line in your batch script, just after your code ends and the Vbscript starts.


Step 4. Above will extract the Vbscript from the batch file and then put in tempfolder.

Step 5. Modify this script’s configuration field and then append in your original batch script after above line

Step 6. Add 8 (or n number) of space before each line of the Vbscript part after :GOTO :EOD line

The point here is , we have to append n number of space before each line and then extract all lines with those number of spaces from this batch file. SO, if you add 8 space here then make sure there is only 8 space exist between ” ” in step b above.


Now, What if you want to customize the email subject with your own variables like computername/date time or something else.

Lets do that as well.. For that you have to modify the configuration field wisely.

This is the configuration field.

SMTPServer = "yoursmtpserver_IP_OR_NAME"
Subject = "Test email"
attachment = "c:\test.txt"

Suppose, you want to change the subject to the computer name of the system from where the script is being executed. Modify the subject line as below.

Subject = Wscript.Arguments(0)

And now, Call the script from your batch like this.

Cscript %temp%\sendemail.vbs “I want the subject as %computername%”

Alright..Thats all for now.. Hope this is useful.