下面我使用PHP的imap库循环遍历电子邮件.它有效,但我一直坚持从邮件中保存附件.
<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'someemail@somedomain.com';
$password = 'somepassword';
/* try to connect */
$imap_connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($imap_connection,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
$structure = imap_fetchstructure($imap_connection,$email_number);
if(isset($structure->parts) && count($structure->parts)){
for($i = 0; $i < count($structure->parts); $i++){
if(!empty($structure->parts[$i]->bytes)){
if(!empty($ifdparameters)){
echo "File: ----".$structure->parts[$i]->dparameters[0]->value;
// returns: testMeOut.jpg
}
}
} //eof $i loop
} //eof $structure->parts
} // eof foreach $emails
} // eof if($emails)
?>
Run Code Online (Sandbox Code Playgroud)
我试图了解如何将附件保存到我的服务器上的目录.我知道:$ structure-> parts [$ i] - > dparameters [0] - > value; 正在返回附件的名称.
我确定我需要做一些与imap_fetchbody()有关的事情,但我完全很困惑阅读在线教程和php的网站.
任何人都可以看到我失去了关于如何从$ message保存附件的最后几个步骤?
一旦写下来用于下载Excel文件;
猜猜它可以改为几乎任何电子邮件附件.
基本上它演示了您缺少的两个步骤:
$message = array();
$message["attachment"]["type"][0] = "text";
$message["attachment"]["type"][1] = "multipart";
$message["attachment"]["type"][2] = "message";
$message["attachment"]["type"][3] = "application";
$message["attachment"]["type"][4] = "audio";
$message["attachment"]["type"][5] = "image";
$message["attachment"]["type"][6] = "video";
$message["attachment"]["type"][7] = "other";
function get_decode_value($message, $encoding) {
switch($encoding) {
case 0:case 1:$message = imap_8bit($message);break;
case 2:$message = imap_binary($message);break;
case 3:case 5:$message=imap_base64($message);break;
case 4:$message = imap_qprint($message);break;
}
return $message;
}
for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) {
$structure = imap_fetchstructure($mbox, $jk , FT_UID);
/* retrieve message timestamp */
$header = imap_headerinfo($mbox, $jk);
$timestamp = $header->udate;
$parts = $structure->parts;
$fpos=2;
for($i = 1; $i < count($parts); $i++) {
$message['pid'][$i] = ($i);
$part = $parts[$i];
if($part->disposition == "ATTACHMENT") {
$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
$message["subtype"][$i] = strtolower($part->subtype);
$ext = $part->subtype;
$params = $part->dparameters;
$filename = $part->dparameters[0]->value;
$body="";$data="";
if($filename=='SomeFile.XLS'){
$body = imap_fetchbody($mbox, $jk, $fpos);
$dst = '/SomeLocalPath/SomeOtherFile.xls';
/* don't overwrite */
if(!file_exists($dst)){
$fp = fopen($dst, 'w');
$data = get_decode_value($body, $part->type);
fputs($fp, $data);
fclose($fp);
}
}
$fpos++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么无法通过任何Google API访问附件...
为什么没有双向GMail API,比如单向Mandrill API?
| 归档时间: |
|
| 查看次数: |
10818 次 |
| 最近记录: |