L'objectif de ce script est principalement de pouvoir envoyer une pièce jointe avec la log d'un traitement en fin de traitement.
Sachant que ce script utilise outlook et non un serveur SMTP, il doit être executé sur un poste client. J'ai donc ajouter la possibilité de planifier la date et l'heure d'envoi.
[MailSpecification.txt]
TO:Thomas@tomamplius.net
CC:Thomas@tomamplius.net
BCC:Thomas@tomamplius.net
SUBJECT:test d'envoi de mail
DATE:21/12/2011 11:47
ATTACHEMENT:\\serveur1\partage\log\traitement.log
BODY:Bonjour,
Ceci est un test
Au revoir
tomamplius
Voici un exemple de commande pour générer un mail depuis le fichier de spécification :
cscript /nologo sendmail.vbs MailSpecification.txt
[sendmail.vbs]
' ************************************************************************
' ** script name : sendMail.vbs
' **
' ** Description : Send mail from file
' **
' ** parameter needed :
' ** STRING: File with mail information
' **
' ************************************************************************
' -------------------- SUIVI DES MODIFICATIONS ---------------------------
' -- 27/12/2011 THL create script
' ---------------------------------------------------------------------
option explicit
Const olMailItem = 0
' ************************************************************************
' Validate type of string
' ************************************************************************
Private sub DoNothing(a)
End sub
' ************************************************************************
' Validate type of string
' ************************************************************************
Private function ValideType(Chaine,TypeValue)
on error resume next
ValideType = true
if Ucase(TypeValue) = "INT" then
Call DoNothing(cint(Chaine))
elseif Ucase(TypeValue) = "DATE" then
Call DoNothing(cdate(Chaine))
elseif Ucase(TypeValue) = "FILE" then
Call CreateObject("Scripting.FileSystemObject").fileexists(Chaine)
elseif Ucase(TypeValue) = "FOLDER" then
Call CreateObject("Scripting.FileSystemObject").folderexists(Chaine)
else
ValideType = false
End if
if not err.number = 0 then
ValideType = false
Call err.clear()
End if
End function
' ************************************************************************
'
' ************************************************************************
private function GetMailStruture()
Dim tmp
set GetMailStruture = createobject("scripting.dictionary")
for each tmp in array( _
"FROM","TO","CC","BCC","SUBJECT","ATTACHEMENT","BODY","DATE")
call GetMailStruture.add(tmp,createobject("scripting.dictionary"))
next
end function
' ************************************************************************
' Read file to get information
' ************************************************************************
private function GetMail(Path)
Dim tmp, line
set GetMail = GetMailStruture()
with createobject("scripting.filesystemobject").opentextfile(Path)
line = .readline()
while not .atendofstream
for each tmp in array( _
"FROM","TO","CC","BCC","SUBJECT","ATTACHEMENT","DATE")
if left(line,len(tmp)+1) = tmp + ":" then
call GetMail.item(tmp).add( _
GetMail.item(tmp).count,mid(line,len(tmp)+2))
end if
next
if left(line,5) = "BODY:" then
call GetMail.item("BODY").add(0,mid(line,6))
while not .atendofstream
line = .readline()
GetMail.item("BODY")(0) = _
GetMail.item("BODY")(0) + chr(13) + chr(10) + line
wend
else
line = .readline()
end if
wend
end with
end function
' ************************************************************************
' Dictionary to list of email
' ************************************************************************
private function DictToString(dict)
dim tmp
for each tmp in dict
DictToString = DictToString & ";" & dict(tmp)
next
DictToString = mid(DictToString,2)
end function
' ************************************************************************
' create and send mail
' ************************************************************************
Public Sub sendMail(mailInfo)
dim tmp
with createobject("Outlook.application").CreateItem(olMailItem)
.To = DictToString(mailInfo("TO"))
.CC = DictToString(mailInfo("CC"))
.BCC = DictToString(mailInfo("BCC"))
.Subject = DictToString(mailInfo("SUBJECT"))
for each tmp in mailInfo("ATTACHEMENT")
.Attachments.Add(mailInfo("ATTACHEMENT")(tmp))
next
.Body = mailInfo("BODY")(0)
Call .Display
'Call .Send
end with
End Sub
' ************************************************************************
' Control input data
' ************************************************************************
private sub ControlInfo(MailInfo)
dim tmp, control
'Valid parameter
for each control in array( array("DATE","DATE"), array("ATTACHEMENT","FILE"))
for each tmp in mailInfo(control(0))
if not ValideType(mailInfo(control(0))(tmp),control(1)) then
call err.raise(-9999, _
"An " + control(0) + " parameter is not a valid : " + _
mailInfo(control(0))(tmp) )
end if
next
next
'Unique parameter
for each tmp in array( "SUBJECT", "DATE")
if mailInfo(tmp).count > 1 then
call err.raise(-9999,"One " + tmp + " parameter is allow")
end if
next
'Mandatory parameter
for each tmp in array( "SUBJECT", "TO")
if mailInfo(tmp).count = 0 then
call err.raise(-9999,"One "+tmp+" parameter is mandatory")
end if
next
End sub
' ************************************************************************
' Control input data
' ************************************************************************
private sub WaitToSend(dictDate)
dim Sendtime, tmp
Sendtime = now
for each tmp in mailInfo("DATE")
Sendtime = CDATE(DictToString(mailInfo("DATE")))
next
while Sendtime > now
wscript.sleep(1000)
wend
End sub
' ************************************************************************
' Main
' ************************************************************************
Dim MailInfo
If not wscript.arguments.count = 1 then
Call wscript.echo("Invalide number of parameters")
elseif not ValideType(wscript.arguments(0),"FILE") then
Call wscript.echo("Parameter is File")
else
set MailInfo = GetMail(wscript.arguments(0))
Call ControlInfo(MailInfo)
Call WaitToSend(MailInfo("DATE"))
Call sendMail(MailInfo)
Call wscript.quit(0)
End if
Call wscript.quit(1)