douyu9433
2011-04-22 20:25 阅读 56
已采纳

解析从Outlook发送的电子邮件到php

Using this tut: parse emails

I was able to get email piping, and attachment/body parsing totally working....as long as the email is not sent from outlook.

It executes perfectly from gmail, and thunderbird, however when the incoming email is sent from outlook the script fails. I figure it has something to do with how outlook formats its messages (in the comments on the tutorial site someone mentions outlook not being compliant), but truthfully the issue is above my head. Any help would be appreciated, thanks.

fyi: this is the newest version of outlook (win7).

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doujiu8178 doujiu8178 2011-04-24 14:40

    Ok...

    So I fixed it. I was setting up the pipe in Cpanel, because it's easier. I put the pipe under "account level filtering", worked great for anything but outlook. I would have loved to have the script print data for debug, but it was never even executing when the email came from outlook. Looked in mail logs...nothing obvious. My admin on a whim suggested that I move the pipe to the "forwarders" section in cpanel. Well now it works perfect. Must be a bug in cpanel. Why is it the more you learn about computers the less sense they make.

    Just a couple other tweaks I had to implement:

    A) when writing/editing the script in a windows environment, hidden characters are added. To fix this, I upload the php file, and open it in the cpanel filemanager (us-ascii), and save it. This removes the characters. (could obviously open in *nix also) B) I had to chmod to 755, or it would not run. Scripts sitting outside my \www so no worries. C) My shebang had to be: #!/usr/bin/php -q. The q was necessary to get it running.

    Hope this helps someone else.

    点赞 评论 复制链接分享
  • dtw52353 dtw52353 2011-04-22 20:39

    As you have encountered, Outlook is the scourge of the email universe. You'll notice that the source provided in the tutorial you're using refers several times to content encoded as text/plain. The email being sent from Outlook likely contains text/html content instead of or in addition to the plaintext.

    Depending on what you wish to do with the content of the email, you may be able to adapt the script to accept text/html encoded content as well by inserting a duplicate body search below the existing one like so:

    //get the message body  
    if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/html')) == 'text/html' && isset($decoded[0]['Body'])){  
    
        $body = $decoded[0]['Body'];  
    
    } elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/html')) == 'text/html' && isset($decoded[0]['Parts'][0]['Body'])) {  
    
        $body = $decoded[0]['Parts'][0]['Body'];  
    
    } elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/html')) == 'text/html' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body'])) {  
    
        $body = $decoded[0]['Parts'][0]['Parts'][0]['Body'];  
    
    }  
    

    Which certainly isn't pretty, but should retrieve the HTML content coming from Outlook if it is detected.

    If you need to actually parse the HTML content, your problem will be a bit more complicated. Your next step would be to take a look at some of the answers for this question: Robust, Mature HTML Parser for PHP.

    Good luck!

    点赞 评论 复制链接分享

相关推荐