/incl/send-email.php

https://gitlab.com/nemote95/avita · PHP · 145 lines · 102 code · 23 blank · 20 comment · 14 complexity · a59f9d78f00755013ba9a527bf157b7f MD5 · raw file

  1. <?php
  2. /* set the email of the recipient (your email) */
  3. $recipient = "info@proteusthemes.com";
  4. if ( isset($_POST['submit']) ) // just send the email if the $_POST variable is set
  5. {
  6. $name = $_POST['name'];
  7. $email = $_POST['email'];
  8. $url = $_POST['url'];
  9. $message = $_POST['message'];
  10. $subject = "Email From Website: " . $name; // subject of the email msg
  11. $errors = array(); // empty array of the err
  12. /*
  13. * The fields
  14. * 1st param: submitted data
  15. * 2nd param: reuqired (TRUE) or not (FALSE)
  16. * 3rd param: the name for the error
  17. */
  18. $fields = array(
  19. 'name' => array($name, TRUE, "Name"),
  20. 'url' => array($url, FALSE, "Website"),
  21. 'email' => array($email, TRUE, "E-mail Address"),
  22. 'message' => array($message, TRUE, "Your Message"),
  23. );
  24. $i=0;
  25. foreach ($fields as $key => $field) {
  26. if ( FALSE == test_field( $field[0], $field[1] ) ) {
  27. $errors[$key] = "The field '".$field[2]."' is required.";
  28. }
  29. $i++;
  30. }
  31. //var_dump($errors);
  32. if (empty($errors)) { // if there is no errors, we can send the mail
  33. $body = "";
  34. $body .= "----- Info about the sender -----\n\n";
  35. $body .= "Name: ".$fields['name'][0]."\n";
  36. $body .= "Email: ".$fields['email'][0]."\n";
  37. $body .= "Website: ".$fields['url'][0]."\n";
  38. $body .= "\n\n----- Message -----\n\n";
  39. $body .= $fields['message'][0];
  40. if( mail( $recipient, $subject, $body, "FROM: ".$fields['email'][0] ) ) { // try to send the message, if not successful, print out the error
  41. message_was_sent($fields);
  42. } else {
  43. echo "It is not possible to send the email. Check out your PHP settings!";
  44. print_the_form($errors, $fields);
  45. }
  46. } else { // if there are any errors
  47. print_the_form($errors, $fields);
  48. }
  49. } else {
  50. print_the_form();
  51. }
  52. /**
  53. * prints out the form if necessary
  54. */
  55. function print_the_form($errors = array(), $fields = null) {
  56. ?>
  57. <!-- = contact form = -->
  58. <form method="post" action="#" class="form form-inline form-contact">
  59. <div class="control-group push-down-20<?php error_class('name', $errors); ?>">
  60. <input type="text" aria-required="true" tabindex="1" size="30" value="<?php inpt_value('name', $fields); ?>" id="name" name="name" required>
  61. <label for="name" class="control-label">Name<span class="red-clr bold">*</span></label>
  62. <?php show_error('name', $errors); ?>
  63. </div>
  64. <div class="control-group push-down-20<?php error_class('email', $errors); ?>">
  65. <input type="email" aria-required="true" tabindex="2" size="30" value="<?php inpt_value('email', $fields); ?>" id="email" name="email" required>
  66. <label for="email">Mail<span class="red-clr bold">*</span></label>
  67. <?php show_error('email', $errors); ?>
  68. </div>
  69. <div class="control-group push-down-20<?php error_class('url', $errors); ?>">
  70. <input type="text" tabindex="3" size="30" value="<?php inpt_value('url', $fields); ?>" id="url" name="url">
  71. <label for="url">Website</label>
  72. <?php show_error('url', $errors); ?>
  73. </div>
  74. <div class="control-group push-down-20<?php error_class('message', $errors); ?>">
  75. <textarea class="input-block-level" tabindex="4" rows="7" cols="70" id="message" name="message" placeholder="Your Message goes here ..." required><?php inpt_value('message', $fields); ?></textarea>
  76. <?php show_error('message', $errors); ?>
  77. </p>
  78. <p>
  79. <input type="hidden" value="1" name="submit" />
  80. <button class="btn btn-primary bold" type="submit" tabindex="5" id="submit">SEND EMAIL</button>
  81. </p>
  82. </form>
  83. <!-- = /contact form = -->
  84. <?php
  85. }
  86. function message_was_sent($fields) { // notification that sending the mail was successful
  87. ?>
  88. <p class="text-info">Your message was sent successfully!</p>
  89. <?php
  90. }
  91. /**
  92. * Returns TRUE if field is required and OK
  93. */
  94. function test_field($content, $required) {
  95. if ( TRUE === $required ) {
  96. return strlen($content) > 0;
  97. } else {
  98. return TRUE;
  99. }
  100. }
  101. /**
  102. * Add the appropirate class name to the specified input field
  103. */
  104. function error_class($name, $errors) {
  105. if ( array_key_exists( $name, $errors ) ) {
  106. echo " error";
  107. }
  108. }
  109. /**
  110. * repopulate the data when the form is submitted and errors returned
  111. */
  112. function inpt_value($name, $fields) {
  113. if ( null === $fields ) {
  114. return;
  115. } else {
  116. echo $fields[$name][0];
  117. }
  118. }
  119. function show_error( $name, $errors ) {
  120. if ( array_key_exists( $name, $errors ) )
  121. echo '<div class="help-block"> ' . $errors[$name] . ' </div>';
  122. }