【问题标题】:how to get content of html template & inline images from gmail using imap php?如何使用 imap php 从 gmail 获取 html 模板和内联图像的内容?
【发布时间】:2016-01-30 16:25:22
【问题描述】:

我正在使用 imap php 处理邮箱。我正在使用 imap_fetchbody($mbox, 4,2) 来读取邮件的内容。它适用于普通短信,但当电子邮件具有内联图像(不是附件)或 html 模板时,它无法正常工作。它只是返回一些加密代码。我参考了很多代码。我用的是:

$mbox = imap_open($hostname,$username,$password) 
        or die('Cannot connect to Gmail: ' .       imap_last_error());
$message=imap_fetchbody($mbox, 4,2);
$decoded_data = base64_decode($message);
file_put_contents('image002.jpg',$decoded_data); 
<img src="image002.jpg">   

它获取内联图像,但如果邮件有多个内联图像,它只返回最后一个图像。我不知道如何获取 html 模板消息,谁能帮助我?

【问题讨论】:

标签: php email imap


【解决方案1】:

这个post 会像对我一样帮助你! 你可以做 strip_tags($message) 只得到文本。 希望有帮助

【讨论】:

    【解决方案2】:

    <?php
    $hostname = '{********:993/imap/ssl}INBOX';
    $username = '*********';
    $password = '******';
    
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error());
    
    $emails = imap_search($inbox,'ALL');
    
    if($emails) {
        $output = '';
        rsort($emails);
    
        foreach($emails as $email_number) {
            $overview = imap_fetch_overview($inbox,$email_number,0);
            $structure = imap_fetchstructure($inbox, $email_number);
    
            if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
                $part = $structure->parts[1];
                $message = imap_fetchbody($inbox,$email_number,2);
    
                if($part->encoding == 3) {
                    $message = imap_base64($message);
                } else if($part->encoding == 1) {
                    $message = imap_8bit($message);
                } else {
                    $message = imap_qprint($message);
                }
            }
    
            $output.= '<div class="toggle'.($overview[0]->seen ? 'read' : 'unread').'">';
            $output.= '<span class="from">From: '.utf8_decode(imap_utf8($overview[0]->from)).'</span>';
            $output.= '<span class="date">on '.utf8_decode(imap_utf8($overview[0]->date)).'</span>';
            $output.= '<br /><span class="subject">Subject('.$part->encoding.'): '.utf8_decode(imap_utf8($overview[0]->subject)).'</span> ';
            $output.= '</div>';
    
            $output.= '<div class="body">'.$message.'</div><hr />';
        }
    
        echo $output;
    }
    
    imap_close($inbox);
    ?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2015-01-17
      • 1970-01-01
      相关资源
      最近更新 更多