home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / email.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  5.2 KB  |  156 lines

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