ZaphodQB Posted May 10, 2008 Share Posted May 10, 2008 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 Quote Link to comment Share on other sites More sharing options...
ZaphodQB Posted May 10, 2008 Author Share Posted May 10, 2008 maybe I should add to this. I have the body of the eMail as a string which I have extracted using Net_POP3. $msgBody = htmlspecialchars($pop3->getBody(1)); Quote Link to comment Share on other sites More sharing options...
ZaphodQB Posted May 12, 2008 Author Share Posted May 12, 2008 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted May 12, 2008 Share Posted May 12, 2008 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.