Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Command line email revisited

70 views
Skip to first unread message

Tom Lavedas

unread,
Dec 22, 2012, 1:57:28 PM12/22/12
to
Foxidrive posted about the subject back in June and Timo added and expanded it in his FAQ site. I appended an update last week, but because it was at the end of an old post and my post contained some errors, I've decided to post the correction in a new thread. Also, this version's functionality is tested and is known to work.

The procedure is a hybrid batch/VBScript solution, much like that in the earlier thread. The fundamental differences here are that this approach does not use a temporary VBscript file and it allows input arguments to be provided in any order.

:: email.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Tom Lavedas, Modified to eliminate temporary VBS script file
@echo off
setlocal

:: defaults
set From=m...@mymail.com
set To=y...@somemail.com
set Subj="email test %date% %time%"
set Body="did it work? %date% %time%"
set Serv=outgoing.server.com
set Auth=userid
set Pass=password
set fileattach=

:Use command line arguments, if supplied
if [%1] EQU [] goto Continue
set Arg=%1
set Arg=%Arg::==%
set Arg=%Arg:/=%
set %Arg%
shift
goto :Use
:Continue

set "VBS=set fso=createobject(""scripting.filesystemobject""):"
set "VBS=%VBS%Execute(fso.GetStandardStream(0).readALL):"
set "VBS=%VBS%end with:close"

call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass% %fileattach%
id not errorlevel 0 echo Error: %ErrorLevel%

if %0==%~f0 pause
exit /b %ErrorLevel%

:send
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
set "FA="
if "%~8" NEQ "" FA=echo objEmail.AddAttachment "%~8"
::echo on
(
echo with CreateObject("CDO.Message"^^^)
echo .From = "%~1"
echo .To = "%~2"
echo .Subject = "%~3"
echo .Textbody = "%~4"
%FA%
echo with .Configuration.Fields
echo .Item ("%cdoSchema%/sendusing"^^^) = 2 ' not local, smtp
echo .Item ("%cdoSchema%/smtpserver"^^^) = "%~5"
echo .Item ("%cdoSchema%/smtpserverport"^^^) = 25
echo .Item ("%cdoSchema%/smtpauthenticate"^^^) = 1 ' cdobasic
echo .Item ("%cdoSchema%/sendusername"^^^) = "%~6"
echo .Item ("%cdoSchema%/sendpassword"^^^) = "%~7"
echo .Item ("%cdoSchema%/smtpusessl"^^^) = False
echo .Item ("%cdoSchema%/smtpconnectiontimeout"^^^) = 25
echo .Update
echo end with ' Configuration.Fields
echo On Error Resume Next
echo .Send
echo end with ' CDO.Message
echo if Err.Number = 0 then
echo sRes = "0#Mail sent without error"
echo else
echo sRes = Hex(Err.Number^^^) + "#" + Err.Description
echo end if
echo fso.GetStandardStream(1^^^).write sRes
) | @for /f "tokens=1,2 delims=#" %%A in (
'more ^^^| mshta.exe vbscript:Execute("%VBS%"^^^)'
) do @echo %%B & exit /b %%A
:: ======== END email-bat.cmd =========

Input arguments are provided as colon separated strings, thus ...

varname:"String input"

They can be in any order. Using them looks like this ...

email-bat to:servi...@somewhere.com body:"Server is down: %date% %Time%"

Hope that is of some use to someone.
______________________
Tom Lavedas

foxidrive

unread,
Dec 22, 2012, 7:07:56 PM12/22/12
to
On 23/12/2012 5:57 AM, Tom Lavedas wrote:
>
> :: email.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::
> :: Tom Lavedas, Modified to eliminate temporary VBS script file
> @echo off
> setlocal

Nice enhancements Tom.


--
foxi

Timo Salmi

unread,
Dec 23, 2012, 3:12:05 AM12/23/12
to
On 22.12.2012 20:57 Tom Lavedas <tglb...@verizon.net> wrote:
> Foxidrive posted about the subject back in June and Timo added and
> expanded it in his FAQ site.

The latter being

188} How can I send email or newsgroup messages from the command-line?
http://www.netikka.net/tsneti/info/tscmd188.php

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php
0 new messages