Created
November 11, 2019 17:52
-
-
Save aindriu80/2341b7dcdca113965b66d06f3a25dfef to your computer and use it in GitHub Desktop.
PHP and HTML
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 | |
if($_POST['Honeypot'] != ''){ | |
die("You spammer!"); | |
} | |
// comment.php | |
if (!empty($_POST['fax'])) { | |
// It's SPAM | |
http_response_code(400); | |
exit; | |
} | |
/* | |
THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION | |
*/ | |
require 'PHPMailer-master/PHPMailerAutoload.php'; | |
/* | |
* CONFIGURE EVERYTHING HERE | |
*/ | |
// an email address that will be in the From field of the email. | |
$fromEmail = '[email protected]'; | |
$fromName = 'Website contact form'; | |
// an email address that will receive the email with the output of the form | |
$sendToEmail = '[email protected]'; | |
$sendToName = 'Name'; | |
// subject of the email | |
$subject = 'New message via contact form on X Website'; | |
// form field names and their translations. | |
// array variable name => Text to appear in the email | |
$fields = array('name' => 'Name', 'surname' => 'Surname', 'subject' => 'Subject', 'email' => 'Email', 'message' => 'Message'); | |
// message that will be displayed when everything is OK :) | |
$okMessage = 'Contact form successfully submitted. Thank you, we will get back to you soon!'; | |
// If something goes wrong, we will display this message. | |
$errorMessage = 'There was an error while submitting the form. Please try again later'; | |
/* | |
* LET'S DO THE SENDING | |
*/ | |
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0); | |
error_reporting(E_ALL & ~E_NOTICE); | |
try | |
{ | |
if($_POST['Honeypot'] != ''){ | |
die("You spammer!"); | |
} | |
// comment.php | |
if (!empty($_POST['fax'])) { | |
// It's SPAM | |
http_response_code(400); | |
exit; | |
} | |
if(count($_POST) == 0) throw new \Exception('Form is empty'); | |
$emailTextHtml = "<h4>You have a new message from the contact form on the X website.</h4><hr>"; | |
$emailTextHtml .= "<table>"; | |
foreach ($_POST as $key => $value) { | |
// If the field exists in the $fields array, include it in the email | |
if (isset($fields[$key])) { | |
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>"; | |
} | |
} | |
$emailTextHtml .= "</table><hr>"; | |
$emailTextHtml .= "<p></p>"; | |
$mail = new PHPMailer; | |
$mail->setFrom($fromEmail, $fromName); | |
$mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress(); | |
$mail->addReplyTo($from); | |
$mail->isHTML(true); | |
$mail->Subject = $subject; | |
// $mail->Subject = ($fromSubject); | |
// $mail->setFrom($fromSubject); | |
$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy | |
if(!$mail->send()) { | |
throw new \Exception('I could not send the email.' . $mail->ErrorInfo); | |
} | |
$responseArray = array('type' => 'success', 'message' => $okMessage); | |
} | |
catch (\Exception $e) | |
{ | |
// $responseArray = array('type' => 'danger', 'message' => $errorMessage); | |
$responseArray = array('type' => 'danger', 'message' => $e->getMessage()); | |
} | |
// if requested by AJAX request return JSON response | |
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { | |
$encoded = json_encode($responseArray); | |
header('Content-Type: application/json'); | |
echo $encoded; | |
} | |
// else just display the message | |
else { | |
echo $responseArray['message']; | |
} | |
<div class="col-lg-5 col-md-8"> | |
<div class="form"> | |
<div class="form-group"> | |
<input type="text" name="name" class="form-control" id="name" placeholder="Full Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" /> | |
<div class="validation"></div> | |
</div> | |
<div class="form-group"> | |
<input type="email" class="form-control" name="email" id="email" placeholder="Email" data-rule="email" data-msg="Please enter a valid email" /> | |
<div class="validation"></div> | |
</div> | |
<div class="form-group"> | |
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" /> | |
<div class="validation"></div> | |
</div> | |
<div class="form-group"> | |
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea> | |
<div class="validation"></div> | |
</div> | |
<p class="fax"> | |
<label>Fax</label> | |
<input type="text" name="your_fax"> | |
</p> | |
<input type="text" id="honeypot" name="honeypot" placeholder="Leave Blank If Human" autocomplete="off"> | |
<div class="text-center"><button type="submit">Send Message</button></div> | |
</form> | |
</div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jhon Michael alburo Tababa
[email protected]
block 3 lot 12 sadaka 1 dakila st. batasan hills QUEZON CITY
1126
$500