php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

Problem with attachments

Open blackxel opened this issue 10 months ago • 1 comments

Describe the bug This email should have attachments.

Code to Reproduce

$eml = <<<EML
Delivered-To: [email protected]
Received: by 2002:a05:6400:62b5:b0:27c:7b7b:895b with SMTP id kv53csp168015ecb;
        Tue, 4 Feb 2025 04:13:47 -0800 (PST)
Return-Path: <[email protected]>
DKIM-Signature: v=1; a=rsa-sha256; c=simple;
    d=cloud.bxexpress.cl; s=default; r=y; l=19018; t=1738671297;
    x=1739276097; [email protected]; q=dns/txt; h=InvoixSignature:
    Message-ID:MIME-Version:From:To:Date:Subject:Content-Type; z=Rec
    eived:=20from=20169.254.3.185=20[(34.200.193.86)]=20by=20cloud.g
    dexpress.cl=20with=20ESMTPSA=20id=20md5001028814655.msg=3B=20=0D
    =0A=09Tue,=2004=20Feb=202025=2009=3A14=3A56=20-0300|InvoixSignat
    ure:=20Rr1k5jv/K5uM6tNO7D2nrHEn+/bhDFYKOlemyHBwocUmUSGKSt2ZtCOjI
    ChG/MZEBt6MPkzAVCiU4JUzpLyAsKDWyj/fV+nXdxzy9gYelPcaHxUF0LeZe7ZLh
    PkEICEG|Message-ID:=20<XML_DTE_76946917-6_33_92838_0_20250204121
    [email protected]>|MIME-Version:=201.0|From:=20"BXExpress"=
    20<[email protected]>|To:[email protected]|Date:=204=20
    Feb=202025=2012=3A13=3A43=20+0000|Subject:=20=3D?utf-8?B?RmFjdHV
    yYSBFbGVjdHLDs25pY2EgOTI4MzggZGVsIDAzLzAy?=3D=0D=0A=20=3D?utf-8?
    B?LzIwMjUgZW1pdGlkYSBwb3IgRUxFQ1RSSUNJREFEIENBTExFSkFTIFNQ?=3D=0
    D=0A=20=3D?utf-8?B?QSBwYXJhIDc2NDY2MjQ1LTg=3D?=3D|Content-Type:=
    20multipart/mixed=3B=0D=0A=20boundary=3D--boundary_54_7771d5fe-3
    6a2-4520-b9c5-f65735f817b5; bh=5xfWXdA485Hl/XUGTYNfYDP77e7bOJ+h4
    zzSFd0sgGY=; b=CDb8NkFSwht6q8wQyPxpWR0dYZiJij+NMdNsvIAVFxsBL4+0k
    bR6MSdMX6JDyiJHMFyqKqUW6B9kSbUsXjDbRRXO3XSJvpSGDi00I40DqBcR+vwRk
    EMqmkT4eUeyl1Qt2liz4lF6/8jE/EuWDyOc3gGowEuYlFS/OxF91IVX1OGOBC0bb
    WEUro0nz3p1An1SY7B+2/v5z/c1NzfZNaCgVRXDQZoqQTkJkYFpfQ4kbJLrumRkq
    BEJ9MIwLKai2JZ8YLi+GlAlc3j7GwQUJVsKDZMvGPM1RRO+E0kdOvhdgdSPCc8u5
    k+hXw1FR0GXGeDDaFRiMYXRz84AA/VOdH2ulg==
Received: from 169.254.3.185 [(34.200.193.86)] by cloud.bxexpress.cl with ESMTPSA id md5001028814655.msg;
    Tue, 04 Feb 2025 09:14:56 -0300
Message-ID: <[email protected]>
MIME-Version: 1.0
From: "BXExpress" <[email protected]>
To: [email protected]
Date: 4 Feb 2025 12:13:43 +0000
Subject: =?utf-8?B?RmFjdHVyYSBFbGVjdHLDs25pY2EgOTI4MzggZGVsIDAzLzAy?=
    =?utf-8?B?LzIwMjUgZW1pdGlkYSBwb3IgRUxFQ1RSSUNJREFEIENBTExFSkFTIFNQ?=
    =?utf-8?B?QSBwYXJhIDc2NDY2MjQ1LTg=?=
Content-Type: multipart/mixed;
    boundary=--boundary_54_7771d5fe-36a2-4520-b9c5-f65735f817b5


----boundary_54_7771d5fe-36a2-4520-b9c5-f65735f817b5
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64

PGRpdiBkaXI9Imx0ciI+PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZv
bnQtZmFtaWx5OnRhaG9tYSxzYW5zLXNlcmlmIj5UZXN0PC9kaXY+PC9kaXY+
----boundary_54_7771d5fe-36a2-4520-b9c5-f65735f817b5
Content-Type: application/octet-stream;
    name=ed9f1e16-4ba3-47f3-b219-4641a83017ac.xml
Content-Transfer-Encoding: base64
Content-Disposition: attachment

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHRlc3Q+MTwvdGVz
dD4=
----boundary_54_7771d5fe-36a2-4520-b9c5-f65735f817b5--
EML;
$message =  Webklex\PHPIMAP\Message::fromString($eml);
var_dump($message->hasAttachments());//false :-/

I "Fixed" modifying the find function in src/Header.php

/**
 * Perform a regex match all on the raw header and return the first result
 * @param $pattern
 *
 * @return mixed|null
 */
public function find($pattern): mixed {
    if (preg_match_all($pattern, $this->raw, $matches)) {
        if (isset($matches[1])) {
            if (count($matches[1]) > 0) {
                //return $matches[1][0];
                $maxLength = 0;
                $item = '';
                foreach ($matches[1] as $match) {
                    if (strlen($match) > $maxLength) {
                        $maxLength = strlen($match);
                        $item = $match;
                    }
                }
                return $item;
            }
        }
    }
    return null;
}

blackxel avatar Feb 12 '25 01:02 blackxel

I'm having the same problem with a couple of emails from one of our clients, but using this edited find function didn't fix it for me...

The problem seems to be with the DKIM-Signature key, cause it contains the boundary text, if i remove this key from the .eml, i get all attachments correctly.

😭

Edit: Here are some of the DKIM-Signature that we have. As you can see, they endup a bit different from the one on this post and thats why their fix didn't help me...

DKIM-Signature's
DKIM-Signature: v=1; a=rsa-sha256; c=simple;
	d=email1.com; s=MDaemon; r=y; l=798972;
	t=1759947332; x=1760552132; [email protected];
	q=dns/txt; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:
	Content-Type:Thread-Index:Content-Language; z=Received:=20by=20m
	x1.brdrive.net=20with=20ESMTPSA=20id=20md5001036462776.msg=3B=20
	Wed,=2008=20Oct=202025=2015=3A15=3A31=20-0300|From:=20"xxxxxxxx=
	20xxxxxxxxx=20xxxx=20-=20T.I=20xxxxx=20xxxxxxx"=20<zzzz@yyyyyyyy
	yyyyyyyyyyyy.com.br>|To:=20"'yyyyy=20-=20xxxxx=20xxxxxxx'"=20<xx
	[email protected]>|Cc:=20"xxxxx=20xxxx=20xx=20xxxxxx=20
	xxxxx"=20<[email protected]>|Subject:=20xxxx=20xxx
	xxxxx|Date:=20Wed,=208=20Oct=202025=2015=3A15=3A26=20-0300|Me
	ssage-ID:=20<00bb01dc387f$85ba8db0$912fa910$@xxxxxxxxxxxxxxx.
	com.br>|MIME-Version:=201.0|Content-Type:=20multipart/related=3B
	=0D=0A=09boundary=3D"----=3D_NextPart_000_00BC_01DC3866.606DCAE0
	"|Thread-Index:=20Adw4fxn9tk7IYIwgQw670jKwPcXNGw=3D=3D|Content-L
	anguage:=20pt-br; bh=way+xAuEFlYXCIj8/WUNbQ+g7WMlZRiU33ZeH4ykXAw
	=; b=g6d/ewBnCIBxrPU0mpLC32LGx8hasL/nbAanJ9h8vxyWaj4ryoiY5eE4QYf
	TLdkOataNO0ExJc3TkqBP2rf2A6/3ZAir5KavGETrLx9bXqnHTa9zdNl44BEdUMF
	4Psd8o4n64RivTJaHHmveL3HN63ymdI4GhEb2BCrCB+542mA=
Content-Type: multipart/related;
	boundary="----=_NextPart_000_00BC_01DC3866.606DCAE0"
X-Mailer: Microsoft Outlook 16.0


DKIM-Signature: v=1; a=rsa-sha256; c=simple;
	d=xxxxxxxxxxxxxxxxxxx.com.br; s=MDaemon; r=y; l=57744;
	t=1760017853; x=1760622653; [email protected];
	q=dns/txt; h=Date:From:To:Subject:MIME-Version:Content-Type:
	Message-ID:In-Reply-To:References; z=Received:=20by=20mx1.brdriv
	e.net=20with=20ESMTPSA=20id=20md5001036489749.msg=3B=20Thu,=2009
	=20Oct=202025=2010=3A50=3A53=20-0300|Received:=20from=20[111.11.
	111.111]=20by=20xxxxxxxxxxxxxxxxxxx.com.br=20via=20MDaemon=20Webm
	ail=20with=20HTTP=3B=0D=0A=09Thu,=2009=20Oct=202025=2010=3A50=3A
	48=20-0300|Date:=20Thu,=2009=20Oct=202025=2010=3A50=3A48=20-0300
	|From:=20"xx"=20<[email protected]>|To:=20"Xxxxxxx=20
	-=20Xxxxx=20Xxxxxxx"=20<[email protected]>|Subject:=20
	Re=3A=20Xxxxxxxx=20xx=20Xxxxx|MIME-Version:=201.0|Content-Type:=
	20multipart/mixed=3B=20boundary=3D"1009-1350-48-01-PART_BREAK"|M
	essage-ID:=20<[email protected]>
	|In-Reply-To:=20<[email protected]
	om.br>|References:=20<WC20251008185918.091251@xxxxxxxxxxxxxxxxxxx
	.com.br>=20<[email protected]
	>=20<[email protected]>; bh=0
	69bP6wOcYMEKjrx/+JXS10X58dt7nb9xftlK1ox4LM=; b=oJTSPkhDWu2/F+IiN
	HWD147eJO8nXnbvqpLQTdp91EXelxMe0iMgwahJ3ynLyZBfhl2upp+vsR4tCXpMw
	lINZgRbCxJsKEXiixSIlSzy4CPd58FLTHj7ogmxxlrG0yj0wXsWT5OiVGYsVTCDK
	/67ln+XQcYakSzWTnr+OHkvFME=
Content-Type: multipart/mixed; boundary="1009-1350-48-01-PART_BREAK"
X-Mailer: MDaemon Webmail

The problem is definitely with the getBoundary / find / clearBoundaryString functions, i just dont know what we can do to fix this problem without compromising other emails... ;/

gilney-canaltelecom avatar Oct 09 '25 17:10 gilney-canaltelecom