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 Link to comment https://forums.phpfreaks.com/topic/104956-regex-email-mime-headers/ 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)); Link to comment https://forums.phpfreaks.com/topic/104956-regex-email-mime-headers/#findComment-537354 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 Link to comment https://forums.phpfreaks.com/topic/104956-regex-email-mime-headers/#findComment-538789 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). Link to comment https://forums.phpfreaks.com/topic/104956-regex-email-mime-headers/#findComment-539055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.