Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14417

Re: mail attachment

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: mail attachment
Date 2014-10-27 16:44 -0400
Organization A noiseless patient Spider
Message-ID <m2mare$ao1$1@dont-email.me> (permalink)
References <m25ma0$bmf$1@news.albasani.net> <10481010.O8SIMfjRTL@PointedEars.de> <m2a83a$u0o$1@news.albasani.net>

Show all headers | View raw


On 10/23/2014 2:43 AM, Jan Novak wrote:
> Am 22.10.2014 um 20:42 schrieb Thomas 'PointedEars' Lahn:
>> Jan Novak wrote:
>>
>>> i want to get mails from my mailbox and the attachments.
>>
>> Use an existing MTA or a MUA; _not_ PHP.
> 
> 
> ??? So what ?
> For example: If i ask for PHP questions, you answer with - use Java !?
> How can i use in an PHP Code other then PHP ?
> I can not Install an MTA to get some E-Mails from somewhere.
> 
>> SSL is obsolete.  Your provider would be wise to have disabled it by now,
>> and you would be wise not to use it anymore; see POODLE.
> 
> Yes, but with tls it is the same problem !
> 
>> The host name of your e-mail server so that, hopefully, one can determine
>> its capabilities.
> 
> This is not relevant, because it does not work on any servers, what i
> have try, so its an (my) PHP problem and thats the reason, why i ask it
> here !
> 
> 
>> Make sure that your e-mail provider supports any of these protocols.
> 
> Absolutly right. This is an basic and i does it.
> 
> So now i am at the same point as before, therefore below is my code.
> Rmember - for me is important, to get and save the attachment.
> 
> Jan
> 
> 
> 
> function getBody($uid, $imap) {
>     $body = get_part($imap, $uid, "TEXT/HTML");
>     if ($body == "") {
>         $body = get_part($imap, $uid, "TEXT/PLAIN");
>     }
>     return $body;
> }
> 
> function get_part($imap, $uid, $mimetype, $structure = false,
> $partNumber = false) {
>     if (!$structure) {
>         $structure = imap_fetchstructure($imap, $uid, FT_UID);
>     }
>     if ($structure) {
>         if ($mimetype == get_mime_type($structure)) {
>             if (!$partNumber) {
>                 $partNumber = 1;
>             }
>             $text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
>             switch ($structure->encoding) {
>                 case 3: return imap_base64($text);
>                 case 4: return imap_qprint($text);
>                 default: return $text;
>             }
>         }
> 
>         // multipart
>         if ($structure->type == 1) {
>             foreach ($structure->parts as $index => $subStruct) {
>                 $prefix = "";
>                 if ($partNumber) {
>                     $prefix = $partNumber . ".";
>                 }
>                 $data = get_part($imap, $uid, $mimetype, $subStruct,
> $prefix . ($index + 1));
>                 if ($data) {
>                     return $data;
>                 }
>             }
>         }
>     }
>     return false;
> }
> 
> function get_mime_type($structure) {
>     $primaryMimetype = array("TEXT", "MULTIPART", "MESSAGE",
> "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
> 
>     if ($structure->subtype) {
>         return $primaryMimetype[(int)$structure->type] . "/" .
> $structure->subtype;
>     }
>     return "TEXT/PLAIN";
> }
> 
> function downloadAttachment($imap, $uid, $partNum, $encoding, $path) {
>     $partStruct = imap_bodystruct($imap, imap_msgno($imap, $uid),
> $partNum);
>     $filename = $partStruct->dparameters[0]->value;
>     $message = imap_fetchbody($imap, $uid, $partNum, FT_UID);
> 
>     switch ($encoding) {
>         case 0:
>         case 1:
>             $message = imap_8bit($message);
>             break;
>         case 2:
>             $message = imap_binary($message);
>             break;
>         case 3:
>             $message = imap_base64($message);
>             break;
>         case 4:
>             $message = quoted_printable_decode($message);
>             break;
>     }
> 
>     header("Content-Description: File Transfer");
>     header("Content-Type: application/octet-stream");
>     header("Content-Disposition: attachment; filename=" . $filename);
>     header("Content-Transfer-Encoding: binary");
>     header("Expires: 0");
>     header("Cache-Control: must-revalidate");
>     header("Pragma: public");
>     echo $message;
> 
> }
> 
> function saveAttachment($imap, $uid, $partNum, $encoding, $path,
> $attachmentName) {
>     $partStruct = imap_bodystruct($imap, imap_msgno($imap, $uid),
> $partNum);
>     $filename = $partStruct->dparameters[0]->value;
>     $message = imap_fetchbody($imap, $uid, $partNum, FT_UID);
> 
>     switch ($encoding) {
>         case 0:
>         case 1:
>             $message = imap_8bit($message);
>             break;
>         case 2:
>             $message = imap_binary($message);
>             break;
>         case 3:
>             $message = imap_base64($message);
>             break;
>         case 4:
>             $message = quoted_printable_decode($message);
>             break;
>     }
>     
>     $fdw = fopen($path ."/". $attachmentName, "w+");
>     fwrite($fdw, $message);
>     fclose($fdw);
> 
> }
> 
> 
> function getAttachments($imap, $mailNum, $part, $partNum) {
>     
>     $attachments = array();
>     if (isset($part->parts)) {
>         foreach ($part->parts as $key => $subpart) {
>             if($partNum != "") {
>                 $newPartNum = $partNum . "." . ($key + 1);
>             }
>             else {
>                 $newPartNum = ($key+1);
>             }
>             $result = getAttachments($imap, $mailNum, $subpart,
>                     $newPartNum);
>             if (count($result) != 0) {
>                 array_push($attachments, $result);
>             }
>         }
>     }
>     else if (isset($part->disposition)) {
>         if ($part->disposition == "ATTACHMENT") {
>             $partStruct = imap_bodystruct($imap, $mailNum,
>                     $partNum);
>             $attachmentDetails = array(
>                     "name"    => $part->dparameters[0]->value,
>                     "partNum" => $partNum,
>                     "enc"     => $partStruct->encoding
>             );
>             return $attachmentDetails;
>         }
>     }
> 
>     return $attachments;
> }
> 
> function openimap(){
>     global $cfg;
>     
>     if (extension_loaded("imap")) {
>         $mailbox =
> "{".$cfg["FETCH_MAILSERVER"].":".$cfg["FETCH_PORT"]."/".$cfg["FETCH_PROTO"].$cfg["FETCH_PROTOOPTS"]."}INBOX";
> 
>         $user = $cfg["FETCH_USER"];
>         $pass = $cfg["FETCH_PASSWD"];
>         $imap = imap_open($mailbox, $user, $pass)  or die('Error : ' .
> imap_last_error());;
>         $error="";
>     } else {
>         $error="php5-imap not loaded";
>         $imap="";       
>     }
>     
>     return array($imap, $error);
> }
> 
> 

Jan, looking at your code it seems to be incomplete.  Where do you call
any of your attachment-related functions?  Is it somewhere else in your
code?

Also, it looks like your get_part() routine will only fetch parts that
have a mime_type of TEXT/PLAIN.  Again, is there more to your code than
this?

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

mail attachment Jan Novak <repcom@gmail.com> - 2014-10-21 15:14 +0200
  Re: mail attachment Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-22 20:42 +0200
    Re: mail attachment Jan Novak <repcom@gmail.com> - 2014-10-23 08:43 +0200
      Re: mail attachment Chuck Anderson <cycletourist@invalid.invalid> - 2014-10-23 23:59 -0600
      Re: mail attachment Jerry Stuckle <jstucklex@attglobal.net> - 2014-10-24 11:21 -0400
        Re: mail attachment Jan Novak <repcom@gmail.com> - 2014-10-27 09:23 +0100
          Re: mail attachment Jerry Stuckle <jstucklex@attglobal.net> - 2014-10-27 14:16 -0400
            Re: mail attachment Jan Novak <repcom@gmail.com> - 2014-10-28 11:58 +0100
              Re: mail attachment Jerry Stuckle <jstucklex@attglobal.net> - 2014-10-28 08:21 -0400
      Re: mail attachment Jerry Stuckle <jstucklex@attglobal.net> - 2014-10-27 16:44 -0400
        Re: mail attachment Jan Novak <repcom@gmail.com> - 2014-10-28 14:16 +0100
          SOLVED: mail attachment Jan Novak <repcom@gmail.com> - 2014-10-28 14:25 +0100
            Re: SOLVED: mail attachment Jerry Stuckle <jstucklex@attglobal.net> - 2014-10-28 09:35 -0400
  Re: mail attachment Jerry Stuckle <jstucklex@attglobal.net> - 2014-10-23 06:07 -0400

csiph-web