How can MIME types be handled when sending attachments via email using PHP?
When sending attachments via email using PHP, it is important to set the appropriate MIME type for each attachment to ensure compatibility with the recipient's email client. This can be achieved by using the `mime_content_type()` function to determine the MIME type of the file being attached and then setting it in the email headers using the `Content-Type` and `Content-Disposition` headers.
// Get the MIME type of the file
$mime_type = mime_content_type($file_path);
// Set the email headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: ".$mime_type."; charset=UTF-8" . "\r\n";
$headers .= "Content-Disposition: attachment; filename=\"".$file_name."\"" . "\r\n";
// Send the email with attachment
mail($to, $subject, $message, $headers);
Related questions
-
Dependency Injection Klassenverbindung OOP
-
PHP Joomla database operations
-
ctype_digit input validation PHP