index.html?reci pient=s&title=d &body=d&submit= Send!
PS. AJAX works behind the lines, you normally see nothing from AJAX itself.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"> <head> <title> Unimail </title> <script type="text/javascript" src="main.js"></script> <link rel="shortcut icon" href="/images/favicon.ico" /> <link rel="stylesheet" type="text/css" href="/styles/main.css" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta property="og:description" content="Free one-time use email" /> <meta property="og:title" content="Unimail email service" /> <meta name="author" content="Unimail" /> <meta property="og:site_name" content="Unimail" /> </head> <body> <div id="wrapper"> <div id="top"> <div class="header"> <img src="/images/email_logo4.jpg" alt="Unimail" /> </div> <div class="navig"> <ul class="navbar"> <li><a href="http://localhost/index.html"><span>Home</span></a></li> <li><a href="http://localhost/about.html">About us</a></li> <li><a href="#">FAQ</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Other services</a></li> </ul> </div> </div> <div id="body"> <br /> <p> Hello! Welcome to Unimail! With Unimail, you can send emails without registering. No inbox, no pre-chosen address, no name. One-time email! </p> <br /><br /> <form name="sendmail" action="" method="" class="sender"> <p>Recipient's address </p> <input name="recipient" type="text" id="recipient" /><br /> <p> Title </p><input name="title" type="text" id="title" /> <p> Body </p><textarea rows="15" cols="65" name="body" id="body" ></textarea><br /> <input name="submit" type="button" value="Send!" onclick="getObject(this.form);"/> <span id="error"> </span> </form> <p> Remember! You will not be able to receive any reply! </p> </div> <div id="footer"> <p> This site was created for occasional emailing. Please do not attempt to use this site to spam emails. </p> </div> </div> </body> </html>
function getObject(f) { var http; var url = "send.php"; var email = f.elements['recipient'].value; var subject = f.elements['title'].value; var body = f.elements['body'].value; var parameters = "e=" + email + "&s=" + subject + "&b=" + body; try { http = new XMLHttpRequest(); } catch(e) { try { http = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { http = new ActiveXObject("Microsoft.XMLHTTP"); } } function getServer() { if (http.readyState == 4) { if (http.responseText.match("Error") == "Error") { document.getElementById("error").style.color = "red"; document.getElementById("error").innerHTML = " " + http.responseText; } else { document.getElementById("error").style.color = "green"; document.getElementById("error").innerHTML = " " + http.responseText; } } } http.open("GET", url+"?"+parameters, true); http.onreadystatechange = getServer; http.send(null); }
<?php require_once('class.phpgmailer.php'); $emailRep = ""; $titleRep = ""; $bodyRep = ""; function verifyEmail($email) { if (filter_var($email, FILTER_VALIDATE_EMAIL) == true) { return true; } $emailRep = "Error: Invalid email address!"; } function verifyTitle($title) { if (strlen($title) > 0) { return true; } $titleRep = "Error: Empty titles are not allowed!"; } function verifyBody($body) { if (strlen($body) > 19) { return true; } $bodyRep = "Error: Your email must consist of at least 20 character!"; } $to = $_GET['e']; $subject = $_GET['s']; $message = $_GET['b']; if (verifyEmail($to) && verifyTitle($subject) && verifyBody($message)) { $mail = new PHPGMailer(); $mail->Username = 'unimail.auto@gmail.com'; $mail->Password = '*******************'; $mail->From = 'unimail.auto@gmail.com'; $mail->FromName = 'Unimail'; $mail->Subject = $subject; $mail->AddAddress($to); $mail->Body = $message; $mail->Send(); echo "Message sent!"; } else { echo $emailRep . "<br />" . $titleRep . "<br />" . $bodyRep; } ?>
function verifyTitle($text) { # abort if the text length is zero if (strlen(trim($text)) == 0) { throw new LengthException("Empty titles are not allowed!"); } # further input validation/sanitising return $text; }
UnexpectedValue Exception()
for verifyEmail() and LengthException ()
for verifyBody()).# I left off the function definitions here # start controlling the flow try { $title = verifyTitle($_GET['s']); $body = verifyBody($_GET['b']); $email = verifyEmail($_GET['e']); $mail = new PHPGMailer(); // etc. } # collect any errors and output the error message catch (Exception $e) { echo $e->getMessage(); } # end of flow control
Comment