Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Send email after completion of batch file execution  (Read 39275 times)

0 Members and 1 Guest are viewing this topic.

rkp

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    Send email after completion of batch file execution
    « on: August 23, 2012, 10:01:30 AM »
    is there any way to send an email after completion of the batch file execution?

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Send email after completion of batch file execution
    « Reply #1 on: August 23, 2012, 06:45:43 PM »
    Try this:

    Code: [Select]
    ::email-bat.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
    @echo off
    setlocal

    :: defaults
    set [email protected]
    set [email protected]
    set Subj="email test   %date% %time%"
    set Body="did it work? %date% %time%"
    set Serv=mail.server.com.au
    set Auth=user
    set Pass=pass
    set fileattach=

    :: if command line arguments are supplied then use them
    if "%~7" NEQ "" (
    set From=%1
    set To=%2
    set Subj="%~3"
    set Body="%~4"
    set Serv=%5
    set "Auth=%~6"
    set "Pass=%~7"
    set "fileattach=%~8"
    )

    call :createVBS "email-bat.vbs"

    call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass%
    del "%vbsfile%" 2>nul
    goto :EOF

    :send
    cscript.exe /nologo "%vbsfile%" %1 %2 %3 %4 %5 %6 %7 >nul 2>nul
    goto :EOF

    :createVBS
    set "vbsfile=%~1"
    del "%vbsfile%" 2>nul
    set cdoSchema=http://schemas.microsoft.com/cdo/configuration
    echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
    echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
    echo >>"%vbsfile%" objEmail.From     = objArgs(0)
    echo >>"%vbsfile%" objEmail.To       = objArgs(1)
    echo >>"%vbsfile%" objEmail.Subject  = objArgs(2)
    echo >>"%vbsfile%" objEmail.Textbody = objArgs(3)
    if defined fileattach echo >>"%vbsfile%" objEmail.AddAttachment "%fileattach%"
    echo >>"%vbsfile%" with objEmail.Configuration.Fields
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = objArgs(4)
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = 25
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = objArgs(5)
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendpassword")     = objArgs(6)
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = False
    echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
    echo >>"%vbsfile%"  .Update
    echo >>"%vbsfile%" end with
    echo >>"%vbsfile%" objEmail.Send
    rem

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Send email after completion of batch file execution
    « Reply #2 on: August 23, 2012, 09:28:38 PM »
    don't know why, but your code didn't work for me Foxidrive. If it doesn't end up working for you rkp, here's one you could try. Only got it to work for gmail so far though (must have a gmail account to send from)

    Code: [Select]

    'Usage: cscript sendemail.vbs <[email protected]> "<subject_line>" "<email_body>" "<optional:email_attachment_path>"
    'Ex. No attach: cscript sendemail.vbs [email protected] "test subject line" "test email body"
    'Ex. W/ attach: cscript sendemail.vbs [email protected] "test subject line" "test email body" "c:\scripts\log.txt"

    '***********
    '****CONFIGURE THE FROM EMAIL ADDRESS AND PASSWORD

    Const fromEmail = "[email protected]"
    Const password = "xxxxxxxxxx"

    '****END OF CONFIGURATION
    '***********

    Dim emailObj, emailConfig
    Set emailObj = CreateObject("CDO.Message")
    emailObj.From = fromEmail
    emailObj.To = WScript.Arguments.Item(0)
    emailObj.Subject = WScript.Arguments.Item(1)
    emailObj.TextBody = WScript.Arguments.Item(2)

    If WScript.Arguments.Count > 3 Then
    emailObj.AddAttachment WScript.Arguments.Item(3)
    End If

    Set emailConfig = emailObj.Configuration
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = fromEmail
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password
    emailConfig.Fields.Update

    emailObj.Send

    Set emailobj = nothing
    Set emailConfig = nothing
    you'll want to save this as a mail.vbs

    then using your batch file navigate to it and use the command:
    Code: [Select]
    mail <[email protected]> "<subject>" "<body>"
    or
    Code: [Select]
    mail <[email protected]> "<subject>" "<body>" "<path:\\attachment>"

    if those don't work, you could try the more wordy version
    Code: [Select]
    cscript sendemail.vbs <[email protected]> "<subject>" "<body>" "<path:\\attachment>"

    if you put mail.vbs in your system32 folder (you may not want to) you can do this without navigating to it's location
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Send email after completion of batch file execution
    « Reply #3 on: August 24, 2012, 10:22:40 AM »
    Lemonilla,
    Foxidrive's code works just fine. You just need to tweak it for your smtp servers settings.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Send email after completion of batch file execution
    « Reply #4 on: August 24, 2012, 03:47:58 PM »
    This might be an alternative to CDO which has been deprecated on newer Window versions. If you can, try downloading Blat which can be run standalone or part of a batch file.

    If you have access to Powershell, a cmdlet specific for this purpose is available (Send-MailMessage) or it can be scripted with the SMTP client object.

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Ocalabob



      Rookie

      Thanked: 4
      • Experience: Familiar
      • OS: Windows 7
      Re: Send email after completion of batch file execution
      « Reply #5 on: August 24, 2012, 06:31:44 PM »
      Greetings all,

      I have had success testing foxidrive's code on yahoo.com, gmail.com
      and earthlink.net using XP SP3 and Win 7 Home Edition

      rkp, should you wish to enhance the capabilities of foxidrive's
      code, for example to have more than one line of text in the
      'body' of the email, then I would suggest a review of Timo Salmi's code
      which can be found at:  www.netikka.net/tsneti/info/tscmd.php

      Have a look at item number 188.

      Best wishes!

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Send email after completion of batch file execution
      « Reply #6 on: August 24, 2012, 06:44:44 PM »
      rkp, should you wish to enhance the capabilities of foxidrive's
      code, for example to have more than one line of text in the
      'body' of the email, then I would suggest a review of Timo Salmi's code
      which can be found at:  www.netikka.net/tsneti/info/tscmd.php

      Have a look at item number 188.

      Best wishes!

      Thanks Ocalabob, and I confirm that there are other good solutions at Timo's link.