What are the best practices for extracting and saving images from MIME emails in PHP?

When extracting and saving images from MIME emails in PHP, it is important to parse the email content and identify the image attachments. Once identified, the images can be saved to a specified directory on the server for further use or display.

// Sample code to extract and save images from MIME emails in PHP

// Parse the MIME email content
$raw_email = file_get_contents('email.eml');
$parser = new PhpMimeMailParser\Parser();
$parser->setText($raw_email);

// Get image attachments
$attachments = $parser->getAttachments();
foreach ($attachments as $attachment) {
    if ($attachment->contentType == 'image/jpeg' || $attachment->contentType == 'image/png') {
        $image_data = $attachment->getContent();
        $image_name = $attachment->getFilename();
        file_put_contents('images/' . $image_name, $image_data);
    }
}