I am developing a small email system and have a question about PHP Mailparse which I can't seem to find an answer to, nor figure out from the code itself. I've spent a bunch of time going through the Mailparse code and can't see an answer.
Simply, is there any way of discerning between images which are attached to an incoming email and those which are embedded in the email (e.g. signature images at the foot of the email).
Mailparse seems to deliver both attached files and embedded images all as attachments. What I'm trying to do is to preserve any attachments that the sender may have attached to the email, but discard any embedded images in the email.
Is this possible?
Thanks.
My Answer:
Yes, it is possible to discern between attached files and embedded images using PHP Mailparse. When parsing the email with Mailparse, you can check the Content-Disposition header of each attachment to determine if it is an attachment or an embedded image.
Attachments will typically have a Content-Disposition header with a value of "attachment", while embedded images will have a Content-Disposition header with a value of "inline". You can use this information to filter out the embedded images and only preserve the attachments that the sender has attached to the email.
Here is an example of how you can check the Content-Disposition header of each attachment using PHP Mailparse:
php
$resource = mailparse_msg_create();
mailparse_msg_parse($resource, $email_content);
$attachments = mailparse_msg_get_structure($resource);
foreach ($attachments as $attachment) {
$info = mailparse_msg_get_part_data($resource, $attachment);
$headers = mailparse_msg_get_part($resource, $attachment);
$contentDisposition = mailparse_msg_get_part_data($headers)['disposition'];
if ($contentDisposition == 'attachment') {
// This is an attachment, preserve it
} elseif ($contentDisposition == 'inline') {
// This is an embedded image, discard it
}
}
mailparse_msg_free($resource);
By checking the Content-Disposition header of each attachment, you can differentiate between attached files and embedded images in the email and handle them accordingly.
Rate this post
3 of 5 based on 1261 votesComments