Header Ads

  • New Updates

    Sending Email from the Command Line using Blat and Stunnel





    Sending Email from the Command Line using Blat and Stunnel


    If you are generating Print  report using "GeneratePrintJobAccountingReports.ps1" and if you want send monthly report as e-mail you can use below script .
    For sending e-mail using Gmail SMTP you may need 2 Utilities,
    1.Blat
    2.Stunnel


    I am running this script in same printer server 2012.
    After you complete generate report (to generate Print report user


    Download Blat and copy blat.exe and other files in system32 folder .And open cmd and type below command for setup your email settings in Blat.


    > Blat -install -Server smtp.gmail.com -Port 465 -f  printserverreports@gmail.com  -u  printserverreports@gmail.com -Pw Password
    Install Stunnel


    If you didn't install and configure Stunnel you will stuck at this error " SMTP ERROR: ERROR-NOT A SOCKET"


     Go to ProgramFile > stunnel >conf> stunnel.conf and edit below lines


    # GLOBAL OPTIONS
    client = yes
    output = stunnel-log.txt
    debug = 0
    taskbar = no
    # SERVICE-LEVEL OPTIONS
    [SMTP Gmail]
    accept = 127.0.0.1:1099
    connect = smtp.gmail.com:465
    [POP3 Gmail]
    accept = 127.0.0.1:1109
    connect = pop.gmail.com:995


    Save settings and Install Stunnel as service.
    All Programs >Windows search search for  stunnel Service Install  and install
    Then start the service.
    Got Tips from below site,
    https://blog.frogslayer.com/sending-an-email-through-google-smtp-via-windows-command-line/


    Then Make below script in notepad and save as cmd.


    To send e-mail report as attachment download below link from >>
    https://gallery.technet.microsoft.com/scriptcenter/Script-to-generate-print-84bdcf69#content


    Save script in D:\Scripts Folder


    @echo off 
     
    REM Run this script on the first day of each month to generate and e-mail detail and summary reports of printer usage by username 
     
    REM set the Blat error log filename 
    REM   %~f0 is the fully-qualified path name of this script. Note the Windows "Region and Language" format needs to be "English (United States)" (so the date environment variable is of the format "Sun 11/13/2011"set BLATERRORLOGFILENAME="%~f0-%date:~-4%%date:~4,2%%date:~7,2%-%time:~0,2%%time:~3,2%-ERROR.log" 
     
    REM generate print job reports for specified print server for the specified time period 
    powershell.exe -command "D:\Scripts\GeneratePrintJobAccountingReports.ps1 localhost PreviousMonth" 
    if not %ERRORLEVEL%==0 goto REPORT_ERROR 
     
    REM the print job report succeeded, so mail the generated reports to the desired recipient 
    "\\%USERDOMAIN%\NETLOGON\Blat\blat.exe" - -f printserverreports@gmail.com -to destinationaddress@gmail.com -server 127.0.0.1:1099 -subject "Print server usage report" -body "The print server usage report is attached." -attach "D:\Scripts\Print job accounting report*.csv" -log %BLATERRORLOGFILENAME% 
     
    REM check if the e-mail was sent successfully (error level 0 returned); if it was, delete the Blat log file to clean up; if it was not, display an error message to the console and leave the full error log file in place 
    if %ERRORLEVEL%==0 (del %BLATERRORLOGFILENAME%else (echo ERROR: %date% %time% An error occurred when Blat tried to send e-mail. The e-mail was not sent. Please check the log file %BLATERRORLOGFILENAME% for more information. & echo %date% %time% An error occurred when Blat tried to send e-mail. The e-mail was not sent. Error details are above. >> %BLATERRORLOGFILENAME%) 
     
    REM delete the generated reports to clean up 
    del "D:\Scripts\Print job accounting report*.csv" 2nul 
    goto END 
     
    :REPORT_ERROR 
    REM the print job report generation process had an error, so mail an error report 
    echo ERROR: The print job report generation process had an error, so mailing an error report... 
    "\\%USERDOMAIN%\NETLOGON\Blat\blat.exe" - -f printserverreports@domain.local -to destinationaddress@domain.local -server 127.0.0.1:1099 -subject "ERROR: Print server usage report" -body "The print server usage report could not be generated due to an error. Please contact IT support for assistance." -log %BLATERRORLOGFILENAME% 
     
    REM check if the e-mail was sent successfully (error level 0 returned); if it was, delete the Blat log file to clean up; if it was not, display an error message to the console and leave the full error log file in place 
    if %ERRORLEVEL%==0 (del %BLATERRORLOGFILENAME%else (echo ERROR: %date% %time% An error occurred when Blat tried to send e-mail. The e-mail was not sent. Please check the log file %BLATERRORLOGFILENAME% for more information. & echo %date% %time% An error occurred when Blat tried to send e-mail. The e-mail was not sent. Error details are above. >> %BLATERRORLOGFILENAME%) 
     
    :END 
    I Run this both scripts (Script for generate report and Script for Send e-mail ) in same Printer server.It may vary in your case.


    Create a scheduled task to run "D:\Scripts\GenerateAndEMailPrintJobAccountingReports.cmd" every 1st of the month




    :)



















    No comments

    Post Top Ad

    Post Bottom Ad

    Ads