2013-02-08 38 views
0

我正在使用此脚本(http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-2/)将电子邮件附件保存到我的服务器上。您也可以在浏览器上查看完整脚本:http://stuporglue.org/downloads/mailReader.txt解析电子邮件并将附件保存到目录中的问题

一切正常,但这里有两个问题。

1),我保存到的目录中的图像的文件名不是一个图像:1360341823_test_jpg

How to convert the file name from 1360341823_test_jpg to 1360341823_test.jpg 
in the script? 

2)保存在目录中的文件的权限600

How to make it default 755 or 775? 

我相信这是将图像转换脚本:

function saveFile($filename,$contents,$mimeType){ 
global $save_directory,$saved_files,$debug; 
$filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename); 

$unlocked_and_unique = FALSE; 
    while(!$unlocked_and_unique){ 
    // Find unique 
    $name = time()."_".$filename; 
     while(file_exists($save_directory.$name)) { 
     $name = time()."_".$filename; 
     } 

    // Attempt to lock 
    $outfile = fopen($save_directory.$name,'w'); 
     if(flock($outfile,LOCK_EX)){ 
     $unlocked_and_unique = TRUE; 
     } else { 
     flock($outfile,LOCK_UN); 
     fclose($outfile); 
     } 
    } 

fwrite($outfile,$contents); 
fclose($outfile); 

    // This is for readability for the return e-mail and in the DB 
    $saved_files[$name] = Array(
    'size' => formatBytes(filesize($save_directory.$name)), 
    'mime' => $mimeType 
); 
}  

任何他的功能的LP?

+0

您可以考虑使用此Python脚本:https://github.com/auino/mail-附件存档器 – auino 2016-10-04 06:22:15

回答

1

原始脚本使用数据存储在数据库中,但我认为您正试图将其保存在文件中。你是不带扩展名创建该文件的位置:

// Attempt to lock 
    $outfile = fopen($save_directory.$name,'w'); 

无论是行后添加.JPG:

#outfile.=".jpg"; 

,如果你不想改变脚本,然后你可以用其他的方式:

$contents = file_get_contents($save_directory.$name); 
$outfile = fopen($save_directory.$new_name,'w'); 
write($outfile,$contents); 
fclose($outfile); 

这将解决您的第一个问题和第二个问题的善意使用所提供的FTP或控制面板访问文件更改所有权。如果您不知道任何事情,请联系您的虚拟主机服务提供商以分享所有权从755到775