简体   繁体   中英

Upload a file to server sent by email

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM