Download presentation
Presentation is loading. Please wait.
Published byVivien Sheena McKenzie Modified over 9 years ago
1
Email
2
If the PHP server is an email server or is aware of which server is the email server, then one can write code that emails information. –For example, an email confirmation (Don’t become a spammer.) (Be careful about emailing sensitive inofrmation.)
3
Design of a form
4
Display code (no email yet)
5
Result so far (no email yet)
6
Searching for an email regular expression (at http://regexlib.com/default.aspx)
7
Pick one with a good rating
8
Client-side email validation
9
Client-side email validation (Cont.) var email = document.getElementById("txtEmail").value; email = email.replace(/^\s+|\s+$/g, ''); Get the string entered by the user from text field Use the string function replace and the regular expression for white space to make a JavaScript version of the trim function. (//http://lawrence.ecorp.net/inet/samples/regexp- format.php)
10
Client-side email validation (Cont.) var emailPattern = new RegExp(/^([\w\-\.]+)@((\[([0- 9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/); Declare and initialize a JavaScript regular expression. –Source: http://regexlib.com/Search.aspx?k=email http://regexlib.com/Search.aspx?k=email –Author: David Lott
11
Client-side email validation (Cont.) if(!email.match(emailPattern)) { alert("Please enter a proper email."); return false; } Do not send information to server if the user’s email does NOT (!) match the email pattern regular expression.
12
Server-side: email code
13
Server-side: email code (Cont.) if(!preg_match("/^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0- 9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/",$email)) { print " You entered an invalid email and we cannot confirm for registration. "; } Validate email using regular expression. Remember never rely on your client-side validation – you need to validate on the server side as well.
14
Server-side: email code (Cont.) else { $return_address="ACT@lasalle.edu"; //used below in $extra $extra = "from: $return_address\nContent-type: text/html; charset=iso-8859-1 "; $subject = "ACT Training Registration Confirmation"; If the email is valid, prepare some variable that will be used by the mail function Note that the return address becomes part of a longer variable that indicates that the email’s body will use HTML formatting.
15
Server-side: email code (Cont.) $body = " "; $body = $body. " $firstName $lastName, thank you for registering for "; $body = $body. " the following workshop. "; $body = $body. " $workshops[$session] "; $body = $body. " You have selected a $lunch[$lunchChoice] for lunch. "; $body = $body. " "; The body starts and ends with HTML tags and encloses the email’s text. Recall that the period. corresponds to concatenation, so $body = $body. “something” adds something to whatever value the variable $body already has.
16
Server-side: email code (Cont.) mail($email,$subject,$body,$extra); The mail function above has four arguments: –The first is the address to which the email will be sent. –The second is the subject field of the email –The third is the actually content of the message (here it was put in HTML format) –The fourth includes a return address as well as an indication that we are using HTML formatting
17
Result (this part is the same as before)
18
Resulting email
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.