Smtp Mailer That Supports Html And Attachments.


SUBMITTED BY: Guest

DATE: May 29, 2013, 7:01 a.m.

FORMAT: Text only

SIZE: 6.2 kB

HITS: 1421

  1. ;
  2. ;##################################
  3. ; Include
  4. ;##################################
  5. #Include<file.au3>
  6. ;##################################
  7. ; Variables
  8. ;##################################
  9. $SmtpServer = "MailServer" ; address for the smtp-server to use - REQUIRED
  10. $FromName = "Name" ; name from who the email was sent
  11. $FromAddress = "your@Email.Address.com" ; address from where the mail should come
  12. $ToAddress = "your@Email.Address.com" ; destination address of the email - REQUIRED
  13. $Subject = "Userinfo" ; subject from the email - can be anything you want it to be
  14. $Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail
  15. $AttachFiles = "" ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
  16. $CcAddress = "CCadress1@test.com" ; address for cc - leave blank if not needed
  17. $BccAddress = "BCCadress1@test.com" ; address for bcc - leave blank if not needed
  18. $Importance = "Normal" ; Send message priority: "High", "Normal", "Low"
  19. $Username = "******" ; username for the account used from where the mail gets sent - REQUIRED
  20. $Password = "********" ; password for the account used from where the mail gets sent - REQUIRED
  21. $IPPort = 25 ; port used for sending the mail
  22. $ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS
  23. ;~ $IPPort=465 ; GMAIL port used for sending the mail
  24. ;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS
  25. ;##################################
  26. ; Script
  27. ;##################################
  28. Global $oMyRet[2]
  29. Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
  30. $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
  31. If @error Then
  32. MsgBox(0, "Error sending message", "Error code:" & @error & " Description:" & $rc)
  33. EndIf
  34. ;
  35. ; The UDF
  36. Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
  37. Local $objEmail = ObjCreate("CDO.Message")
  38. $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
  39. $objEmail.To = $s_ToAddress
  40. Local $i_Error = 0
  41. Local $i_Error_desciption = ""
  42. If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
  43. If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
  44. $objEmail.Subject = $s_Subject
  45. If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
  46. $objEmail.HTMLBody = $as_Body
  47. Else
  48. $objEmail.Textbody = $as_Body & @CRLF
  49. EndIf
  50. If $s_AttachFiles <> "" Then
  51. Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
  52. For $x = 1 To $S_Files2Attach[0]
  53. $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
  54. ;~ ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
  55. If FileExists($S_Files2Attach[$x]) Then
  56. ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
  57. $objEmail.AddAttachment($S_Files2Attach[$x])
  58. Else
  59. ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
  60. SetError(1)
  61. Return 0
  62. EndIf
  63. Next
  64. EndIf
  65. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  66. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
  67. If Number($IPPort) = 0 then $IPPort = 25
  68. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
  69. ;Authenticated SMTP
  70. If $s_Username <> "" Then
  71. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  72. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
  73. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
  74. EndIf
  75. If $ssl Then
  76. $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
  77. EndIf
  78. ;Update settings
  79. $objEmail.Configuration.Fields.Update
  80. ; Set Email Importance
  81. Switch $s_Importance
  82. Case "High"
  83. $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High"
  84. Case "Normal"
  85. $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal"
  86. Case "Low"
  87. $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low"
  88. EndSwitch
  89. $objEmail.Fields.Update
  90. ; Sent the Message
  91. $objEmail.Send
  92. If @error Then
  93. SetError(2)
  94. Return $oMyRet[1]
  95. EndIf
  96. $objEmail=""
  97. EndFunc ;==>_INetSmtpMailCom
  98. ;
  99. ;
  100. ; Com Error Handler
  101. Func MyErrFunc()
  102. $HexNumber = Hex($oMyError.number, 8)
  103. $oMyRet[0] = $HexNumber
  104. $oMyRet[1] = StringStripWS($oMyError.description, 3)
  105. ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF)
  106. SetError(1); something to check for when this function returns
  107. Return
  108. EndFunc ;==>MyErrFunc

comments powered by Disqus