Important alert: (current site time 5/24/2013 9:26:52 AM EDT)
 

VB icon

Download gmail attachment in PHP

Email
Submitted on: 4/27/2012 5:23:28 PM
By: Amit Kumar Gaur  
Level: Advanced
User Rating: Unrated
Compatibility: PHP 5.0
Views: 1253
author picture
(About the author)
 
     Download gmail attachment in PHP If you want to download gmail attachment by PHP code?, then here is the code that will do it. just copy and paste the entire code and run it.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Download gmail attachment in PHP
// Description:Download gmail attachment in PHP
If you want to download gmail attachment by PHP code?, then here is the code that will do it.
just copy and paste the entire code and run it.
// By: Amit Kumar Gaur
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3019&lngWId=8//for details.//**************************************

See here full code.
http://amitkgaur.blogspot.in/2010/07/download-gmail-attachment-in-php.html
<?php
class ImapMail
{
var $dirPath,$deleteEmails=false;
var $imapInBox;
function __construct($mailHost,$emailLogin,$pass) {
$this->imapInBox=imap_open($mailHost,$emailLogin,$pass) or die("Unable to connect:".imap_last_error());
}
function getdecodevalue($msg,$bodyType) {
if($bodyType==0 || $bodyType==1) {
$msg = imap_base64($msg);
}
if($bodyType==2) {
$msg = imap_binary($msg);
}
if($bodyType==3 || $bodyType==5) {
$msg = imap_base64($msg);
}
if($bodyType==4) {
$msg = imap_qprint($msg);
}
return $msg;
}
function downLoadAttachment($dirPath,$deleteEmails=false) {
$nAttachment = 0;
$dirPath = str_replace('\\', '/', $dirPath);
if (substr($dirPath, strlen($dirPath) - 1) != '/') {
$dirPath .= '/';
}
$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";
$nEmails = imap_search($this->imapInBox, 'ALL', SE_UID);
$j=-1;
for ($j = 0; $j < count($nEmails); $j++) {
$j++;
$messStructure = imap_fetchstructure($this->imapInBox, $nEmails[$j] , FT_UID);
$overview = imap_fetch_overview($this->imapInBox,$j ,0);
echo '<span>Read/Unread'.($overview[0]->seen ? 'read' : 'unread').'</span>';
echo '<span>Subject:: '.$overview[0]->subject.'</span> ';
echo '<span>From:: '.$overview[0]->from.'</span>';
echo '<span>On:: '.$overview[0]->date.'</span>';
$body = imap_fetchbody($this->imapInBox,$j ,2);
echo $body;
if(isset($messStructure->parts)) {
$parts = $messStructure->parts;
$fpos=2;
for($i = 1; $i < count($parts); $i++) {
$message["pid"][$i] = ($i);
$part = $parts[$i];
if(isset($part->disposition) && $part->disposition == "ATTACHMENT") {
$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
$message["subtype"][$i] = strtolower($part->subtype);
$fileName=$part->dparameters[0]->value;
$mess = imap_fetchbody($this->imapInBox,$j+1,$fpos); 
$fp=fopen($dirPath.$fileName, 'w');
$data=$this->getdecodevalue($mess,$part->type);
fwrite($fp,$data);
fclose($fp);
$nAttachment++;
$fpos+=1;
}
}
if ($this->deleteEmails) {
//imap_delete($this->imapInBox,$j+1);
}
}
}
if ($this->deleteEmails) {
//imap_expunge($this->imapInBox);
}
imap_close($this->imapInBox);
return ("Completed ($nAttachment attachment(s) downloaded into $dirPath)");
}
}
$mailHost="{imap.gmail.com:993/imap/ssl}INBOX";
$emailLogin="FULL-GMAIL-ID-HERE";
$pass="PASSWORD-HERE";
$dirPath=$_SERVER['DOCUMENT_ROOT']."test/gmail/";
$deleteEmails=false;
$mailObj=new ImapMail($mailHost,$emailLogin,$pass);
echo $mailObj->downLoadAttachment($dirPath,$deleteEmails=false);
?>


Other 30 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.