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:

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)