dongyan1974 2017-03-10 19:00
浏览 63

通过电子邮件将文件上传到服务器

I have a general question regarding a file upload via email, as I don't know if this is possible at all.

I have a shared webspace with Wordpress installed on it. We want to enhance our users to upload files via email (.pdf, .doc, etc.), send to a specific email address.

It doesn't matter if the file is recognized by Wordpress, the files could also just be uploaded by my own .php-script to a specific folder.

Is this in general possible?

When I search the web for this question, I just find reverse solutions (email notification when a file upload happens).

  • 写回答

1条回答 默认 最新

  • douxiuyi6529 2017-03-10 20:07
    关注

    I have done it a few times, and is always kind of a pain. you should also log into the mailbox once in awhile to check on it. Also, remember you will have spam, bad formatted emails, reply all emails in there so account for that.

    also fun decoding the email body as it comes in a million flavors, with embedded graphics as well.

    This is what I used to monitor a mailbox that had text based alerts.

     class someEmailClass
    
     function inbox() {
        if( DEBUG) $GLOBALS['log']->Info( __CLASS__ .'.'. __FUNCTION__ .'()' );
    
        $this->msg_cnt = imap_num_msg($this->conn);
        $in = array();
    
        imap_headers( $this->conn); // to boost performance somehow
    
        $emails_to_fetch = $this->batchSize;
        if( $this->batchSize > $this->msg_cnt) 
    // if fewer inbox items than batch size
        {
            $emails_to_fetch = $this->msg_cnt;
        }
    
        for($i = 1; $i <= $emails_to_fetch; $i++) {
            $email = array(
                'index'     => $i,
                'header'    => imap_headerinfo($this->conn, $i),
                'structure' => imap_fetchstructure($this->conn, $i)
            );
    
            $body = imap_fetchbody( $this->conn, $i, "1.1");
            if ($body == "") {
                $body = imap_fetchbody( $this->conn, $i, "1");
            }
            $email['body'] = $body;
            $email['body2'] = self::email_body_decode( $body, (int)$email['structure']->encoding );
    
            $in[] = $email;
    
        }
    
        $this->inbox = $in;
    
        $this->Errors = imap_errors();
    }
    
        public static function email_body_decode( $data, $code =0)
    {
        switch( $code){
    //            case 0:
    //                return imap_utf7_decode($data);
            case 1:
                return imap_utf8($data);
            case 3:
                return imap_base64($data);
            case 4:
                return imap_qprint($data);
            default:
                return $data;
        }
    }
    
    // http://www.php.net/manual/en/function.imap-open.php
    function connect_debug()
    {
        try {
            $this->conn = (imap_open("{mail.domain.com:993/imap/notls/ssl/novalidate-cert}INBOX", <user>, <pass>));
            if( $this->conn == false) {
                $err = 'Could not connect to email server ['. imap_last_error() .']';
                $this->Errors[] = $err;
                throw new \Exception( $err);
            }
    
        }
        catch( \Exception $ex){
            if( DEBUG) $GLOBALS['log']->Error( $ex );
            throw $ex;
        }
    }
    
    }
    

    it is not complete, but will let you play with the message body at least after you figure out how to connect to the mailbox on the email server

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度