Search code examples
vbscript

creating a vbs script function for sending email with attachment


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 = "wolverinewest@wolverinetruckgroup.com"
        .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

Solution

  • 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"