搜索热词
我在网上发现了
PHP代码,使用IMAP从这里下载附件到一个目录.
http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
我修改它略有变化
$structure = imap_fetchstructure($mBox,$jk);
$parts = ($structure->parts);
至
$structure = imap_fetchstructure($mBox,$jk);
$parts = ($structure);
以使其正常运行,否则我得到了一个关于stdClass如何定义一个名为$parts的属性的错误.这样做,我能够下载所有的附件.我最近测试过,但是没有起作用.那么它没有工作6次,工作了第七,然后还没有工作.我认为这跟我拧紧零件处理有关,因为count($parts)每个消息都返回1,所以没有找到任何附件.
由于它一度下载了附件,没有任何问题,我相信该地区的事情正在紧锣密鼓地在这里.在这个代码块是一个循环,通过框中的每个消息,并且在循环之后,每个imap结构只需要$零件.感谢您的任何帮助,您可以提供.我查看了PHP.net上的imap_fetchstructure页面,无法弄清楚我做错了什么.
编辑:我刚刚打了我的问题,双击检查文件夹,它都弹出.我觉得我要坚果在我开始打字之前几分钟之内,我没有运行代码,对我来说这并不需要这么长时间才能触发.我在邮箱中有大约800条消息,但是由于它在PHP的末尾打印了我的所有文件创建工作完成的声明.
这是最后的工作样本
include('application.PHP');
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBox';
$username = 'XX@XX.com';
$password = 'XX';
/* try to connect */
$inBox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inBox,'FROM "xxx@gmail.com"');
/* if emails are returned,cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inBox,$email_number,0);
$message = imap_fetchbody($inBox,2);
$structure = imap_fetchstructure($inBox,$email_number);
pre($overview);
$attachments = array();
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,$i+1);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
} // for($i = 0; $i < count($structure->parts); $i++)
} // if(isset($structure->parts) && count($structure->parts))
if(count($attachments)!=0){
foreach($attachments as $at){
if($at[is_attachment]==1){
file_put_contents($at[filename],$at[attachment]);
}
}
}
}
// echo $output;
}
/* close the connection */
imap_close($inBox);
?>
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。