Any way to send log file or txt file into email

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Any way to send log file or txt file into email

#1 Post by goodywp » 31 May 2018 13:02

Hi all,

It looks like that batch file itself can not send any log file or txt file into email recipient. Which one you suggest to use, I looked up on internet and blat is the one..
My purpose is to send out the log file and some error txt file into several email recipients to alert for the batch job running...

Any input from your experience or expertise shall be appreciated...

Thanks

goodywp

Squashman
Expert
Posts: 4473
Joined: 23 Dec 2011 13:59

Re: Any way to send log file or txt file into email

#2 Post by Squashman » 31 May 2018 13:52

Just search the forums. Blat has mention a dozen times. Your other option is a VBscript.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: Any way to send log file or txt file into email

#3 Post by goodywp » 01 Jun 2018 07:27

Thanks Squashman as always !!!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Any way to send log file or txt file into email

#4 Post by Ed Dyreen » 01 Jun 2018 20:10

goodywp wrote:
31 May 2018 13:02
Hi all,

It looks like that batch file itself can not send any log file or txt file into email recipient. Which one you suggest to use, I looked up on internet and blat is the one..
My purpose is to send out the log file and some error txt file into several email recipients to alert for the batch job running...

Any input from your experience or expertise shall be appreciated...

Thanks

goodywp
Using telnet, files can be attached by MIME, data should be base64encoded

Code: Select all

Subject:Small Koala
MIME-Version: 1.0
Content-Type:multipart/mixed;boundary="KkK170891tpbkKk__FV_KKKkkkjjwq"
--KkK170891tpbkKk__FV_KKKkkkjjwq
Content-Type:application/octet-stream;name="koala.jpg"
Content-Transfer-Encoding:base64
Content-Disposition:attachment;filename="koala.jpg"
https://dustyhoppe.wordpress.com/2012/0 ... nd-telnet/

I never found a way to properly automate telnet with batch, for the automation I use Telnet Scripting Tool v.1.0 by Albert Yale.

Code: Select all

@echo off &setlocal enableDelayedExpansion &set $lf=^


::
set  ^"$c1=%%~#^<nul ^^"
set ^"$n1c=^^^%$lf%%$lf%^%$lf%%$lf%^<nul ^^"

:: ip changed ?
:: ipconfig /flushdns &cls

call :getDate "()"

:: CUSTOMIZABLE SECTION
:: (
	set    ^"$server=********.ddns.net 25" %= your domain and port =%

	set       "$user=********=" %= base64encoded =%
	set       "$pass=********=" %= base64encoded =%

	set       "$from=********@outlook.com" %= can be the same as to for a happy spam =%
	set         "$to=********@outlook.com"
	set "$returnPath=" %= can be left empty for a happy spam =%

	set  "$fromAlias=" %= can be left empty for a happy spam =%
	set    "$toAlias="
	set    "$replyTo=********@outlook.com" %= can be the same as to for a happy spam =%

	set   ^"$subject=*******"
	set      ^"$date=!$dayName!, !$day! !$monthName! !$year! 00:00:00 +0000"
	
%==%for %%# in ("") do set ^"$data=%$c1%
%=	=%%$c1%
%=	=%happy spam%$n1c%
"
:: )

:: Surround with doublequotes if defined
for %%? in ( $fromAlias, $toAlias ) do if defined %%~? set %%~?="!%%~?!" &set %%~?=!%%~?:""="!

for %%# in ("") do set ^"$data=%$c1%
%$c1%
%=	=%FROM: !$fromAlias! ^<!$from!^>%$n1c%
%=	=%TO: !$toAlias! ^<!$to!^>%$n1c%
%=	=%REPLY-TO: ^<!$replyTo!^>%$n1c%
%$c1%
%=	=%SUBJECT: !$subject!%$n1c%
%=	=%DATE: !$date!%$n1c%
%=	=%%$n1c%
%=	=%!$data!%$n1c%
%=	=%%$n1c%
%=	=%.%$n1c%
%=	=%%$n1c%
"
for %%? in ( "!$lf!" ) do set "$data=!$data:%%~?=\m!"

> "script.txt" (

	echo.!$server!
	echo.WAIT "server ready."

	echo.SEND "helo\m"
	echo.WAIT "Hello, ."

	echo.SEND "auth login\m"
	echo.WAIT "334 VXNlcm5hbWU6"
	echo.SEND "!$user!\m"
	echo.WAIT "334 UGFzc3dvcmQ6"
	echo.SEND "!$pass!\m"
	echo.WAIT "235 Authentication successful."

	echo.SEND "MAIL FROM: <!$returnPath!>\m"
	echo.WAIT "250 Sender OK - send RCPTs."

	echo.SEND "RCPT TO: <!$to!>\m"
	echo.WAIT "250 Recipient OK - send RCPT or DATA."

	echo.SEND "DATA\m"
	echo.WAIT "354 OK, send data, end with CRLF.CRLF"

	echo.SEND "!$data!"
	echo.WAIT "250 Data received OK."

	echo.SEND "QUIT"
	%=										=%
	%=	must end with no or double empty line otherwise				=%
	%=	Telnet Scripting Tool v1.0 will show help instead of running the script	=%
	echo.
)

type "script.txt"
pause
start /LOW "Telnet Scripting Tool v1.0" "tst10.exe" /r:script.txt &exit 0

:getDate "()"
::
setlocal
:: (
	set "$date=!DATE!"

	:: get $dayName
	:: (
		set "ma=Mon" &set "di=Tue" &set "wo=Wed" &set "do=Thu" &set "vr=Fri" &set "za=Sat" &set "zo=Sun"
		for %%? in ( "!$date:~0,2!" ) do set "$dayName=!%%~?!"
	:: )

	:: get $day
	:: (
		for /F "tokens=1 delims=/" %%? in ( "!$date:~3!" ) do set "$=%%~?"
		if %$:~0,1% NEQ 0 ( set "$=!$:~0,2!" ) else set "$=!$:~1,1!"

		set "$day=!$!"
	:: )

	:: get $monthName
	:: (
		for /F "tokens=2 delims=/" %%? in ( "!$date:~3!" ) do set "$=%%~?"
		if %$:~0,1% NEQ 0 ( set "$=!$:~0,2!" ) else set "$=!$:~1,1!"

		set "1=jan" &set "2=feb" &set "3=mar" &set "4=apr" &set "5=may" &set "6=jun"
		set "7=jul" &set "8=aug" &set "9=sep" &set "10=okt" &set "11=nov" &set "12=dec"
		for %%? in ( "!$!" ) do set "$monthName=!%%~?!"
	:: )

	:: get $year
	:: (
		for /F "tokens=3 delims=/" %%? in ( "!$date:~3!" ) do set "$=%%~?"

		set "$year=!$!"
	:: )
:: )
endlocal &set "$dayName=%$dayName%" &set "$day=%$day%" &set "$monthName=%$monthName%" &set "$year=%$year%" &exit /B 0

haggy
Posts: 11
Joined: 24 May 2018 03:39

Re: Any way to send log file or txt file into email

#5 Post by haggy » 04 Jun 2018 05:29

You may want to have a look at this tool . it is an external prog though but scriptable.

https://retired.beyondlogic.org/consult ... nemail.htm

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: Any way to send log file or txt file into email

#6 Post by goodywp » 14 Jun 2018 08:05

Hi all,
Thank you for all your response. I end up using a Python + Gmail as below code:

Code: Select all

from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib
import sys

recipients = ['goodywp@hotmail.com','goodwyang@gmail.com'] 
emaillist = [elem.strip().split(',') for elem in recipients]
msg = MIMEMultipart()
msg['Subject'] = str(sys.argv[1])
msg['From'] = 'goodwyang@gmail.com'
msg['Reply-to'] = 'goodwyang@gmail.com'
 
msg.preamble = 'Multipart massage.\n'
 
part = MIMEText("Hi, please find the attached file for failed scheme_replacement")
msg.attach(part)
 
part = MIMEApplication(open(sys.argv[2],"rb").read())
#part = MIMEApplication(open(str(sys.argv[2]),"rb").read())
part.add_header('Content-Disposition', 'attachment', filename=str(sys.argv[2]))
msg.attach(part)
 

server = smtplib.SMTP('smtp.gmail.com:587')
#server = smtplib.SMTP("smtp.gmail.com:587")
server.ehlo()
server.starttls()
server.login("goodwyang@gmail.com", "Gmailisgr8")
 
server.sendmail(msg['From'], emaillist , msg.as_string())
Then a batch file to run it as below:

Code: Select all

@echo off
call C:\auto_pkg_build\Tools\Doc_Convert\send_email_attachment.py "scheme_replace failed log" "C:/auto_pkg_build/Tools/Doc_Convert/replace_scheme.log"
Thanks

Post Reply