PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://gitlab.com/skortekaas/PHP-SMTP-ContactForm
PHP | 319 lines | 185 code | 56 blank | 78 comment | 17 complexity | c2650cf19b0af96e8b9124d2eb0fb5a8 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * It is a fully responsive HTML5 - JavaScript contact form. It can send emails through the PHP mail() function or through a
  5. * SMTP server by making use of the PHPMailer class (https://github.com/phpmailer/phpmailer)
  6. *
  7. * @license GPL-3.0
  8. * @author Sven Kortekaas - https://skortekaas.nl
  9. * @version 1.0
  10. *
  11. * Copyright 2016 Sven Kortekaas
  12. *
  13. * This file is part of PHP-SMTP-ContactForm.
  14. *
  15. * PHP-SMTP-ContactForm is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * PHP-SMTP-ContactForm is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with PHP-SMTP-ContactForm. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. //Set this to TRUE for SMTP mail otherwise it will use the PHP mail() function
  30. $SMTPmail = FALSE;
  31. //If true - don't forget to set the SMTP server credentials
  32. if($SMTPmail) {
  33. $smtpServer = "smtp.domain.tld"; //Set the hostname of the mail server
  34. $smtpPort = 25; //Set the SMTP port number - likely to be 25, 465 or 587
  35. $smtpAuth = true; //Whether to use SMTP authentication
  36. //$smtpSecure = "tls"; //Set SSL or TLS
  37. $smtpUsername = "username@domain.tld"; //Username to use for SMTP authentication
  38. $smtpPassword = "password"; //Password to use for SMTP authentication
  39. }
  40. if(isset($_POST['submit'])):
  41. if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
  42. //Set here your GOOGLE RECAPTCHA2 SECRET KEY
  43. $secret = 'GOOGLE RECAPTCHA2 SECRET KEY';
  44. //Verify the recaptcha
  45. $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
  46. $responseData = json_decode($verifyResponse);
  47. //Getting all the values
  48. $name = !empty($_POST['name'])?$_POST['name']:'';
  49. $adres = !empty($_POST['adres'])?$_POST['adres']:'';
  50. $phone = !empty($_POST['phone'])?$_POST['phone']:'';
  51. $message = !empty($_POST['message'])?$_POST['message']:'';
  52. $email = !empty($_POST['email'])?$_POST['email']:'';
  53. $website = !empty($_POST['website'])?$_POST['website']:'';
  54. //The email content
  55. $htmlContent = "
  56. <h1>Email body title</h1>
  57. <p><b>Name: </b>".$name."</p>
  58. <p><b>Adress: </b>".$adres."</p>
  59. <p><b>Email: </b>".$email."</p>
  60. <p><b>Website: </b>".$website."</p>
  61. <p><b>Telephonenumber: </b>".$phone."</p>
  62. <p><b>Message: </b>".$message."</p>
  63. ";
  64. //Send the email to this adress
  65. $to = 'YOUREMAIL@DOMAIN.TLD';
  66. //Put the email subject in here
  67. $subject = 'EMAIL SUBJECT';
  68. //If the recaptcha verify succeeded continue
  69. if($responseData->success):
  70. //If SMTPmail is TRUE
  71. if($SMTPmail) {
  72. /*
  73. * This will send the email by SMTP making use of the PHPMailer class
  74. * https://github.com/phpmailer/phpmailer
  75. * You need to install this on your website
  76. */
  77. //SMTP needs accurate times, and the PHP time zone MUST be set
  78. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  79. date_default_timezone_set('Etc/UTC');
  80. //Set the right path to PHPMailerAutoload.php
  81. require 'PHPMailerAutoload.php';
  82. //Create a new PHPMailer instance
  83. $mail = new PHPMailer;
  84. //Tell PHPMailer to use SMTP
  85. $mail->isSMTP();
  86. //Enable SMTP debugging
  87. // 0 = off (for production use)
  88. // 1 = client messages
  89. // 2 = client and server messages
  90. $mail->SMTPDebug = 2;
  91. //Ask for HTML-friendly debug output
  92. $mail->Debugoutput = 'html';
  93. //Set the hostname of the mail server
  94. $mail->Host = $smtpServer;
  95. //Set the SMTP port number - likely to be 25, 465 or 587
  96. $mail->Port = $smtpPort;
  97. //Whether to use SMTP authentication
  98. $mail->SMTPAuth = $smtpAuth;
  99. //Set SSL or TLS
  100. //$mail->SMTPSecure = $smtpSecure;
  101. //Username to use for SMTP authentication
  102. $mail->Username = $smtpUsername;
  103. //Password to use for SMTP authentication
  104. $mail->Password = $smtpPassword;
  105. //Set who the message is to be sent from
  106. $mail->setFrom('username@domain.tld', $name);
  107. //Set who the message is to be sent to
  108. $mail->addAddress($to, 'John Doe');
  109. //Set who the message is to be sent to in CC
  110. //$mail->AddCC('email@domain.tld', 'John Doe');
  111. //Set the subject line
  112. $mail->Subject = $subject;
  113. //Read an HTML message body from an external file, convert referenced images to embedded,
  114. //convert HTML into a basic plain-text alternative body
  115. $mail->msgHTML($htmlContent);
  116. //Replace the plain text body with one created manually
  117. $mail->AltBody = $htmlContent;
  118. //send the message, check for errors
  119. if (!$mail->send()) {
  120. $succMsg = "Something went wrong. Please contact your webmaster. SMTP Mailer Error: " . $mail->ErrorInfo;
  121. } else {
  122. $succMsg = "Mailed with succes through SMTP.";
  123. }
  124. $name = '';
  125. $adres = '';
  126. $message = '';
  127. $phone = '';
  128. $email = '';
  129. $url = '';
  130. } else { //If SMTPmail is FALSE
  131. /*
  132. * This will send the email with the PHP mail() function
  133. */
  134. $headers = "MIME-Version: 1.0" . "\r\n";
  135. $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  136. $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
  137. if(mail($to,$subject,$htmlContent,$headers))
  138. {
  139. $succMsg = 'Mailed with succes with PHP mail()';
  140. } else {
  141. $succMsg = 'Something went wrong. Please contact your webmaster. PHP Mail() failed. Returned FALSE.';
  142. }
  143. $name = '';
  144. $adres = '';
  145. $phone = '';
  146. $message = '';
  147. $email = '';
  148. $url = '';
  149. }
  150. else:
  151. $errMsg = 'Anti-spam/Robot verification failed. Please try again.';
  152. endif;
  153. else:
  154. $errMsg = 'Check the reCAPTCHA box for anti-spam verification.';
  155. endif;
  156. else:
  157. $errMsg = '';
  158. $succMsg = '';
  159. $name = '';
  160. $adres = '';
  161. $phone = '';
  162. $message = '';
  163. $email = '';
  164. $url = '';
  165. endif;
  166. ?><!DOCTYPE html>
  167. <html lang="en">
  168. <head>
  169. <meta charset="UTF-8">
  170. <title>Website title</title>
  171. <meta name="viewport" content="width=device-width,initial-scale=1.0">
  172. <meta name="keywords" content="keyword,keyword,keyword,keyword">
  173. <meta name="description" content="Site description">
  174. <meta name="author" content="Sven Kortekaas - https://skortekaas.nl/">
  175. <link href="css/style.css" rel="stylesheet">
  176. <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  177. </head>
  178. <body>
  179. <div class="wrapper">
  180. <div id="main" style="padding:50px 0 0 0;">
  181. <!-- Form -->
  182. <form id="contact-form" action="" method="POST">
  183. <h4><?php if(!empty($errMsg)): ?><div class="errMsg"><?php echo $errMsg; ?></div><?php endif; ?></h4>
  184. <h4><?php if(!empty($succMsg)): ?><div class="succMsg"><?php echo $succMsg; ?></div><?php endif; ?></h4>
  185. <h3>Title</h3>
  186. <h4>Subtitle text.</h4>
  187. <div>
  188. <label>
  189. <span>Name: (required)</span>
  190. <input name="name" placeholder="Full name" type="text" tabindex="1" required autofocus>
  191. </label>
  192. </div>
  193. <div>
  194. <label>
  195. <span>Adress:</span>
  196. <input name="adres" placeholder="Your adress" type="text" tabindex="2">
  197. </label>
  198. </div>
  199. <div>
  200. <label>
  201. <span>Email: (required)</span>
  202. <input name="email" placeholder="Your email" type="email" tabindex="3" required>
  203. </label>
  204. </div>
  205. <div>
  206. <label>
  207. <span>Website:</span>
  208. <input name="website" placeholder="http://www.yourwebsite.tld" type="url" tabindex="4">
  209. </label>
  210. </div>
  211. <div>
  212. <label>
  213. <span>Telephonenumber:</span>
  214. <input name="phone" placeholder="Your telephonenumber" type="tel" tabindex="5">
  215. </label>
  216. </div>
  217. <div>
  218. <label>
  219. <span>Message:</span>
  220. <textarea name="message" placeholder="Your message" tabindex="6"></textarea>
  221. </label>
  222. </div>
  223. <div class="g-recaptcha" data-sitekey="GOOGLE RECAPTCHA2 WEBSITE KEY"></div>
  224. <div>
  225. <br>
  226. <button name="submit" type="submit" id="contact-submit">Send Email</button>
  227. </div>
  228. <p><strong>Credits:</strong>
  229. <br> Contact form by <a href="https://skortekaas.nl">Sven Kortekaas</a>
  230. </p>
  231. </form>
  232. <!-- /Form -->
  233. </div>
  234. </div>
  235. <script>
  236. (function() {
  237. // Create input element for testing
  238. var inputs = document.createElement('input');
  239. // Create the supports object
  240. var supports = {};
  241. supports.autofocus = 'autofocus' in inputs;
  242. supports.required = 'required' in inputs;
  243. supports.placeholder = 'placeholder' in inputs;
  244. // Fallback for autofocus attribute
  245. if (!supports.autofocus) {
  246. }
  247. // Fallback for required attribute
  248. if (!supports.required) {
  249. }
  250. // Fallback for placeholder attribute
  251. if (!supports.placeholder) {
  252. }
  253. // Change text inside send button on submit
  254. var send = document.getElementById('contact-submit');
  255. if (send) {
  256. send.onclick = function() {
  257. this.innerHTML = '...Sending';
  258. }
  259. }
  260. })();
  261. </script>
  262. </body>
  263. </html>