Search code examples
emailvbscriptmultiline

Vbscript - Multiple lines on vbs script email


I use the following code to send automatic emails, passing a parameter in them :

Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
Const cdoBasic = 2
Const cdoSendUsingPort = 2
Dim oMsg, oConf
Dim sDateTimeStamp

Set args = WScript.Arguments
arg1 = args.Item(0)  


' E-mail properties
Set oMsg      = CreateObject("CDO.Message")
oMsg.From     = "[email protected]"  ' or "Sender Name <[email protected]>"
oMsg.To       = "[email protected]"    ' or "Recipient Name <[email protected]>"
oMsg.Subject  = "Subject"
oMsg.TextBody = "Transfer of files failed - Check RUNLOG for error.  Julian date :  " & arg1  


' GMail SMTP server configuration and authentication info
 Set oConf = oMsg.Configuration
 oConf.Fields(schema & "smtpserver")       = "smtp server" 'server address
 oConf.Fields(schema & "smtpserverport")   = 25              'port number
 oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
 oConf.Fields(schema & "smtpauthenticate") = cdoBasic         'authentication type
 oConf.Fields(schema & "smtpusessl")       = False             'use SSL encryption
 oConf.Fields(schema & "sendusername")     = "[email protected]" 'sender username
 oConf.Fields(schema & "sendpassword")     = "password"      'sender password
 oConf.Fields.Update()

I would like to add a multiline string in the text message body, i have tried using the _ concat operator like so :

multi = "aaaaaa " & _
   "FROM NC301B " & _
   "WHERE EDIPROC like 'P30_' " & _
   "AND (LF301M > 0) " 

but it does not work. Can someone give me pointers? Thank you.

I would like the end body of the email to contain this:

"Transfer of files failed - Check RUNLOG for error."  
"Julian date : " & arg1  
"Line of text"  
"Line of text"  

If this is also doable without multi-lining with & _ then i m curious about that also. Thank you!


Solution

  • Just adding a string concatenation character doesn't automatically insert a newline, you need to use one of the built in constants to define a newline

    multi = "aaaaaa " & vbNewLine & _
       "FROM NC301B " & vbNewLine & _
       "WHERE EDIPROC like 'P30_' " & vbNewLine & _
       "AND (LF301M > 0) "
    

    vbNewLine is the best one as it's platform specific. But for completeness the other ones are;

    '--- Named Constants ---
    vbCr              'Carriage Return
    vbLf              'Linefeed
    vbCrLf            'Carriage Return Linefeed
    
    '--- Character Codes using Chr() ---
    Chr(13)           'Carriage Return
    Chr(10)           'Linefeed
    Chr(13) & Chr(10) 'Carriage Return Linefeed
    

    If you also wanted to build a HTMLBody for your e-mail using newline would not work as HTML ignores whitespace, you would need to use <br /> to force a newline.