当我通过 PHP 代码从我的电子邮件帐户下载文件时,以希伯来语附加的文件名被编码

Posted

技术标签:

【中文标题】当我通过 PHP 代码从我的电子邮件帐户下载文件时,以希伯来语附加的文件名被编码【英文标题】:When I download files from my e-mail account by PHP code, the file name attached in Hebrew is encoded 【发布时间】:2020-02-25 05:48:25 【问题描述】:

连接到我的电子邮件帐户以阅读邮件并通过 php 代码将附件下载到服务器

到目前为止,我已经使用了third code that appears here 确实 [来自 GuRu] 如果它可以使会员更容易,这里是代码的副本。

    <?php

set_time_limit(3000); 

/* connect to gmail with your credentials */
$hostname = 'imap.gmail.com:993/imap/sslINBOX';
$username = 'YOUR_USERNAME'; 
$password = 'YOUR_PASSWORD';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect 
to Gmail: ' . imap_last_error());

$emails = imap_search($inbox, 'FROM "abc@gmail.com"');

/* if any emails found, iterate through each email */
if($emails) 

    $count = 1;

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) 
    

    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);

    $message = imap_fetchbody($inbox,$email_number,2);

    /* get mail structure */
    $structure = imap_fetchstructure($inbox, $email_number);

    $attachments = array();

    /* if any attachments found... */
    if(isset($structure->parts) && count($structure->parts)) 
    
        for($i = 0; $i < count($structure->parts); $i++) 
        
            $attachments[$i] = array(
                'is_attachment' => false,
                'filename' => '',
                'name' => '',
                'attachment' => ''
            );

            if($structure->parts[$i]->ifdparameters) 
            
                foreach($structure->parts[$i]->dparameters as $object) 
                
                    if(strtolower($object->attribute) == 'filename') 
                    
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i]['filename'] = $object->value;
                    
                
            

            if($structure->parts[$i]->ifparameters) 
            
                foreach($structure->parts[$i]->parameters as $object) 
                
                    if(strtolower($object->attribute) == 'name') 
                    
                        $attachments[$i]['is_attachment'] = true;
                        $attachments[$i]['name'] = $object->value;
                    
                
            

            if($attachments[$i]['is_attachment']) 
            
                $attachments[$i]['attachment'] = imap_fetchbody($inbox, 
$email_number, $i+1);

                    /* 3 = BASE64 encoding */
                    if($structure->parts[$i]->encoding == 3) 
                     
                        $attachments[$i]['attachment'] = 
base64_decode($attachments[$i]['attachment']);
                    
                    /* 4 = QUOTED-PRINTABLE encoding */
                    elseif($structure->parts[$i]->encoding == 4) 
                     
                        $attachments[$i]['attachment'] = 
quoted_printable_decode($attachments[$i]['attachment']);
                    
                
            
        

    /* iterate through each attachment and save it */
    foreach($attachments as $attachment)
    
        if($attachment['is_attachment'] == 1)
        
            $filename = $attachment['name'];
            if(empty($filename)) $filename = $attachment['filename'];

            if(empty($filename)) $filename = time() . ".dat";
            $folder = "attachment";
            if(!is_dir($folder))
            
                 mkdir($folder);
            
            $fp = fopen("./". $folder ."/". $email_number . "-" . 
$filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
            
        
    
 

/* close the connection */
imap_close($inbox);

echo "all attachment Downloaded";

?>

英文或数字名称的附件非常棒! 但如果名称是希伯来语,则文件名的编码如下: 1424 - =? UTF-8?乙? 15TXntec16bXlCDXoteR15XXqCDXkNek15zXkdeQ15XXnSDXkg ==? = =? UTF-8?乙? 150g16jXkSDXkteV15DXnNee158yICgxKS5qcGc =? =

我一直在一天中的任何时间搜索几天,我尝试了很多代码。同时, 我不知道该怎么办!

(也许我只是没有将修复程序放在正确的位置?无论如何,目前我真的很绝望,如果有人可以提供帮助,我很乐意提供任何实用的解决方案。)

以下是一些尝试:

Sending an email with attachment displays in encoded format PHP 我认为这正是我的问题的标题。但是我无法以可以合并到我的代码或此处提供的代码中的方式找出答案

setting encoding for attachment in phpmailer 虽然它并不完全相关,但它可能会有所帮助。反正我是来不及了

Reload this Page PHP: Send email with attachment and UTF8 in filenames

Base 64 Attachment in PHP Mail() not working 虽然它确实提供了代码,但它是用于发送和接收电子邮件的代码。这对我有什么帮助?

Sending MIME-encoded email attachments with utf-8 filenames

同样的问题

PHP - Convert string to unicode 我无法将其集成到代码中

How to read the Content Type header and convert into utf-8 while Gmail IMAP has utf8 and Outlook has ISO-8859-7? 没有帮助我

这只是一部分... 我会很感激任何想法 谢谢!!

【问题讨论】:

【参考方案1】:

经过大量搜索和尝试,这行挽救了局面:是jlh先生带来的on this page:

iconv_mime_decode($str, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8')

我把它放在这里:

                $filename = $attachment['name'];
                    $filename = iconv_mime_decode($filename, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');

            if(empty($filename)) $filename = $attachment['filename'];

希望对做更多好事有所帮助

【讨论】:

以上是关于当我通过 PHP 代码从我的电子邮件帐户下载文件时,以希伯来语附加的文件名被编码的主要内容,如果未能解决你的问题,请参考以下文章

PHP xampp mail()函数不起作用

当我尝试使用我的 Google 服务帐户通过 Gmail API 发送电子邮件时,“myemail@mydomain.com”的委派被拒绝

从托管在 Hostgator 服务器上的 PHP 脚本发送 SMTP 电子邮件

使用 php 从贝宝帐户付款到另一个贝宝帐户

是啥导致我的“test.txt”文件从 Python 发送到我的 gmail 帐户时,当我在我的电子邮件收件箱中收到它时,它会显示为“无标题”?

安装配置文件时出现问题