谷歌的GMail API下载附件

7
我正在使用新版Gmail API的PHP SDK。我该如何从邮件中获取附件? 这里是API文档,但在这种情况下,它缺少针对PHP的示例。尽管我已经看到有人使用PHP实现了这个功能。 编辑: 这是我目前为止的代码,不确定它是否正确:
$attachmentData = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId);

$myfile = fopen("excel.xlsx", "w");
fwrite($myfile, $attachmentData);
fclose($myfile);

你目前尝试了什么?给我们展示一下你的代码。如果你看到其他人已经实现了相同的结果,也许你可以从他们的代码中获得一些灵感? - Tholle
@Tholle,我已经添加了我的代码片段。清楚吗? - davidvnog
4个回答

16

首先,我们需要从附件对象中获取数据:

 $attachmentObj = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId);
 $data = $attachmentObj->getData(); //Get data from attachment object

在向文件写入之前,将数据转换为标准的 RFC 4648 基础 64 编码:

 $data = strtr($data, array('-' => '+', '_' => '/'));
 $myfile = fopen("excel.xlsx", "w+");;
 fwrite($myfile, base64_decode($data));
 fclose($myfile);

现在它运作正常了!


1
strtr替换是我所缺少的部分。我不知道我自己怎么能想到这个(即使在阅读了所有Google API文档之后)。它们为什么必要?@davidvnog,你能否在这里解释一下? - perelin

5
function getAttachment($messageId, $partId, $userId)
    {
        try {
            $client = getClient();
            $gmail = new Google_Service_Gmail($client);
            $message = $gmail->users_messages->get($userId, $messageId);
            $message_payload_details = $message->getPayload()->getParts();
            $attachmentDetails = array();
            $attachmentDetails['attachmentId'] = $message_payload_details[$partId]['body']['attachmentId'];
            $attachmentDetails['headers'] = $message_payload_details[$partId]['headers'];
            $attachment = $gmail->users_messages_attachments->get($userId, $messageId, $attachmentDetails['attachmentId']);
            $attachmentDetails['data'] = $attachment->data;
            return ['status' => true, 'data' => $attachmentDetails];
        } catch (\Google_Service_Exception $e) {
            return ['status' => false, 'message' => $e->getMessage()];
        }
    }

    function base64_to_jpeg($base64_string, $content_type) {
        $find = ["_","-"]; $replace = ["/","+"];
        $base64_string = str_replace($find,$replace,$base64_string);
        $url_str = 'data:'.$content_type.','.$base64_string;
        $base64_string = "url(".$url_str.")";
        $data = explode(',', $base64_string);
        return base64_decode( $data[ 1 ] );
    }

    // Get the API client and construct the service object.
    $client = getClient();
    $service = new Google_Service_Gmail($client);
    $opt_param = array();
    $opt_param['labelIds'] =  'INBOX';
    $opt_param['maxResults'] = 1;
    $messages = $service->users_messages->listUsersMessages($userId, $opt_param);

    foreach ($messages as $message_thread) {
            $message = $service->users_messages->get($userId, $message_thread['id']);
            $message_parts = $message->getPayload()->getParts();
            $files = array();
            $attachId = $message_parts[1]['body']['attachmentId'];
            $attach = $service->users_messages_attachments->get($userId, $message['id'], $attachId);
            foreach ($message_parts as $key => $value) {
                if ( isset($value->body->attachmentId) && !isset($value->body->data)) {
                  array_push($files, $value['partId']);
                }
            }   
    }

    if(isset($_GET['messageId']) && $_GET['part_id']){ // This is After Clicking an Attachment
        $attachment = getAttachment($_GET['messageId'], $_GET['part_id'], $userId);
        $content_type = "";
        foreach ($attachment['data']['headers'] as $key => $value) {
            if($value->name == 'Content-Type'){ $content_type = $value->value; }
            header($value->name.':'.$value->value);
        }
        $content_type_val = current(explode("/",$content_type));
        $media_types = ["video", "image", "application"];
        if(in_array($content_type_val, $media_types )){
            echo base64_to_jpeg($attachment['data']['data'], $content_type); // Only for Image files
        } else {
          echo base64_decode($attachment['data']['data']); // Other than Image Files
        }
    } else { // Listing All Attachments
            if(!empty($files)) {
                foreach ($files as $key => $value) {
                    echo '<a target="_blank" href="index.php?messageId='.$message['id'].'&part_id='.$value.'">Attachment '.($key+1).'</a><br/>';
                }
            }

    }

这是可运行的代码!如果您需要其他细节,请告诉我。 测试的媒体类型:视频,图像,docx,xlsx,.txt,.php - Sada

1

如果这段代码在你的电脑上正常运行,请尝试在线程中使用。

<?php


/**
 *  Gmail attachment extractor.
 *
 *  Downloads attachments from Gmail and saves it to a file.
 *  Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
 *  extension=php_imap.dll
 *
 */


set_time_limit(3000); 


/* connect to gmail with your credentials */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'YOUR_GMAIL_USERNAME'; # e.g somebody@gmail.com
$password = 'YOUR_GMAIL_PASSWORD';


/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());


/* get all new emails. If set to 'ALL' instead 
 * of 'NEW' retrieves all the emails, but can be 
 * resource intensive, so the following variable, 
 * $max_emails, puts the limit on the number of emails downloaded.
 * 
 */
$emails = imap_search($inbox,'ALL');

/* useful only if the above search is set to 'ALL' */
$max_emails = 16;


/* 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);

        /* get mail message, not actually used here. 
           Refer to http://php.net/manual/en/function.imap-fetchbody.php
           for details on the third parameter.
         */
        $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";

                /* prefix the email number to the filename in case two emails
                 * have the attachment with the same file name.
                 */
                $fp = fopen("./" . $email_number . "-" . $filename, "w+");
                fwrite($fp, $attachment['attachment']);
                fclose($fp);
            }

        }

        if($count++ >= $max_emails) break;
    }

} 

/* close the connection */
imap_close($inbox);

echo "Done";

?>

了解更多信息,请查看以下相关讨论和 Stack Overflow 问题


我在描述、问题和标签中都指定了,我正在尝试使用Google GMail API来实现这一目标。 - davidvnog
解决了我的问题。像魔法一样有效。 - Puneet

0

也许其他人正在寻找简单的解决方案。请参见下面的脚本代码,可用作Google脚本(Java脚本,请在此处开始教程:https://script.google.com/home/start):

// GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['sql','gz'];
//Name of the folder in google drive i which files will be put
var folderName = 'BACKUPS';
//Name of the label which will be applied after processing the mail message
var labelName = 'SavedToGDrive';



function GmailToDrive(){
  //build query to search emails
  var query = '';
  
  //uncomment to search by filetypes
  //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
  //for(var i in fileTypesToExtract){
  // query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  //}
  //query = 'in:inbox has:nouserlabels ' + query;
  
  //search by label
  query = 'label:backups-real-estate-backup';
  //exclude already downloaded mails, so you can repeat running this script 
  if timeout happened
  query = query + ' AND NOT label:savedtogdrive';
  
  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var parentFolder;
  if(threads.length > 0){
    parentFolder = getFolder_(folderName);
  }
  var root = DriveApp.getRootFolder();
  for(var i in threads){
    var mesgs = threads[i].getMessages();
 for(var j in mesgs){
      //get attachments
      var attachments = mesgs[j].getAttachments();
      for(var k in attachments){
        var attachment = attachments[k];
        var isDefinedType = checkIfDefinedType_(attachment);
     if(!isDefinedType) continue;
     var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
        parentFolder.addFile(file);
        root.removeFile(file);
      }
 }
 threads[i].addLabel(label);
  }
}

//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  var fi = DriveApp.getFoldersByName(folderName);
  if(fi.hasNext()){
    folder = fi.next();
  }
  else{
    folder = DriveApp.createFolder(folderName);
  }
  return folder;
}

//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
  n = parseInt(n);
  var date = new Date();
  date.setDate(date.getDate() - n);
  return Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy/MM/dd');
}

function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(!label){
 label = GmailApp.createLabel(name);
  }
  return label;
}

//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
  else return false;
}

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接