douju9272 2014-07-14 10:09
浏览 73

php MIME mailparse - imap。 如何解析正文和正文html?

It's been a while that I'm trying to do a job that gets the emails from gmail, parses them and then puts the required data in the database, but I'm finding it hard to work with PHP MailParse which documentation is very poor. I tried MimeMailParser.class.php that I found on google code (that it use MailParse inside) but it doesn't work as expected (or maybe is my fault).

The situation: clients send emails on different mail addresses, and all these emails are forwarded to a unique email address. I have to get the emails on this central mailbox.

Objective: I want to parse the email BODY TEXT and BODY HTML, The script: using MimeMailParse:

$body = imap_body($imap_connection,$email_number);
$parser->setText($body);
$text = $parser->getMessageBody('text');
$html = $parser->getMessageBody('html');

The output of the $text like $body, The output of $html is empty.

using raw Mailparse:

$buffer = imap_body($imap, $i);
$mail = mailparse_msg_create();
$pro = mailparse_msg_parse($mail,$buffer);
$struct = mailparse_msg_get_structure($mail); 
print_r($struct); // this outputs always: 1
foreach($struct as $st) { 
    $section = mailparse_msg_get_part($mail, $st); 
    $info = mailparse_msg_get_part_data($section);   
    print_r($info);
}

Output:

Array
(
    [0] => 1
)
struct endArray
(
    [headers] => Array
        (
        )

    [starting-pos] => 0
    [starting-pos-body] => 48
    [ending-pos] => 4186
    [ending-pos-body] => 4186
    [line-count] => 93
    [body-line-count] => 91
    [charset] => us-ascii
    [transfer-encoding] => 8bit
    [content-type] => text/plain
    [content-base] => /
)

So how can I get the html body and the text body, using these library or other library(I hope they have some examples and documentations)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥15 Python3.5 相关代码写作
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像