Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.55 KB | None | 0 0
  1. <?php
  2. // Set the header response to JSON
  3. header('Content-type: application/json');
  4.  
  5. // Do not show php error
  6. ini_set('display_errors','Off');
  7.  
  8. $response = array();
  9.  
  10. /*
  11. *Handle Message From
  12. */
  13. // check email into post data
  14. if (isset($_POST['submit_message'])) {
  15.     $email = trim($_POST['email']);
  16.     $name = trim($_POST['name']);
  17.     $product = trim($_POST['product']);
  18.     $message = trim($_POST['message']);
  19.    
  20.    
  21.     $email = filter_var(@$_POST['email'], FILTER_SANITIZE_EMAIL );
  22.    
  23.     $name = htmlentities($name);
  24.     $product = htmlentities($product);
  25.     $message = htmlentities($message);
  26.    
  27.     // Validate data first
  28.     if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 50 ) {
  29.         http_response_code(403);
  30.         $response['error']['email'] = "A valid email is required";
  31.     }
  32.     if (empty($name) ) {
  33.         http_response_code(403);
  34.         $response['error']['name'] = 'Name is required ';
  35.     }
  36.     if (empty($message)) {
  37.         http_response_code(403);
  38.         $response['error']['message'] = 'Empty message is not allowed';
  39.     }
  40.    
  41.     // Process to emailing if forms are correct
  42.     if (!isset($response['error']) || $response['error'] === '') {
  43.        
  44.        
  45.         /* in this sample code, messages will be stored in a text file */
  46.         //        PROCESS TO STORE MESSAGE GOES HERE
  47.        
  48.         $content = "Name: " . $name . " \r\nEmail: " . $email .  " \r\nMessage: " . $message;
  49.         $content = str_replace(array('<','>'),array('&lt;','&gt;'),$content);
  50.         $name = str_replace(array('<','>'),array('&lt;','&gt;'),$name);
  51.         $message = str_replace(array('<','>'),array('&lt;','&gt;'),$message);
  52.        
  53.         // -- BELOW : EXAMPLE SEND YOU AN EMAIL CONTAINING THE MESSAGE (comment to disable it/ uncomment it to enable it)
  54.         // Set the recipient email address.
  55.         // IMPORTANT - FIXME: Update this to your desired email address (relative to your server domaine).
  56.         $recipient = "hello@arsdata.io";
  57.        
  58.         // Set the email subject.
  59.         $subject = "Need support message From ".$name;
  60.        
  61.         // Build the email content.
  62.         $email_content = $message."\n \n";
  63.         $email_content .= "Sincerely,";
  64.         $email_content .= "From: $name\n\n";
  65.         $email_content .= "Email: $email\n\n";
  66.        
  67.         // Build the email headers.
  68.         $email_headers = "MIME-Version: 1.0" . "\r\n";
  69.         $email_headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
  70.         $email_headers .= "From: $name <$email>" . "\r\n";
  71.         $email_headers .= "Reply-To: <$email>";
  72.        
  73.        
  74.         // Send the email.
  75.         if ( mail($recipient, $subject, $email_content, $email_headers) ) {
  76.             // Set a 200 (okay) response code.
  77.             http_response_code(200);
  78.             $response['success'] = 'Thank You! Your message has been sent';
  79.            
  80.         } else {
  81.             // Set a 500 (internal server error) response code.
  82.             http_response_code(500);
  83.             $response['error'] = 'Oops! Something went wrong and we couldn\'t send your message';
  84.             $content = 'Message delivery error - can not send message'. "\r\n" . $content;
  85.             // Uncomment below to Write message into a file as a backup
  86.             //file_put_contents("message.txt", $content . "\r\n---------\r\n", FILE_APPEND | LOCK_EX);
  87.         }
  88.        
  89.     }
  90.     else {
  91.         // Set a 403 (error) forbidden response code due missing data to error.
  92.         http_response_code(403);
  93.         //$response['error'] = '<ul>' . $response['error'] . '</ul>';
  94.     }
  95.    
  96.    
  97.     $response['email'] = $email;
  98.     $response['form'] = 'submit_message';
  99.     echo json_encode($response);
  100.     /*
  101.     response will be : {
  102.         email : 'user@email.com',
  103.         form : 'submit_message',
  104.         success : 'Feedback message if success',
  105.         error : 'A feedback message if an error happened, or a JSON'
  106.     }
  107.     */
  108. }
  109.  
  110.  
  111. // Receive email newsletter subscription
  112. if (isset($_POST['submit_email'])) {
  113.    
  114.     $email = filter_var(@$_POST['email'], FILTER_SANITIZE_EMAIL );
  115.    
  116.     // Form validation handled by the server here if required
  117.     if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 50 ) {
  118.         $response['error']['email'] = "A valid email is required";
  119.     }
  120.    
  121.    
  122.     if (!isset($response['error']) || $response['error'] === '') {
  123.        
  124.         //        PROCESS TO STORE EMAIL GOES HERE
  125.        
  126.        
  127.         $email = str_replace(array('<','>'),array('&lt;','&gt;'),$email);
  128.        
  129.         // -- BELOW : EXAMPLE SEND YOU AN EMAIL ABOUT THE NEW USER (comment to disable it/ uncomment it to enable it)
  130.         // Set the recipient email address.
  131.         // IMPORTANT - FIXME: Update this to your desired email address (relative to your server domaine).
  132.         $recipient = "your@email.com";
  133.        
  134.         // Set the email subject.
  135.         $subject = "New subscription";
  136.        
  137.         // Build the email content.
  138.         $email_content = "Hello \n New user subscription.\n";
  139.         $email_content .= "Email: $email\n\n";
  140.         $email_content .= "Sincerely,";
  141.        
  142.         // Build the email headers.
  143.        
  144.         $email_headers = "MIME-Version: 1.0" . "\r\n";
  145.         $email_headers .= "Content-Type: text/html; charset=UTF-8" . "\r\n";
  146.         $email_headers .= "From: <$email>" . "\r\n";
  147.        
  148.         // Send the email.
  149.         if (mail($recipient, $subject, $email_content, $email_headers)) {
  150.             // Set a 200 (okay) response code.
  151.             http_response_code(200);
  152.             $response['success'] = "Thank You! You will be notified.";
  153.            
  154.         } else {
  155.             // Set a 500 (internal server error) response code.
  156.             //http_response_code(500);
  157.             http_response_code(500);
  158.             $response['error'] = "Oops! Something went wrong and we couldn't send your message.";
  159.            
  160.         }
  161.         // -- END OF : EXAMPLE YOU AN EMAIL ABOUT THE NEW USER
  162.        
  163.         // Uncomment below to save email list to a file as backup
  164.         file_put_contents("email.txt", $email . " \r\n", FILE_APPEND | LOCK_EX);
  165.        
  166.        
  167.     }
  168.     $response['email'] = $email;
  169.     $response['form'] = 'submit_email';
  170.     echo json_encode($response);
  171.    
  172.     /*
  173.     response will be : {
  174.         email : 'user@email.com',
  175.         form : 'submit_message',
  176.         success : 'Feedback message if success',
  177.         error : 'A feedback message if an error happened, or a JSON'
  178.     }
  179.     */
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement