Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding base64 body #202

Open
denyszrazhevskiy opened this issue Nov 4, 2022 · 1 comment
Open

Decoding base64 body #202

denyszrazhevskiy opened this issue Nov 4, 2022 · 1 comment

Comments

@denyszrazhevskiy
Copy link

Hi, Zaahid! Hope for your advice.

We receive non-standard emails from the same sender.
mail_example.txt as example (Some data has been removed, but this does not affect the main problem).
Email with headers:

Content-Type: multipart/mixed
Content-Transfer-Encoding: base64

The sender uses SAP Web Application Server 7.00, which encodes the entire body of the message in base64 encoding.
According to RFC 1521 such email is not valid
If a Content-Transfer-Encoding header field appears as part of a message header, it applies to the entire body of that message. If a Content-Transfer-Encoding header field appears as part of a body part's headers, it applies only to the body of that body part. If an entity is of type "multipart" or "message", the Content-Transfer-Encoding is not permitted to have any value other than a bit width (e.g., "7bit", "8bit", etc.) or "binary

As a workaround, we now use this construction

before check multipart/mixed and base64 and entire body is encoded in base64
$message = $zBatesonMimeParser->parse($nonStandardMail, false);
$message->removeHeader("Content-Transfer-Encoding");
$message->addRawHeader("Content-Transfer-Encoding", "binary");
$message->setContent(base64_decode($message->getContent()));
$message2 = $zBatesonMimeParser->parse($message->getStream(), false);

Perhaps there are better ways to do this?

Thank you for your attention.

@zbateson
Copy link
Owner

zbateson commented Nov 5, 2022

Hi @denyszrazhevskiy --

That's pretty close to what I'd do. I'm not a huge fan of base64_decode though since it doesn't work on streams which might be annoying for larger emails... here's what I'd do instead:

use ZBateson\MailMimeParser\Stream\HeaderStream;
use GuzzleHttp\Psr7\AppendStream;
use GuzzleHttp\Psr7;

$encoded = $zBatesonMimeParser->parse($nonStandardMail, false);
$message = $zBatesonMimeParser->parse(
    new AppendStream([ new HeaderStream($encoded), $encoded->getContentStream() ]),
    true
);

Hope that helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants