Send Mail
Recall that we are still within our original, single page. We have collected our information, validated it, and added a record to our database. We still have all of our information in Page Memory. All we have to do is assemble it, format it a bit so that it is intelligible to us when we get it (as opposed to a single line of unbroken data), and send it to ourselves.
Since this is a fairly common task, sending email, it might be a good idea to write a function that can be called easily whenever we want it. The pseudo-code for it will look much like this:
SendMail_Function (ToWho, FromWho, Subject, Message)
Declare variables
Get valid SMTP object
Set SMTP object properties (assemble the email message)
Send Mail.
You would store this function somewhere on your server in a file that you then include in the page that needs to perform this function. You might store a great many useful little functions in a common library (hint, hint).
Returning to our page code, we must assemble the message parts for our function to use. the email addresses for To and From as well as the Subject are no trouble. The message will need to be formatted, however. You will need to write Line Breaks (or Carriage Returns) into the message text. The process will look like this:
Declare a Message_Variable
Add a definition string + your form data string + Line Break to Message_Variable
Repeat until done
Once completed you need to call your function to do the work
SendMail_Function("You@yourdomain.com",
" Visitor@theirdomain.com",
"Contact From WebSite",
Message_Variable)
- At this point I feel it is probably a good idea to add one more piece of extraneous information. Pay attention, however, because it is NOT trivial. If you put your email address into your webpage, even in code sections, it is vulnerable to unintended use by SPAMBOTs. In short, do not use your email address as text without taking some precaution to shield it from illicit use. The simplest mechanism - simple but effective - is to HTML Encode your email address text.
And, completing our page, redirect the user to a new page; Home, Thank You, etc. You have completed a series of tasks all within a single page.
Food
For
Thought
