email. if the php server is an email server or is aware of which server is the email server, then...

18
Email

Upload: vivien-sheena-mckenzie

Post on 24-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Email

Email

• 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.)

Design of a form

Display code (no email yet)

Result so far (no email yet)

Searching for an email regular expression (at http://regexlib.com/default.aspx)

Pick one with a good rating

Client-side email validation

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)

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

– Author: David Lott

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.

Server-side: email code

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 "<p>You entered an invalid email and we cannot confirm for registration.</p>";}

• Validate email using regular expression. Remember never rely on your client-side validation – you need to validate on the server side as well.

Server-side: email code (Cont.)

else{

$return_address="[email protected]"; //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.

Server-side: email code (Cont.)

$body = "<html><body>";$body = $body . "<p>$firstName $lastName, thank you for registering for ";$body = $body . " the following workshop.</p>";$body = $body . "<p>$workshops[$session]</p>";$body = $body . "<p>You have selected a $lunch[$lunchChoice] for

lunch.</p>";$body = $body . "</body></html>";

• 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.

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

Result (this part is the same as before)

Resulting email