Advertisement
Guest User

sendmail.vbs

a guest
Nov 30th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.   ' CDO
  3.  Const cdoSendUsingPickup = 1, cdoSendUsingPort = 2
  4.   Const cdoAnonymous = 0, cdoBasic = 1, cdoNTLM = 2
  5.   Const cdoDSNDefault = 0, cdoDSNNever = 1, cdoDSNFailure = 2, cdoDSNSuccess = 4, cdoDSNDelay = 8, cdoDSNSuccessFailOrDelay = 14
  6.  
  7.   ' FSO
  8.  Const ForReading = 1, ForWriting = 2, ForAppending = 8
  9.  
  10.   ' Server, port, user and password
  11.  Const MAIL_SERV = "mail.example.com"
  12.   Const MAIL_PORT = 25
  13.   Const MAIL_USER = "someuser"
  14.   Const MAIL_PASS = "somepass"
  15.  
  16.   On Error Resume Next
  17.  
  18.   ' global vars
  19.  Dim objArgs, fso
  20.   Dim nExitCode
  21.  
  22.   ' init
  23.  Set objArgs = WScript.Arguments
  24.   Set fso = CreateObject("Scripting.FileSystemObject")
  25.   nExitCode = Main(objArgs.Count, objArgs)
  26.   WScript.Quit(nExitCode)
  27.  
  28. ' test code
  29. Function Main(argc, argv)
  30.   Dim bResult
  31.  
  32.   Main = 1
  33.   bResult = SendMail("foo@example.com", _
  34.                      "bar@example.net; baz@example.net", _
  35.                      "log@example.net", _
  36.                      "", _
  37.                      "Test email from CDO", _
  38.                      "Please see the HTML part of this message", _
  39.                      "c:\temp\tmpfile.htm", _
  40.                      Array("c:\temp\file.zip", "c:\temp\file.log"), _
  41.                      False)
  42.  
  43.  
  44.   If bResult Then
  45.     Main = 0
  46.   End If  
  47. End Function
  48.  
  49. ' Sends an email message
  50. '
  51. ' sFrom     sender
  52. ' sTo       recipient
  53. ' sCc       copy recipient
  54. ' sBCc      blind copy recipient
  55. ' sSubject  message subject
  56. ' sBody     textual body
  57. ' sHTMLfile pathname of an HTML file to use for the HTML body
  58. ' vaAttach  array containing pathnames for attached files
  59. ' bReceipt  flag to enable return receipts
  60. '
  61. Function SendMail(sFrom, sTo, sCc, sBCc, sSubject, sBody, sHTMLfile, vaAttach, bReceipt)
  62.   Dim objMsg, objConf, sConf
  63.   Dim iAtt, sFile
  64.  
  65.   On Error Resume Next
  66.   SendMail = False
  67.   set objMsg = CreateObject("CDO.Message")
  68.   set objConf = CreateObject("CDO.Configuration")
  69.  
  70.   ' setup the SMTP client config
  71.  Set objFlds = objConf.Fields
  72.   sConf = "http://schemas.microsoft.com/cdo/configuration/"
  73.   With objFlds
  74.     .Item(sConf & "sendusing") = cdoSendUsingPort
  75.     .Item(sConf & "smtpserver") = MAIL_SERV
  76.     .Item(sConf & "smtpserverport") = MAIL_PORT
  77.     If Len(MAIL_PASS)>0 Then
  78.       .Item(sConf & "smtpauthenticate") = cdoBasic
  79.       .Item(sConf & "sendusername") = ""
  80.       .Item(sConf & "sendpassword") = ""
  81.     Else
  82.       .Item(sConf & "smtpauthenticate") = cdoAnonymous
  83.       .Item(sConf & "sendusername") = ""
  84.       .Item(sConf & "sendpassword") = ""
  85.     End If      
  86.     .Update
  87.   End With
  88.  
  89.   ' create the message
  90.  With objMsg
  91.     ' setup basic infos
  92.    Set .Configuration = objConf
  93.     .From = sFrom
  94.     .To = sTo
  95.     .Cc = sCc
  96.     .BCc = sBcc
  97.     .Subject = sSubject
  98.     .TextBody = sBody
  99.     If Len(sHTMLfile) > 0 Then
  100.       ' got an HTML file for the body
  101.      sFile = GetFileName(sHTMLfile)
  102.       If Len(sFile) > 0 Then
  103.         sFile = "file://" & Replace(GetFileName(sFile), "\", "/")
  104.         .CreateMHTMLBody sFile
  105.       End If
  106.     End If
  107.     For iAtt = LBound(vaAttach) To UBound(vaAttach)
  108.       If Len(vaAttach(iAtt)) > 0 Then
  109.         ' add attachment
  110.        sFile = GetFileName(vaAttach(iAtt))
  111.         If Len(sFile) > 0 Then
  112.           .Addattachment sFile
  113.         End If
  114.       End If
  115.     Next
  116.     If bReceipt = True Then
  117.       ' set return receipt
  118.      sConf = "urn:schemas:mailheader:"
  119.       .Fields(sConf & "disposition-notification-to") = sFrom
  120.       .Fields(sConf & "return-receipt-to") = sFrom
  121.       .DSNOptions = cdoDSNSuccessFailOrDelay
  122.     End If
  123.     .Fields.update
  124.   End With
  125.  
  126.   ' we're ready, send the message
  127.  Err.Clear
  128.   objMsg.Send
  129.  
  130.   ' check result/errors
  131.  If Err.Number = 0 Then
  132.     SendMail = True
  133.   End If
  134. End Function
  135.  
  136. ' gets the full pathname for a given file
  137. Function GetFileName(sPathName)
  138.   GetFileName = ""
  139.   If Not fso.FileExists(sPathName) Then
  140.     Exit Function
  141.   End If  
  142.   Set fi = fso.GetFile(sPathName)
  143.   GetFileName = fi.Path
  144. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement