设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 202|回复: 0

Get Contents From E-Mail And Write It To Mysql Table - 1

[复制链接]
发表于 2014-2-11 23:03:45 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2014-2-12 15:04 编辑

I managed to get around this and successfully deal with this image looping & image naming process by using a recently published class found here:
http://www.electrict...ge-attachments/

and followed up that class by using it as an include in the script that creates variables such as $text_part, $html_part, and wrote the following:
  1. /****         The below sequence manages IMAGE Tags in E-mail are supplied by direct URL links          ****/
  2.         if (preg_match_all('/<img\s+([^>]*)src="http(s)?:\/\/(.*)"/i', $emailMessage->bodyHTML, $matches)) {
  3.            echo '<br />Detected E-mail Client uses URL links for image tags...continuing News from Mail session<br />';
  4.                 }

  5.         /****         The below sequence manages IMAGE Tags in E-mail are supplied by attachments          ****/
  6.         if (preg_match_all('/src="cid:(.*)"/Uims', $emailMessage->bodyHTML, $matches)) {

  7.                 if(count($matches)) {
  8.                         $search = array();
  9.                         $replace = array();

  10.                         $num_img =  sizeof($emailMessage->attachments);
  11.                         
  12.                         for($i = 1; $i <= $num_img; $i++){
  13.                
  14.                         foreach($matches[1] as $match) {
  15.                         
  16.                                 $matchExt = $emailMessage->attachments[$match]['subtype'];
  17.                                 $fnameExt = (string)$matchExt;
  18.                                 $fnameExt = strtolower($fnameExt);
  19.                                 $uniqueFilename = "image$i.$fnameExt";
  20.                                 $i++;
  21.                                 file_put_contents("$dirname/$uniqueFilename", $emailMessage->attachments[$match]['data']);
  22.                                 $search[] = "src="cid:$match"";
  23.                                 $replace[] = "src="$dirname/$uniqueFilename"";
  24.                                 }
  25.                                         $emailMessage->bodyHTML = str_ireplace($search, $replace, $emailMessage->bodyHTML);
  26.                                 }
  27.                         }
  28.                 }

  29.         /****         The below sequence manages IMAGE Tags in E-mail are supplied by inline image data          ****/

  30.         if(preg_match_all('/src="data:(image\/(.*);.*,(.*))"/Uims', $emailMessage->bodyHTML, $basematches)) {
  31.                 $img_num =  sizeof($basematches[2]);
  32.                 if(count($basematches[3])) {
  33.                
  34.                 $search = array();
  35.                 $replace = array();
  36.                
  37.                 for($i = 1; $i <= $img_num; $i++) {
  38.                
  39.                         foreach($basematches[3] as $base64data) {

  40.                                 $uniqueFilename = "$dirname/image".$i.".png";
  41.                                 $i++;
  42.                                 $base64data_decoded = base64_decode($base64data);
  43.                                 $imCreate = imagecreatefromstring($base64data_decoded);
  44.                                 if ($imCreate !== false) {
  45.                                         imagepng($imCreate, $uniqueFilename);
  46.                                         imagedestroy($imCreate);
  47.                                 }
  48.                         $search[] = "$base64data"";
  49.                         $replace[] = "$uniqueFilename"";
  50.                 }
  51.                 $emailMessage->bodyHTML = str_ireplace($search, $replace, $emailMessage->bodyHTML);
  52.                 $emailMessage->bodyHTML = preg_replace("/data:image\/(jpeg|png|bmp|gif);base64,/i", "", $emailMessage->bodyHTML);
  53.           }
  54.         }
  55. }
复制代码

I am kind of worried about using all that regex to parse this HTML as opposed to finding a DOMDocument solution, but through my research i haven't seen anything that DOMDocuments can do to that length as opposed to PCRE functions. The best one can do is include as many 'possible modifiers' as you can.

I'm just trying to get around another bug where Apple iPhone's standard Mail app splits messages in different parts because of inline attachments, i proved this with some testing.
An example:

--Apple-Mail-1-577088534
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

Title: iPhone Email=0A=
Snippet: iPhone Test=0A=
Tags: iphone=2Cemail=2Cpictures=0A=
Category: Member=0A=
Message:=0A=
Brothers and sisters have I none but this man's father is my father's son.=
=0A=
=0A=

--Apple-Mail-1-577088534
Content-Disposition: inline; filename="image.png"
Content-Type: image/png; name="image.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAUAAAAHgCAYAAADUjLREAAAgAElEQVR4AeydB3xUVfbHTzpJCITe
mwgIWEBRFARBV117WxVde11X176WXeuuZdUt6t+1u4oFe8MK9opiQRAUadJ7S0hIz/zP94YbXh5v

--Apple-Mail-1-577088534
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

=0A=
=0A=
Brothers and sisters have I none but this man's father is my father's son.=
=0A=

--Apple-Mail-1-577088534
Content-Disposition: inline; filename="image.jpeg"
Content-Type: image/jpeg; name="image.jpeg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdC
IFhZWiAHzgACAAkABgAxAABhY3NwTVNGVAAAAABJRUMgc1JHQgAAAAAAAAAAAAAAAQAA9tYAAQAA

--Apple-Mail-1-577088534
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

Brothers and sisters have I none but this man's father is my father's son.=
=0A=

--Apple-Mail-1-577088534
Content-Disposition: inline; filename="image.jpeg"
Content-Type: image/jpeg; name="image.jpeg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQEAYABgAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdC
IFhZWiAHzgACAAkABgAxAABhY3NwTVNGVAAAAABJRUMgc1JHQgAAAAAAAAAAAAAAAQAA9tYAAQAA

--Apple-Mail-1-577088534
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

Brothers and sisters have I none but this man's father is my father's son.=
=0A=

--Apple-Mail-1-577088534
Content-Disposition: inline; filename="image.jpeg"
Content-Type: image/jpeg; name="image.jpeg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQEASABIAAD/4QI2RXhpZgAATU0AKgAAAAgACgEPAAIAAAAGAAAAhgEQAAIA
AAALAAAAjAESAAMAAAABAAYAAAEaAAUAAAABAAAAmAEbAAUAAAABAAAAoAEoAAMAAAABAAIAAAEx

--Apple-Mail-1-577088534
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

Brothers and sisters have I none but this man's father is my father's son.=
=0A=
=0A=
Brothers and sisters have I none Inline image:=

--Apple-Mail-1-577088534
Content-Disposition: inline; filename="image.jpeg"
Content-Type: image/jpeg; name="image.jpeg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQEASABIAAD/4QI2RXhpZgAATU0AKgAAAAgACgEPAAIAAAAGAAAAhgEQAAIA
AAALAAAAjAESAAMAAAABAAYAAAEaAAUAAAABAAAAmAEbAAUAAAABAAAAoAEoAAMAAAABAAIAAAEx

--Apple-Mail-1-577088534
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

but this man's father is my father's son.=

--Apple-Mail-1-577088534--

The email contained multiple lines of the same words where as i just inserted pictures amongst them, and also concluded that the client doesn't use <img> tags, (EXCEPT for when forwarding process, then the client uses <img> tags).                                                                                
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 13:52 , Processed in 0.015402 second(s), 17 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表