使用PHP阅读电子邮件附件(csv)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP阅读电子邮件附件(csv)相关的知识,希望对你有一定的参考价值。

我有一个应用程序,我认为我不能通过API与之交互。它所能做的只是以csv格式导出数据作为一次性下载或将报告通过电子邮件发送为csv到电子邮件。

我想用数据做一些性感的图表,但不想每次我需要生成报告(每月)时手动下载和输入csv数据。

我在想,创建一个基本的电子邮件帐户,每月将csv报告作为电子邮件附件发送到该帐户,并且每月在cron中运行一个php cli脚本以登录并从附件中提取csv信息。

声音可能吗?我知道如何通过PHP发送电子邮件,但我用什么库来打开电子邮件和阅读附件?

答案
<?php  
 // Standard inclusions     
 include("pChart/pData.class");  
 include("pChart/pChart.class");  

 // Dataset definition   
 $DataSet = new pData;  
 $DataSet->ImportFromCSV("Sample/CO2.csv",",",array(1,2,3,4),TRUE,0);  
 $DataSet->AddAllSeries();  
 $DataSet->SetAbsciseLabelSerie();  
 $DataSet->SetYAxisName("CO2 concentrations");  

 // Initialise the graph  
 $Test = new pChart(700,230);  
 $Test->reportWarnings("GD");  
 $Test->setFontProperties("Fonts/tahoma.ttf",8);  
 $Test->setGraphArea(60,30,680,180);  
 $Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
 $Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
 $Test->drawGraphArea(255,255,255,TRUE);  
 $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,90,2);  
 $Test->drawGrid(4,TRUE,230,230,230,50);  

 // Draw the 0 line  
 $Test->setFontProperties("Fonts/tahoma.ttf",6);  
 $Test->drawTreshold(0,143,55,72,TRUE,TRUE);  

 // Draw the line graph  
 $Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());  
 $Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);  

 // Finish the graph  
 $Test->setFontProperties("Fonts/tahoma.ttf",8);     
 $Test->drawLegend(70,40,$DataSet->GetDataDescription(),255,255,255);     
 $Test->setFontProperties("Fonts/tahoma.ttf",10);  
 $Test->drawTitle(60,22,"CO2 concentrations at Mauna Loa",50,50,50,585);  
 $Test->Render("example16.png");     
?>

你可以从qazxsw poi下载这个插件

您可以查看完整的文档http://pchart.sourceforge.net/download.php

这是用于保存从邮件到文件夹的附件的代码。

http://pchart.sourceforge.net/documentation.php

有关更多详情,请访问<?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"; ?>

以上是关于使用PHP阅读电子邮件附件(csv)的主要内容,如果未能解决你的问题,请参考以下文章

从 html 表单发送带有 csv 附件的电子邮件

超实用的php代码片段

PHP发送带有文件附件的电子邮件 - 电子邮件根本不发送

android导出到csv并作为电子邮件附件发送

如何在 PHP 中将电子邮件附件保存到服务器?

如何从电子邮件中获取 csv 附件并保存