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:
- The script gets the form data from the
$_POST
array. - It creates a unique filename for the archived email using the
md5
anduniqid
functions. - It saves the email to a file in the specified archive directory using the
file_put_contents
function. - It sends a confirmation email to the user using the
mail
function. - It sends a notification email to the administrator using the
mail
function. - It redirects the user to a thank you page using the
header
function.
Note:
- Make sure to replace
[email protected]
with the actual email address of the administrator. - Make sure to create the
archives
directory and set the correct permissions for the script to write to it. - This script assumes that the form is sent using the
POST
method. If your form is sent using theGET
method, you'll need to modify the script accordingly. - This script does not validate the form data or prevent spam submissions. You may want to add additional security measures to prevent abuse.