Hi
I have a script (took it off from Microsoft help and did a bit of change). The original was for CDONTS and I changed it into CDOSYS.
The intention is to use it via command line CSCRIPT.
This code doesn't send the email for some reason. But if I replace the MyMail section into hardcoded contents it will send the email properly. This is the hardcoded version
The minute I change MyMail.Subject / TextBody to refer to the argument it won't send.
I echoed out each of the strSubject and strBody and it did print out the parameter properly.
Example usage : CSCRIPT sendmail.vbs -t "myemail@domain.com" -f "myemail@domain.com" -s "Subject here" -b "Some text here"
Firstly, I don't know how to print out the error, it seems Err.Number is empty.
Any insights are appreciated.
Regards,
Namida
I have a script (took it off from Microsoft help and did a bit of change). The original was for CDONTS and I changed it into CDOSYS.
The intention is to use it via command line CSCRIPT.
Code:
Option Explicit
On Error Resume Next
Dim objSendMail, MyMail, oArgs, ArgNum
Dim strTo, strFrom, strSubject, strBody
Set oArgs = WScript.Arguments
ArgNum = 0
While ArgNum < oArgs.Count
Select Case LCase(oArgs(ArgNum))
Case "-to","-t":
ArgNum = ArgNum + 1
strTo = oArgs(ArgNum)
Case "-from","-f":
ArgNum = ArgNum + 1
strFrom = oArgs(ArgNum)
Case "-subject","-s":
ArgNum = ArgNum + 1
strSubject = oArgs(ArgNum)
Case "-body","-b":
ArgNum = ArgNum + 1
strBody = oArgs(ArgNum)
Case "-help","-?":
Call DisplayUsage
Case Else:
Call DisplayUsage
End Select
WScript.Echo oArgs(ArgNum)
ArgNum = ArgNum + 1
Wend
If oArgs.Count=0 Or strTo="" Or strFrom="" Or _
strSubject="" Or strBody="" Then
Call DisplayUsage
Else
Set MyMail = CreateObject("CDO.Message")
MyMail.From = strFrom
MyMail.To = strTo
MyMail.Subject = strSubject
MyMail.TextBody = strBody
MyMail.Send
Set MyMail = Nothing
End If
' Display the usage for this script
Sub DisplayUsage
WScript.Echo "Usage:"
WScript.Echo " sendmail -t <to address> -f <from address> -s " & _
Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & _
"<message body>" & Chr(34)
WScript.Echo " sendmail [-help|-?]"
WScript.Echo ""
WSCript.Quit
End Sub
This code doesn't send the email for some reason. But if I replace the MyMail section into hardcoded contents it will send the email properly. This is the hardcoded version
Code:
MyMail.From = "myemail@domain.com"
MyMail.To = "myemail@domain.com"
MyMail.Subject = "sending email via CDOSYS NewMail"
MyMail.TextBody = "Sending email with CDOSYS NewMail" &_
"objects is easy! Try it!"
The minute I change MyMail.Subject / TextBody to refer to the argument it won't send.
I echoed out each of the strSubject and strBody and it did print out the parameter properly.
Example usage : CSCRIPT sendmail.vbs -t "myemail@domain.com" -f "myemail@domain.com" -s "Subject here" -b "Some text here"
Firstly, I don't know how to print out the error, it seems Err.Number is empty.
Any insights are appreciated.
Regards,
Namida