<!-------------------------------------------------------------------
-- DESCRIPTION
-- Sends email message with optional attachments.
------------------------------------------------------------------>
<package>
<job ID=
"SendMail"
>
<script Language=
"VBScript"
Src=
"Common.vbs"
/>
<script Type=
"text/vbscript"
>
Option
Explicit
Const
SWITCH_FROM =
"from"
Const
SWITCH_TO =
"to"
Const
SWITCH_CC =
"cc"
Const
SWITCH_BCC =
"bcc"
Const
SWITCH_SUBJECT =
"subject"
Const
SWITCH_BODY =
"body"
Const
SWITCH_SERVER =
"smtp"
Const
SWITCH_PORT =
"port"
Const
SWITCH_HTML =
"html"
Const
SWITCH_FILE =
"file"
Const
SWITCH_ADD =
"add"
Const
SWITCH_CHARSET =
"charset"
Const
SWITCH_ADD_DELIMETER =
";"
Wscript.Quit(Main())
Function
Main()
Dim
strFrom, strTo, strCc, strBcc, strSubject, strBody
Dim
strServer, nPort
Dim
bIsHtml, bIsFile
Dim
strCharset
Dim
arAttachments
Dim
i
Main = -1
RevertToCscript(
False
)
If
(IsHelpMode(
False
))
Then
ShowHelp()
Main = 0
Exit
Function
End
If
If
Not
Initialize _
( _
strFrom, strTo, strCc, strBcc, strSubject, strBody, _
strServer, nPort, bIsHtml, bIsFile, strCharset, _
arAttachments _
)
Then
Exit
Function
End
If
If
(bIsFile)
Then
If
Not
FileExists(GetAbsolutePath(strBody))
Then
WScript.Echo
"File '"
& strBody &
"' does not exist."
Exit
Function
End
If
End
If
If
GetArraySize(arAttachments) > 0
Then
For
i = LBound(arAttachments)
To
UBound(arAttachments)
If
Not
FileExists(GetAbsolutePath(arAttachments(i)))
Then
WScript.Echo
"File '"
& arAttachments(i) &
"' does not exist."
Exit
Function
End
If
Next
End
If
If
(bIsFile =
True
)
Then
strBody = ReadTextFromFileEx(GetAbsolutePath(strBody), strCharset)
End
If
SendEmail _
strFrom, strTo, strCc, strBcc, _
strSubject, strBody, strServer, nPort, _
bIsHtml, strCharset, arAttachments
Main = 0
WScript.Echo
"Done."
End
Function
Sub
SendEmail _
( _
ByVal
strFrom, _
ByVal
strTo, _
ByVal
strCc, _
ByVal
strBcc, _
ByVal
strSubject, _
ByVal
strBody, _
ByVal
strServer, _
ByVal
nPort, _
ByVal
bIsHtml, _
ByVal
strCharSet, _
ByRef
arAttachments _
)
Dim
i, oEmail
Set
oEmail = CreateObject(
"CDO.Message"
)
With
oEmail
.From = strFrom
.
To
= strTo
If
Not
IsEmptyString(strCc)
Then
.Cc = strCc
End
If
If
Not
IsEmptyString(strBcc)
Then
.Bcc = strBcc
End
If
.Subject = strSubject
If
bIsHtml =
True
Then
.HTMLBody = strBody
.HTMLBodyPart.charset = strCharset
Else
.TextBody = strBody
.TextBodyPart.charset = strCharset
End
If
If
GetArraySize(arAttachments) > 0
Then
For
i = LBound(arAttachments)
To
UBound(arAttachments)
.AddAttachment Replace(GetAbsolutePath(arAttachments(i)),
"\", "
\\
" ), "
", "
"
Next
End
If
If
Not
IsEmptyString(strServer)
Then
With
.Configuration.Fields
.Update
End
With
End
If
.Send
End
With
Set
oEmail =
Nothing
End
Sub
Function
Initialize _
( _
ByRef
strFrom, _
ByRef
strTo, _
ByRef
strCc, _
ByRef
strBcc, _
ByRef
strSubject, _
ByRef
strBody, _
ByRef
strServer, _
ByRef
nPort, _
ByRef
bIsHtml, _
ByRef
bIsFile, _
ByRef
strCharset, _
ByRef
arAttachments _
)
Initialize =
False
Dim
strValue, strErrMsg
strErrMsg =
"Missing required parameter: "
strFrom = GetParamValue(SWITCH_FROM)
If
(IsEmptyString(strFrom))
Then
WScript.Echo strErrMsg & SWITCH_FROM
Exit
Function
End
If
strTo = GetParamValue(SWITCH_TO)
If
(IsEmptyString(strTo))
Then
WScript.Echo strErrMsg & SWITCH_TO
Exit
Function
End
If
strCc = GetParamValue(SWITCH_CC)
strBcc = GetParamValue(SWITCH_BCC)
strSubject = GetParamValue(SWITCH_SUBJECT)
If
(IsEmptyString(strSubject))
Then
strSubject =
"TEST"
End
If
strBody = GetParamValue(SWITCH_BODY)
If
(IsEmptyString(strBody))
Then
WScript.Echo strErrMsg & SWITCH_BODY
Exit
Function
End
If
strServer = GetParamValue(SWITCH_SERVER)
strValue = GetParamValue(SWITCH_PORT)
If
(IsEmptyString(strValue))
Then
nPort = 25
Else
nPort =
CInt
(strValue)
End
If
strValue = GetParamValue(SWITCH_HTML)
If
(IsEmptyString(strValue))
Then
bIsHtml =
False
Else
strValue = UCase(strValue)
If
(strValue =
"YES"
)
Or
(strValue =
"Y"
)
Or
_
(strValue =
"TRUE"
)
Or
(strValue =
"T"
)
Then
bIsHtml =
True
Else
bIsHtml =
False
End
If
End
If
strValue = GetParamValue(SWITCH_FILE)
If
(IsEmptyString(strValue))
Then
bIsFile =
False
Else
strValue = UCase(strValue)
If
(strValue =
"YES"
)
Or
(strValue =
"Y"
)
Or
_
(strValue =
"TRUE"
)
Or
(strValue =
"T"
)
Then
bIsFile =
True
Else
bIsFile =
False
End
If
End
If
strValue = GetParamValue(SWITCH_CHARSET)
If
(IsEmptyString(strValue))
Then
strCharset =
"utf-8"
Else
strCharset = strValue
End
If
arAttachments = GetParamValues(SWITCH_ADD, SWITCH_ADD_DELIMETER)
Initialize =
True
End
Function
Sub
ShowHelp()
Dim
strMsg
strMsg = _
"DESCRIPTION:"
& vbCrLf &_
vbCrLf &_
" Sends an email."
& vbCrLf &_
vbCrLf &_
"USAGE:"
& vbCrLf &_
vbCrLf &_
" cscript "
& Wscript.ScriptName &_
" [/option[:parameter["
& SWITCH_ADD_DELIMETER &
"...]]] [...]"
& vbCrLf &_
vbCrLf &_
"OPTIONS:"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_FROM & vbCrLf &_
" Email From address."
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_TO & vbCrLf &_
" Email To address. Use comma to separate multiple addresses."
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_CC & vbCrLf &_
" [Optional] Email CC address. Use comma to separate multiple addresses."
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_BCC & vbCrLf &_
" [Optional] Email BCC address. Use comma to separate multiple addresses."
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_SUBJECT & vbCrLf &_
" [Optional] Email Subject line. "
& vbCrLf &_
" [Default: TEST]"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_BODY & vbCrLf &_
" Plain text, HTML text, or path to file containing email message."
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_SERVER & vbCrLf &_
" [Optional] SMTP server."
& vbCrLf &_
" [Default: localhost]"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_PORT & vbCrLf &_
" [Optional] SMTP server port."
& vbCrLf &_
" [Default: 25]"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_HTML & vbCrLf &_
" [Optional] Indicates whether email message format is HTML."
& vbCrLf &_
" [Values: yes|no|y|n|true|false|t|f]"
& vbCrLf &_
" [Default: no]"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_FILE & vbCrLf &_
" [Optional] Indicates whether the [/"
& SWITCH_BODY &
"] parameter points to a file."
& vbCrLf &_
" [Values: yes|no|y|n|true|false|t|f]"
& vbCrLf &_
" [Default: no]"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_CHARSET & vbCrLf &_
" [Optional] Specifies character set of the email text (or HTML text)."
& vbCrLf &_
" [Default: utf-8]"
& vbCrLf &_
vbCrLf &_
" "
& SWITCH_ADD & vbCrLf &_
" [Optional] Indicates paths to file attachments."
& vbCrLf &_
" Multiple files must be separated by semicolons."
Wscript.Echo strMsg
End
Sub
</script>
</job>
</package>