How can PHP be used to automatically send attached files to a fixed email address?
To automatically send attached files to a fixed email address using PHP, you can use the PHP `mail()` function along with the `mail()` headers to specify the email address, subject, and content type. You can use the `$_FILES` superglobal to access the uploaded file and attach it to the email using the `file_get_contents()` function.
<?php
$to = "recipient@example.com";
$subject = "Attached File";
$message = "Please find the attached file.";
$filename = $_FILES['file']['name'];
$file = $_FILES['file']['tmp_name'];
$content = file_get_contents($file);
$attachment = chunk_split(base64_encode($content));
$boundary = md5(time());
$headers = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
$body = "--".$boundary."\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message."\r\n";
$body .= "--".$boundary."\r\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$body .= $attachment."\r\n";
$body .= "--".$boundary."--";
mail($to, $subject, $body, $headers);
?>
Related Questions
- How can PHP be used to customize menu items based on the current URL in a PHPBB forum?
- What are some alternative methods to determine file size in PHP aside from using the filesize() function?
- What are the potential pitfalls of including PHP sites within a webpage using the traditional header-site-footer method?