douji9518 2015-08-10 08:39
浏览 83

在数据库中存储Email-body的最佳方式

I'm working on some sort of application where I can get emails from my inbox via a cron job, where i just insert mail-from, mail-subject and mail-body into a MySQL database.

The problem i'm having is with the different mailtypes like HTML mails, plain text mails,..

Is there a way to get just the text? I've tried with fetchbody and HTML entitites, printable_decode but that results in sometimes an empty body insert or one with all special characters or sometimes perfect.

    //$body = mysqli_real_escape_string($db, htmlentities(quoted_printable_decode(imap_fetchbody($stream,$email_id, 1.2)), ENT_QUOTES));
$body = mysqli_real_escape_string($db, quoted_printable_decode(imap_fetchbody($stream,$email_id, 1.2)));
//$body = quoted_printable_decode(imap_fetchbody($stream,$email_id,2));
$bodyText = imap_fetchbody($stream,$email_id,1.2);
if(!strlen($bodyText)>0){
    $bodyText = "<html><body>".imap_fetchbody($stream,$email_id,1)."</body></html>";
}

Then i've tried with full html body, but that gives problems with inserting into my database and if I escape string, the HTML gets messy. I temporarly fixed it by getting the full HTML body and writing that to a .html file, but that's not really an option.

The reason why i store them into a MySQL database is so I can assign users, colors,.. to different mails (Which isn't an option with iMAP mails in Outlook for ex.)

My ultimate idea is to save the emails, just like it shows in outlook and then showing them exactly like that on a .php page, but it isn't always that easy.

Maybe i'm tackling this issue completely the wrong way, but maybe you guys have some tips, ideas or something?

Thanks in advance, Bert

  • 写回答

2条回答 默认 最新

  • dsq1982 2015-08-10 08:54
    关注

    If emails are sent properly there should be plain text and HTML but it is an ideal world.

    So I suggest you to.

    1. Escape strings for MySQL
    2. Remove potentially danger html tags like script etc. with strip_tags

    When showing to user

    1. Unescape data from MySQL
    2. Show stripped data.

    You can also use eml parser and save eml files in DB.

    Check:

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀