Jump to content

regex email MIME headers


ZaphodQB

Recommended Posts

I am trying to insert a line at the top of an email using regex to locate the top of the message and insert a single line.

My thought was to replace everything from the

"Content-Type: text/plain;" 

to the double carriage return (\r\n\r\n) 

between "quoted-printable" and original start of the email,

with the matched data+ "the line I want to add"

so that the example here;

 

____________________________________

 

 

------_=_NextPart_002_01C8B165.D1A0F25B

Content-Type: text/plain;

charset="us-ascii"

Content-Transfer-Encoding: quoted-printable

 

The original start of the email text

 

 

 

becomes;

 

 

 

------_=_NextPart_002_01C8B165.D1A0F25B

Content-Type: text/plain;

charset="us-ascii"

Content-Transfer-Encoding: quoted-printable

 

the line I want to add

The original start of the email text

_____________________________________

 

 

I want to start with the "Content-Type: text/plain;"  because it could also be "Content-Type: text/html;", and then I would want to match all the way down to the "<body>" tag and then add the  the line I want to add after the <body> tag.

 

My limited experience with regex is failing me.

Anyone got the answer?

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/104956-regex-email-mime-headers/
Share on other sites

Whooo hoo! Got the 1st part figured out for the text/plain portions!

 

I can change;

 

 

This is a multi-part message in MIME format.

 

------=_NextPart_000_0005_01C8B365.50073EA0

Content-Type: text/plain;

charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable

 

This is the original top of the message as it is received before I add =

my line of text.

------=_NextPart_000_0005_01C8B365.50073EA0

 

using;

 

 

preg_match("/(content-type:\s*text\/plain;.*?)\r?\n\r?\n/is",$msgBody,$matches);
$replacement=$matches[0]."My new line of text\n";
$msgBody = preg_replace("/(content-type:\s*text\/plain;.*?)\r?\n\r?\n/is",$replacement,$msgBody);

 

 

 

into;

 

 

This is a multi-part message in MIME format.

 

------=_NextPart_000_0005_01C8B365.50073EA0

Content-Type: text/plain;

charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable

 

My new line of text

This is the original top of the message as it is received before I add =

my line of text.

------=_NextPart_000_0005_01C8B365.50073EA0

 

 

Still working on the text/html pattern

How about using the first 2 new lines within the body section?

 

<pre>
<?php

$data = <<<DATA
This is a multi-part message in MIME format.

------=_NextPart_000_0005_01C8B365.50073EA0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

This is the original top of the message as it is received before I add =
my line of text.
------=_NextPart_000_0005_01C8B365.50073EA0
DATA;

echo preg_replace('/(?<=\r\n\r\n)(?!-+)/', '==Insert==', $data, 1);

?>
</pre>

 

Otherwise, you can match the variations with text/(?:plain|html).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.