login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > ASP > Handige scripts > Simpel e-mail script

Simpel e-mail script

Auteur: humor - 31 augustus 2004 - 13:48 - Gekeurd door: Dennisvb - Hits: 3630 - Aantal punten: 2.43 (7 stemmen)




Hallo,

Ik heb dit script al lang op m'n pc staan. Ik heb het ooit eens van een of andere site gehaald en er de bugs uitgehaald. Normaal gezien moet het nu perfect werken.
De uitleg staat in de code (na ') in het engels omdat het de bedoeling is dat ik dit script op meerdere sites kan posten.

Code:
  1. <%
  2. Dim strTo, strSubject, strBody 'Strings for recipient, subject, boby
  3. Dim objCDOMail 'The CDO object
  4.  
  5.  
  6. 'First we'll read in the values entered
  7. strTo = Request.Form("to")
  8.  
  9. 'These would read the message subject and body if we let you enter it
  10. 'strSubject = Request.Form("subject")
  11. 'strBody = Request.Form("body")
  12.  
  13. ' Both of these should be changed before you run this script.
  14. strSubject = "Sample E-mail sent from ASP 101!"
  15.  
  16. ' This is multi-lined simply for readability
  17. strBody = "This message was sent from a sample at http://www.asp101.com. "
  18. strBody = strBody & "It is used to show people how to send e-mail from an "
  19. strBody = strBody & "Active Server Page. If you did not request this "
  20. strBody = strBody & "e-mail yourself, your address was entered by one of "
  21. strBody = strBody & "our visitors. We do not store these e-mail addresses."
  22. strBody = strBody & " Please address all concerns to webmaster@asp101.com."
  23.  
  24. ' Some spacing:
  25. strBody = strBody & vbCrLf & vbCrLf
  26.  
  27. strBody = strBody & "This was sent to: "
  28.  
  29. ' A lot of people have asked how to use form data in the emails so
  30. ' I added this line to the sample as an example of incorporating form
  31. ' data in the body of the email.
  32. strBody = strBody & Request.Form("to")
  33.  
  34. ' A final carriage return for good measure!
  35. strBody = strBody & vbCrLf
  36.  
  37.  
  38. 'Ok we've got the values now on to the point of the script.
  39.  
  40. 'We just check to see if someone has entered anything into the to field.
  41. 'If it's equal to nothing we show the form, otherwise we send the message.
  42. 'If you were doing this for real you might want to check other fields too
  43. 'and do a little entry validation like checking for valid syntax etc.
  44.  
  45. ' Note: I was getting so many bad addresses being entered and bounced
  46. ' back to me by mailservers that I've added a quick validation routine.
  47. If strTo = "" Or Not IsValidEmail(strTo) Then
  48. %>
  49. <FORM ACTION="./email.asp" METHOD="post">
  50. Enter your e-mail address:<BR>
  51. <INPUT TYPE="text" NAME="to" SIZE="30"></INPUT>
  52.  
  53. <!-- These would be used if we decided to let you edit them
  54. Subject:&nbsp;
  55. <INPUT TYPE="text" NAME="subject" SIZE="30"></INPUT><BR>
  56.  
  57. Message:&nbsp;
  58. <TEXTAREA NAME="body" ROWS="10" COLS="40" WRAP="virtual"></TEXTAREA><BR>
  59. -->
  60.  
  61. <INPUT TYPE="submit" VALUE="Send Mail!"></INPUT>
  62. </FORM>
  63. <%
  64. Else
  65. ' Create an instance of the NewMail object.
  66. Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
  67.  
  68. ' Set the properties of the object
  69.  
  70. '***********************************************************
  71. ' PLEASE CHANGE THESE SO WE DON'T APPEAR TO BE SENDING YOUR
  72. ' EMAIL. WE ALSO DON'T WANT THE EMAILS TO GET SENT TO US
  73. ' WHEN SOMETHING GOES WRONG WITH YOUR SCRIPT... THANKS
  74. '***********************************************************
  75.  
  76. ' This syntax works fine
  77. 'objCDOMail.From = "user@domain.com"
  78. ' But this gets you the appearance of a real name!
  79. objCDOMail.From = "User Name <user@domain.com>"
  80. objCDOMail.To = strTo
  81. objCDOMail.Subject = strSubject
  82. objCDOMail.Body = strBody
  83.  
  84. ' There are lots of other properties you can use.
  85. ' You can send HTML e-mail, attachments, etc...
  86. ' You can also modify most aspects of the message
  87. ' like importance, custom headers, ...
  88. ' Check the documentation for a full list as well
  89. ' as the correct syntax.
  90.  
  91. ' Some of the more useful ones I've included samples of here:
  92. 'objCDOMail.Cc = "user@domain.com;user@domain.com"
  93. 'objCDOMail.Bcc = "user@domain.com;user@domain.com"
  94. 'objCDOMail.Importance = 1 '(0=Low, 1=Normal, 2=High)
  95. 'objCDOMail.AttachFile "c:\path\filename.txt", "filename.txt"
  96.  
  97. ' I've had several requests for how to send HTML email.
  98. ' To do so simply set the body format to HTML and then
  99. ' compose your body using standard HTML tags.
  100. 'objCDOMail.BodyFormat = 0 ' CdoBodyFormatHTML
  101.  
  102. 'Outlook gives you grief unless you also set:
  103. 'objCDOMail.MailFormat = 0 ' CdoMailFormatMime
  104.  
  105. ' THIS LINE SHOULD BE UNCOMMENTED TO ACTUALLY SEND THE
  106. ' MESSAGE. PLEASE BE SURE YOU HAVE APPROPRIATE VALUES
  107. ' FOR TO AND FROM ADDRESSES AND HAVE CHANGED THE MESSAGE
  108. ' SUBJECT AND BODY BEFORE UNCOMMENTING THIS.
  109. ' Send the message!
  110. 'objCDOMail.Send
  111.  
  112. ' Set the object to nothing because it immediately becomes
  113. ' invalid after calling the Send method.
  114. Set objCDOMail = Nothing
  115.  
  116. 'Response.Write "Message sent to " & strTo & "!"
  117. Response.Write "Message ARE NO LONGER BEING SENT because of all the abuse the system was receiving!"
  118. End If
  119. ' End page logic
  120. %>
  121.  
  122. <% ' Only functions and subs follow!
  123.  
  124. ' A quick email syntax checker. It's not perfect,
  125. ' but it's quick and easy and will catch most of
  126. ' the bad addresses than people type in.
  127. Function IsValidEmail(strEmail)
  128. Dim bIsValid
  129. bIsValid = True
  130.  
  131. If Len(strEmail) < 5 Then
  132. bIsValid = False
  133. Else
  134. If Instr(1, strEmail, " ") <> 0 Then
  135. bIsValid = False
  136. Else
  137. If InStr(1, strEmail, "@", 1) < 2 Then
  138. bIsValid = False
  139. Else
  140. If InStrRev(strEmail, ".") < InStr(1, strEmail, "@", 1) + 2 Then
  141. bIsValid = False
  142. End If
  143. End If
  144. End If
  145. End If
  146.  
  147. IsValidEmail = bIsValid
  148. End Function
  149. %>


Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.029s