Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'BootstrapLandinPage/lib/PHPMailer-master/src/Exception.php';
- require 'BootstrapLandinPage/lib/PHPMailer-master/src/PHPMailer.php';
- require 'BootstrapLandinPage/lib/PHPMailer-master/src/SMTP.php';
- $error_msg = array();
- $success_msg = array();
- $data = '';
- // prevent warnings or errors from displaying, else you won't get proper json result
- ini_set('display_errors',0);
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $error_msg['name'] = "Name is required";
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Zא-ת ]*$/",$name)) {
- $error_msg['name'] = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $error_msg['email'] = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $error_msg['email'] = "Invalid email format";
- }
- }
- if (empty($_POST["phone"])) {
- $error_msg['phone'] = "Phone is required";
- } else {
- $phone = test_input($_POST["phone"]);
- // check if e-mail address is well-formed
- if (!preg_match("/^(d[s-]?)?[([s-]{0,2}?d{3}[)]s-]{0,2}?d{3}[s-]?d{4}$/i",$phone)) {
- $error_msg['phone'] = "Invalid phone number";
- }
- }
- if (empty($_POST["subject"])) {
- $error_msg['subject'] = "Subject is required";
- }
- if (empty($_POST["message"])) {
- $error_msg['message'] = "Message is required";
- }
- if (empty($_POST["subject"])) {
- $subject = "";
- } else {
- $subject = test_input($_POST["subject"]);
- }
- if (empty($_POST["message"])) {
- $message = "";
- } else {
- $message = test_input($_POST["message"]);
- }
- if ($_POST['submit']) {
- if (mail ($to, $subject, $body, $from)) {
- echo '<p>Your message has been sent!</p>';
- } else {
- echo '<p>Something went wrong, go back and try again!</p>';
- }
- }
- if (empty($error_msg)){
- $message_body = '';
- unset($_POST['submit']);
- foreach ($_POST as $key => $value){
- $message_body .= "$key: $valuen";
- }
- $to = 'ilonaXXX@gmail.com';// some email I use
- $subjectm = 'Contact Form Submit';
- $mail = new PHPMailer(true);
- try {
- //Server settings
- $mail->SMTPDebug = 2;
- $mail->isSMTP();
- $mail->Host = 'smtp.gmail.com';
- $mail->SMTPAuth = true;
- $mail->Username = 'XXXX@gmail.com'; // my gmail
- $mail->Password = '*****';// my gmail password
- $mail->SMTPSecure = 'ssl';
- $mail->Port = 465;// my xampp port is 80 , which I tried to put here ,but it wont work anyway
- //Recipients
- $mail->setFrom('xxxxx@gmail.com', 'Ilona Sem');
- $mail->addAddress($email, $name);
- //Content
- $mail->isHTML(true);
- $mail->Subject = 'Contact Form Submit';
- $mail->Body = $message;
- $mail->AltBody = preg_replace( "/ns+/", "n", rtrim(html_entity_decode(strip_tags($message))) ); // For clients that do not support html / "spam control"
- $mail->send();
- $data = array('success'=>'Message sent, thank you for contacting us!');
- outputJson($data);
- } catch (Exception $e) {
- $data = array('mail_error'=>'Could not send email');
- outputJson($data);
- //echo 'Mailer Error: ' . $mail->ErrorInfo; // Debug message that you can see in network tab in web developer browser extension
- }
- $name = $email = $phone = $message = $subject = '';
- }
- }
- // output json that you can parse with jquery
- function outputJson($data) {
- // output json that you can parse with jquery
- header('Content-Type: application/json');
- echo json_encode($data);
- exit;
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
Add Comment
Please, Sign In to add comment