0

I have a multipart MIME message where one part looks like

------=_Part_901_990681075.1528833507
Content-Disposition: attachment; filename="metadata.txt"
Content-ID: 3314a2d3-6092-48c3-93d9-a45648b6582b@localhost
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

version: 1.0=0Aid: 13848078-9bc3-4ec1-8cb0-3ee07d74f1cb=0Auser: givenName=
=3DJohn; middleName=3DJacob; surname=3DDoe; dateOfBirth=3D=0A=3D1970-01-01;=
gender=3DM; postalCode=3D12345

but recipient says it is malformed metadata section as the =0A should be replaced with true CRLF.

I know that it is representation of CRLF for quoted-printable encoding but this is not what recipient is expecting.

To create metadata part I have used PHP code:

$sMetadata = "version: 1.0
id: $sTrancasctionId
user: givenName=$sUserFirstName; middleName=$sUserMiddleName; surname=$sUserLastName; dateOfBirth=
=$sUSerDOB; gender=$sUserGender; postalCode=$sUserPostalCode";

and then I'm using existing MailSo framework to add it to existing message object.

Any tips on that matter?

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • Well CRLF would actually be `=0D=0A`, the `=0A` is only the LF. How does your code to create this message look like? – vstm Jun 13 '18 at 13:09
  • @vstm I have updated OP – JackTheKnife Jun 13 '18 at 13:23
  • You could try adding `$sMetadata = preg_replace('/\R/', "\r\n", $sMetadata);` directly after the metadata part. This replaces all newlines with CRLF's. – vstm Jun 13 '18 at 13:36
  • @vstm Almost there - it does the trick but for one occurrence is adding CRLF twice. You can post it as an answer. Thanks! – JackTheKnife Jun 13 '18 at 13:41

1 Answers1

2

As mentioned in the comments the =0A represents only the linefeed (LF) part of CRLF. So the code is likely stored with Unix newlines which only consists of LF, while MIME representation expects CRLF.

With this command directly after the $sMetadata line you can convert all the newlines to CRLF:

$sMetadata = preg_replace('/\R/', "\r\n", $sMetadata);
vstm
  • 12,407
  • 1
  • 51
  • 47
  • Would the regex part be better as `/(\r\n|\r|\n)/` (to cover all) ? Or does the capital `\R` match all forms of newline/return/carriage? I admit I'm not super versed on every possible regex flag. – IncredibleHat Jun 13 '18 at 13:55
  • 1
    @IncredibleHat: Yes `\R` matches ["all" newline combinations](https://www.pcre.org/original/doc/html/pcrepattern.html#newlineseq). – vstm Jun 13 '18 at 14:08
  • Good to know! Learn something new everyday ;) (I was googling like mad, but google doesnt like \R as input lol). Thanks. – IncredibleHat Jun 13 '18 at 14:09