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



连接到我的电子邮件帐户,通过PHP代码读取消息并将附件下载到服务器

到目前为止,我已经使用了此处出现的第三个代码确实[来自GuRu]这是一份代码副本,如果它能让成员更容易的话。

<?php
set_time_limit(3000); 
/* connect to gmail with your credentials */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$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?B15TXntec16bXlDXoteR15XXqCDXkNek15zXkdeQ15XXnSDXkg==?==?UTF-8?B150g16jXkSDXkteV15DXnNe158yCgxKS5qcGc==

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

(也许我只是没有把修复放在正确的地方?无论如何,目前我真的很绝望,如果有人能帮忙,我会很高兴找到任何实际的解决方案。(

以下是一些尝试:

发送带有附件的电子邮件以编码格式PHP显示我想这正是我问题的题目。但我无法以一种可以纳入我的代码或这里提供的代码的方式找出答案

在phpmailer中设置附件的编码虽然它并不完全相关,但它可能会有所帮助。不管怎样,我没能做到

重新加载此页面PHP:以文件名发送带有附件和UTF8的电子邮件

PHP Mail((中的Base64附件不起作用虽然它确实提供了一个代码,但它是一个发送和接收电子邮件的代码。这对我有什么帮助?

使用utf-8文件名发送MIME编码的电子邮件附件

相同问题

PHP-将字符串转换为unicode我无法将其集成到代码中

当Gmail IMAP有utf8,Outlook有ISO-8859-7时,如何读取内容类型标题并转换为utf-8?对我没有帮助

这只是。。。如果有任何想法,我将不胜感激非常感谢。

经过大量搜索和尝试,这一行挽救了局面:它是由本页上的jlh先生带来的:

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'];

我希望它能帮助做更多好事

最新更新