Script archive formmail

Here is an example of a basic script for a formmail archive:

formmail.php

<?php

// Configuration
$archive_dir = 'archives/'; // directory to store archived emails
$subject_prefix = '[Form Submission] '; // prefix for email subject

// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// Create a unique filename for the archived email
$filename = md5(uniqid()). '.txt';

// Save the email to a file
$file_path = $archive_dir. $filename;
file_put_contents($file_path, "Name: $name\nEmail: $email\nMessage: $message\n\n");

// Send a confirmation email to the user
$to = $email;
$subject = $subject_prefix. 'Your form submission has been received';
$message_text = "Thank you for submitting your form. Your email has been archived and a confirmation email has been sent to you.";
mail($to, $subject, $message_text);

// Send a notification email to the administrator
$admin_email = '[email protected]';
$subject = $subject_prefix. 'New form submission';
$message_text = "A new form submission has been received from $name ($email). The email has been archived and a confirmation email has been sent to the user.";
mail($admin_email, $subject, $message_text);

// Redirect the user to a thank you page
header('Location: thank_you.php');
exit;

?>

How it works:

  1. The script gets the form data from the $_POST array.
  2. It creates a unique filename for the archived email using the md5 and uniqid functions.
  3. It saves the email to a file in the specified archive directory using the file_put_contents function.
  4. It sends a confirmation email to the user using the mail function.
  5. It sends a notification email to the administrator using the mail function.
  6. It redirects the user to a thank you page using the header function.

Note: