0
votes

I'm new to scripting and I'm creating an automated task inside openoffice I'm having issue with the crude basic language but I found a excel macro written in vba that does what I need now im trying to convert this to a useable vbscript that can be executed from the command prompt with something like email.vbs mailto subject filetoattach

Dim cdoMsg As New CDO.Message

With cdoMsg
    With .Configuration.Fields
        .Item(cdoSendUsingMethod).Value = cdoSendUsingPort
        .Item(cdoSMTPUseSSL).Value = True
        .Item(cdoSMTPServerPort).Value = 465
        .Item(cdoSMTPServer).Value = "smtpserver"
        .Item(cdoSendUserName).Value = "[email protected]"
        .Item(cdoSendPassword).Value = "mypass"
        .Item(cdoSMTPAuthenticate).Value = cdoBasic
        .Update
    End With
    .From = "myemail"
    .To = "emailto"
    .Subject = "Some more really spiffy mail for you!"
    .TextBody = "please find attachment"
    .AddAttachment App.Path & "attachment"

    On Error Resume Next
    .Send
End With

If Err.Number <> 0 Then
    MsgBox "CDO error " & Hex$(Err.Number) & vbNewLine & Err.Description, _
           vbOKOnly Or vbExclamation, _
           Caption
Else
    MsgBox "Mail sent!", vbOKOnly, Caption
End If
2

2 Answers

1
votes

VBScript has no constants built in so you have to look the constants in VBA's object browser (Alt + F11 - F2 in Excel) and use the number. There is only one datatype in VBScript so no dimming anything as anything (eg Dim x as string only Dim x). VBscript only can late bind so no Dim cdoMsg As New CDO.Message - have to set cdomsg = CreateObject("cdo.message").

There is nothing crude about VBScript.

Fields will have default values taken from Outlook Express or Windows Mail but are not set on Windows 10 as it doesn't have either of those two programs.

Here's the bare minimum

Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = "[email protected]"
emailObj.To       = "[email protected]"
emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

emailObj.AddAttachment "c:\windows\win.ini"

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "Username"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Password"
emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then Msgbox "Done"
0
votes

syntax for launching in windows if using wscript is;

wscript.exe "C:\path\example.vbs" "your email address" "who your sending it to" "your subject" "your body text & vbCRLF & for new line" "c:\path\attachment"

the "" are required because the spaces through off the order and windows send errors if spaces are in the path

dim mailto
dim mailfrom
dim subject
dim body
dim attachment
mailfrom = WScript.Arguments.Item(0)
mailto = WScript.Arguments.Item(1)
subject = WScript.Arguments.Item(2)
body = WScript.Arguments.Item(3)
attachment = WScript.Arguments.Item(4)


Set emailObj      = CreateObject("CDO.Message")
emailObj.From     = mailfrom
emailObj.To       = mailto
emailObj.Subject  = subject
emailObj.TextBody = body

emailObj.AddAttachment attachment

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mysmtp.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "myUsername"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "myPassword"
emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then Msgbox "email sent"