Created
April 9, 2019 22:51
-
-
Save jake-yeg/7190b274f12a750901c68b7bac19459c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHPMailer simple contact form example. | |
* If you want to accept and send uploads in your form, look at the send_file_upload example. | |
*/ | |
//Import the PHPMailer class into the global namespace | |
use PHPMailer\PHPMailer\PHPMailer; | |
require '../vendor/autoload.php'; | |
if (isset($_POST['to'])) { | |
$err = false; | |
$msg = ''; | |
$email = ''; | |
//Apply some basic validation and filtering to the subject | |
if (array_key_exists('subject', $_POST)) { | |
$subject = substr(strip_tags($_POST['subject']), 0, 255); | |
} else { | |
$subject = 'No subject given'; | |
} | |
//Apply some basic validation and filtering to the query | |
if (array_key_exists('query', $_POST)) { | |
//Limit length and strip HTML tags | |
$query = substr(strip_tags($_POST['query']), 0, 16384); | |
} else { | |
$query = ''; | |
$msg = 'No query provided!'; | |
$err = true; | |
} | |
//Apply some basic validation and filtering to the name | |
if (array_key_exists('name', $_POST)) { | |
//Limit length and strip HTML tags | |
$name = substr(strip_tags($_POST['name']), 0, 255); | |
} else { | |
$name = ''; | |
} | |
//Validate to address | |
//Never allow arbitrary input for the 'to' address as it will turn your form into a spam gateway! | |
//Substitute appropriate addresses from your own domain, or simply use a single, fixed address | |
if (array_key_exists('to', $_POST) and in_array($_POST['to'], ['sales', 'support', 'accounts'])) { | |
$to = $_POST['to'] . '@example.com'; | |
} else { | |
$to = '[email protected]'; | |
} | |
//Make sure the address they provided is valid before trying to use it | |
if (array_key_exists('email', $_POST) and PHPMailer::validateAddress($_POST['email'])) { | |
$email = $_POST['email']; | |
} else { | |
$msg .= "Error: invalid email address provided"; | |
$err = true; | |
} | |
if (!$err) { | |
$mail = new PHPMailer; | |
$mail->isSMTP(); | |
$mail->Host = 'localhost'; | |
$mail->Port = 2500; | |
$mail->CharSet = 'utf-8'; | |
//It's important not to use the submitter's address as the from address as it's forgery, | |
//which will cause your messages to fail SPF checks. | |
//Use an address in your own domain as the from address, put the submitter's address in a reply-to | |
$mail->setFrom('[email protected]', (empty($name) ? 'Contact form' : $name)); | |
$mail->addAddress($to); | |
$mail->addReplyTo($email, $name); | |
$mail->Subject = 'Contact form: ' . $subject; | |
$mail->Body = "Contact form submission\n\n" . $query; | |
if (!$mail->send()) { | |
$msg .= "Mailer Error: " . $mail->ErrorInfo; | |
} else { | |
$msg .= "Message sent!"; | |
} | |
} | |
echo json_encode([ | |
'success' => $err, | |
'message' => $msg | |
]); | |
exit; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>PHPMailer Contact Form</title> | |
</head> | |
<body> | |
<h1>Contact us</h1> | |
<div id="flash"></div> | |
<form method="post" id="form"> | |
<label for="to">Send to:</label> | |
<select name="to" id="to"> | |
<option value="sales">Sales</option> | |
<option value="support" selected="selected">Support</option> | |
<option value="accounts">Accounts</option> | |
</select><br> | |
<label for="subject">Subject: <input type="text" name="subject" id="subject" maxlength="255"></label><br> | |
<label for="name">Your name: <input type="text" name="name" id="name" maxlength="255"></label><br> | |
<label for="email">Your email address: <input type="email" name="email" id="email" maxlength="255"></label><br> | |
<label for="query">Your question:</label><br> | |
<textarea cols="30" rows="8" name="query" id="query" placeholder="Your question"></textarea><br> | |
<button type="button" id="submit">Submit</button> | |
</form> | |
<script> | |
function handleResponse(result) | |
{ | |
$('#flash').css('color','green'); | |
$('#flash').text(result.message); | |
} | |
function onError(result) | |
{ | |
$('#flash').css('color','red'); | |
$('#flash').text(result.message); | |
} | |
$('#submit').click(function() { | |
var formData = $('form').serialize(); | |
$.ajax({ | |
url: 'simple_jquery_form.php', | |
data: data, | |
type: 'POST', | |
success: handleResponse, | |
error: onError | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment