home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / SRC / phonemsg.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  11.0 KB  |  374 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. ' If this is from a form submission then we send a message.
  15. ' O/w we simply show the form for them to fill out.
  16. If Request.Form("action") = "send" Then
  17.     ' They've already seen and filled out the form.
  18.     ' Start the message sending process...
  19.     
  20.     Dim strTo       ' Who the message is for
  21.     Dim strTakenBy  ' Who answered the phone 
  22.     Dim strCaller   ' Who was calling
  23.     Dim strSubject  ' Out email subject line
  24.     Dim strBody     ' Our email message body
  25.     Dim bMsgSent    ' Boolean indication success or failure
  26.  
  27.     ' Get the users involed
  28.     strTo      = Request.Form("to")
  29.     strTakenBy = Request.Form("takenby")
  30.     strCaller  = Request.Form("caller")
  31.     
  32.     ' Build our subject line
  33.     strSubject = "Phone Msg: " & strCaller
  34.  
  35.     ' Build our message body
  36.     strBody = ""
  37.     strBody = strBody & "You got a phone call." & vbCrLf & vbCrLf
  38.     strBody = strBody & " From: "  & Request.Form("mrs") & " " & strCaller & vbCrLf
  39.     strBody = strBody & "   Of: "  & Request.Form("company") & vbCrLf
  40.     strBody = strBody & "   On: "  & Request.Form("date") & vbCrLf
  41.     strBody = strBody & "   At: "  & Request.Form("time") & vbCrLf
  42.     strBody = strBody & "Phone: "  & Request.Form("phone") & vbCrLf
  43.     strBody = strBody & "Notes: "  & Request.Form("notes") & vbCrLf
  44.     strBody = strBody & vbCrLf & "Message:" & vbCrLf
  45.     strBody = strBody & Request.Form("message") & vbCrLf
  46.  
  47.     ' Send the message and store status sent back
  48.     bMsgSent = SendEmail(strTakenBy, strTo, strSubject, strBody)
  49.  
  50.     ' Display either success or failure message
  51.     If bMsgSent Then
  52.         %>
  53.         <p>
  54.         <b>Your message has been sent to <%= Server.HTMLEncode(strTo) %>.</b>
  55.         </p>
  56.         <p>
  57.         A preview of your message is below:
  58.         </p>
  59.         
  60.         <p>
  61.         <%= Replace(Server.HTMLEncode(strBody), vbCrLf, "<br />" & vbCrLf) %>
  62.         </p>
  63.         <%
  64.     Else
  65.         %>
  66.         <p>
  67.         There was a problem sending your message, please try
  68.         again later or notify the recipient via another method.
  69.         </p>
  70.         <%
  71.     End If
  72. Else
  73.     ' This means we're displaying the form so here goes...
  74.     
  75.     ' To make this interesting I'm getting a list of employees
  76.     ' from a DB or text file so you don't have to keep typing
  77.     ' the names into the form fields.
  78.  
  79.     ' The recordset is for the employee list.  I then build a
  80.     ' set of options for use with any select box and store it
  81.     ' in the string variable for multiple uses.  The last var
  82.     ' is just a temp building area.
  83.     Dim rstEmployeeList
  84.     Dim strEmpOptions
  85.     Dim strTemp
  86.  
  87.     ' Expected format for our recordset is: first, last, email
  88.     ' Both the DB and Text File routines work and are listed
  89.     ' below.  I was just going to do a text file, but I figured
  90.     ' people would immediately want to hook this up to their
  91.     ' employee DB so I added that capability as well.
  92.     ' Modifying the DB function to work with your DB should
  93.     ' be pretty straight forward.
  94.     '
  95.     ' Make sure only one of the lines below is un-commented
  96.     ' or you'll be doing twice the work you need to!
  97.     Set rstEmployeeList = GetCompanyListFromFile(Server.MapPath("phonemsg.txt"))
  98.     'Set rstEmployeeList = GetCompanyListFromDB
  99.  
  100.     ' Start with first employee
  101.     rstEmployeeList.MoveFirst
  102.  
  103.     ' Loop through the RS and build the option string.
  104.     Do While Not rstEmployeeList.EOF
  105.         strTemp = rstEmployeeList.Fields("first").Value & " " _
  106.             & rstEmployeeList.Fields("last").Value & " " _
  107.             & "<" & rstEmployeeList.Fields("email").Value & ">"
  108.         
  109.         strEmpOptions = strEmpOptions & "<option value=""" & strTemp & """>" _
  110.             & strTemp & "</option>" & vbCrLf
  111.         
  112.         rstEmployeeList.MoveNext
  113.     Loop
  114.     
  115.     ' Close and dispose of our RS
  116.     rstEmployeeList.Close
  117.     Set rstEmployeeList = Nothing
  118.     
  119.     ' Now we just build our form.
  120.     ' The message for section was originally built from the same
  121.     ' string as shown in the commented out section, but so you can
  122.     ' play with it on our site, I thought I should open it up to
  123.     ' let you enter any address.
  124.     %>
  125.  
  126.     <form action="<%= Request.ServerVariables("URL") %>" method="post">
  127.     <input type="hidden" name="action" value="send" />
  128.     <table border="0" cellspacing="0" cellpadding="0"><tr><td>
  129.     <table border="0" cellspacing="2" cellpadding="2">
  130.         <tr>
  131.             <td colspan="4" align="center">
  132.                 <font size="+2"><b>While You Were Out</b></font>
  133.             </td>
  134.         </tr>
  135.         <tr>
  136.             <td align="right">Message For:</td>
  137.             <td colspan="3">
  138.                 <input type="text" name="to" value="Enter Email" size="30" />
  139.                 <!--
  140.                 <select name="to" />
  141.                 <%= strEmpOptions %>
  142.                 </select>
  143.                 -->
  144.             </td>
  145.         </tr>
  146.         <tr>
  147.             <td align="right">Taken By:</td>
  148.             <td colspan="3">
  149.                 <select name="takenby" />
  150.                 <%= strEmpOptions %>
  151.                 </select>
  152.             </td>
  153.         </tr>
  154.         <tr>
  155.             <td align="right">Date:</td>
  156.             <td><input type="text" name="date" value="<%= Date() %>" size="10" /></td>
  157.             <td align="right">Time:</td>
  158.             <td><input type="text" name="time" value="<%= Time() %>" size="10" /></td>
  159.             </td>
  160.         </tr>
  161.         <tr>
  162.             <td align="right">
  163.                 <select name="mrs">
  164.                     <option>Mr.</option>
  165.                     <option>Mrs.</option>
  166.                     <option>Ms.</option>
  167.                 </select>
  168.             </td>
  169.             <td colspan="3"><input type="text" name="caller" size="30" /></td>
  170.         </tr>
  171.         <tr>
  172.             <td align="right">Company:</td>
  173.             <td colspan="3"><input type="text" name="company" size="30" /></td>
  174.         </tr>
  175.         <tr>
  176.             <td align="right">Phone:</td>
  177.             <td colspan="3"><input type="text" name="phone" size="30" /></td>
  178.         </tr>
  179.         <tr>
  180.             <td>
  181.             </td>
  182.             <td colspan="3">
  183.                 <table border="0" cellspacing="1" cellpadding="0">
  184.                 <tr>
  185.                     <td><input type="checkbox" name="notes" value="Returned Call" />Returned Call</input></td>
  186.                     <td><input type="checkbox" name="notes" value="Please Call" />Please Call</input></td>
  187.                 </tr>
  188.                 <tr>
  189.                     <td><input type="checkbox" name="notes" value="Will Call Again" />Will Call Again</input></td>
  190.                     <td><input type="checkbox" name="notes" value="Urgent" />Urgent</input></td>
  191.                 </tr>
  192.                 </table>
  193.             </td>
  194.         </tr>
  195.     </table>
  196.     <table border="0" cellspacing="2" cellpadding="2">
  197.         <tr>
  198.             <td colspan="4">
  199.                 Message:<br />
  200.                 <textarea name="message" cols="40" rows="10" wrap="virtual"></textarea>
  201.             </td>
  202.         </tr>
  203.         <tr>
  204.             <td colspan="4" align="right">
  205.                 <input type="submit" value="Send Message" />
  206.             </td>
  207.         </tr>
  208.     </table>
  209.     </td></tr></table>
  210.     </form>
  211.     <%
  212. End If
  213.  
  214. ' Reads the employee list in from a file and
  215. ' returns a recordset containing the data.
  216. Function GetCompanyListFromFile(strFileFullPath)
  217.     Const ForReading = 1
  218.     Const adVarChar = 200
  219.     
  220.     Const FieldDelimiter = "|"
  221.     ' This won't work:
  222.     'Const RecordDelimiter = vbCrLf
  223.     ' So...
  224.     Dim RecordDelimiter
  225.     RecordDelimiter = vbCrLf
  226.     ' Anyone know if this can be implemented as a Const?
  227.     ' If so I'd love to see it... mailto:john@asp101.com
  228.     
  229.     Dim objFSO, objFile
  230.     Dim strFile
  231.     Dim arrCompanyList
  232.     Dim rstTemp
  233.     Dim arrTemp
  234.     Dim I
  235.  
  236.     ' Read in the entire file
  237.     Set objFSO  = Server.CreateObject("Scripting.FileSystemObject")
  238.     Set objFile = objFSO.OpenTextFile(strFileFullPath, ForReading, False)
  239.     strFile = objFile.ReadAll
  240.     objFile.Close
  241.     Set objFile = Nothing
  242.     Set objFSO  = Nothing
  243.  
  244.     ' Deal with trailing delimiters
  245.     Do While Right(strFile, Len(RecordDelimiter)) = RecordDelimiter
  246.         strFile = Left(strFile, Len(strFile) - Len(RecordDelimiter))
  247.     Loop
  248.  
  249.     ' Split each line into an array
  250.     arrCompanyList = Split(strFile, RecordDelimiter)
  251.  
  252.     ' Set up our new RS
  253.     Set rstTemp = Server.CreateObject("ADODB.Recordset")
  254.     rstTemp.Fields.Append "first", adVarChar, 255
  255.     rstTemp.Fields.Append "last", adVarChar, 255
  256.     rstTemp.Fields.Append "email", adVarChar, 255
  257.     
  258.     ' Open it up
  259.     rstTemp.Open
  260.     
  261.     ' Loop through the array adding entries to the RS
  262.     For I = LBound(arrCompanyList) To UBound(arrCompanyList)
  263.         arrTemp = Split(arrCompanyList(I), FieldDelimiter)
  264.         
  265.         rstTemp.AddNew
  266.         rstTemp.Fields("first").Value = arrTemp(0)
  267.         rstTemp.Fields("last").Value = arrTemp(1)
  268.         rstTemp.Fields("email").Value = arrTemp(2)
  269.         rstTemp.Update
  270.     Next
  271.     
  272.     ' Set the RS as the functions return value
  273.     Set GetCompanyListFromFile = rstTemp
  274. End Function
  275.  
  276. ' This is simply a skeleton for you to use since I knew
  277. ' I'd get questions about it if I didn't provide one.
  278. Function GetCompanyListFromDB()
  279.     Const adUseClient = 3
  280.     Const adOpenStatic = 3
  281.     Const adLockReadOnly = 1
  282.     Const adCmdText = &H0001
  283.  
  284.     Dim cnnTemp, rstTemp
  285.     
  286.     ' Connect to our DB
  287.     Set cnnTemp = Server.CreateObject("ADODB.Connection")
  288.     cnnTemp.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" _
  289.         & Server.MapPath("phonemsg.mdb") & "; User Id=admin; " _
  290.         & "Password=;"
  291.     
  292.     ' Create a RS that we can disconnect
  293.     Set rstTemp = Server.CreateObject("ADODB.Recordset")
  294.     rstTemp.CursorLocation = adUseClient
  295.     Set rstTemp.ActiveConnection = cnnTemp
  296.     
  297.     ' Get the data... this is where you could change the
  298.     ' sort order if you wanted to.
  299.     rstTemp.Open "SELECT first, last, email " _
  300.         & "FROM tblPhoneList ORDER BY last;" _
  301.         , , adOpenStatic, adLockReadOnly, adCmdText
  302.  
  303.     ' Diconnect    
  304.     Set rstTemp.ActiveConnection = Nothing
  305.     
  306.     ' Close and dispose of our connection
  307.     cnnTemp.Close
  308.     Set cnnTemp = Nothing
  309.  
  310.     ' Set the RS as the functions return value
  311.     Set GetCompanyListFromDB = rstTemp
  312. End Function
  313.  
  314. ' Simply sends a basic email message
  315. Function SendEmail(strFrom, strTo, strSubject, strBody)
  316.     On Error Resume Next
  317.     
  318.     Dim objMessage
  319.     Dim bSuccess
  320.  
  321.     ' Set default to success
  322.     bSuccess = True
  323.  
  324.     ' Quick check for valid email addr
  325.     If IsValidEmail(strTo) Then
  326.         ' Note that I'm using the Win2000 CDO and not CDONTS!
  327.         ' Could be either, but I figured I'd let you guys
  328.         ' see the new syntax since I rarely use it.
  329.         Set objMessage = Server.CreateObject("CDO.Message")
  330.         objMessage.To       = strTo
  331.         objMessage.From     = strFrom
  332.         objMessage.Subject  = strSubject
  333.         objMessage.TextBody = strBody
  334.         objMessage.Send
  335.         Set objMessage = Nothing
  336.     Else
  337.         ' If email is invalid abort w/ a failure code.
  338.         bSuccess = False
  339.     End If
  340.  
  341.     ' Check for errors
  342.     If Err.number <> 0 Then
  343.         bSuccess = False
  344.     End If
  345.     
  346.     ' Set return status
  347.     SendEmail = bSuccess
  348. End Function
  349.  
  350. ' A quick email syntax checker.  It's pretty lame
  351. ' but it's quick and easy and will catch people
  352. ' who enter nothing.  Note it's pretty darn lax
  353. ' because I allow this format:
  354. '  User Name <username@domain.com>
  355. Function IsValidEmail(strEmail)
  356.     Dim bIsValid
  357.     bIsValid = True
  358.     
  359.     If Len(strEmail) < 5 Then
  360.         bIsValid = False
  361.     Else
  362.         If InStr(1, strEmail, "@", 1) < 2 Then
  363.             bIsValid = False
  364.         Else
  365.             If InStrRev(strEmail, ".") < InStr(1, strEmail, "@", 1) + 2 Then
  366.                 bIsValid = False
  367.             End If
  368.         End If
  369.     End If
  370.  
  371.     IsValidEmail = bIsValid
  372. End Function
  373. %>
  374.