0

请原谅我这里的新手状态...

我需要从我的 (HostGator) 电子邮件帐户下载电子邮件,解析并将它们显示到浏览器窗口中,包括所有附件。然后我将使用浏览器的打印功能将它们打印出来......

我得到了一个纯文本版本,但这个项目也需要出现图像/附件。网上有很多零碎的东西,但没有万能的解决方案。他们中的许多人甚至似乎都没有工作!

我已经为此沮丧了好几个星期。

我安装了 PECL Mailparse(尽管如果另一种解决方案更好,我不喜欢那个),以及 PHP IMAP 扩展。

有没有人对此有解决方案,或者愿意为我联系和处理这个问题?我发誓,在这一点上,我会以你的名字命名我的第一个孩子!

这是我现有的代码:

<?php

// This seems to be rx'ed by the PHP Master tutorial for dealing w attachments, but draws code errors???
// include 'emailAttachments.inc';

// connects successfuly to the inmate database, where I store the received emails
include 'includes/dbConnect.inc';

// connects successfully to the email hosting service
include 'includes/emailConnect.inc';

/* get the emails */
//$emails = imap_search($inbox,'UNSEEN');       //Restore this one once done testing; only new ones show
$emails = imap_search($inbox,'ALL');        //This is for testing, so ALL emails show up w/out re-sending new

/* if emails are returned, cycle through each... */
if($emails) {

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get the overview part to the email */
        $overview = imap_fetch_overview($inbox,$email_number,0);

        // returns the structure (NOT the data within the structure...)
        $structure = imap_fetchstructure($inbox,$email_number,0);

        /* get body part of this email */
        /* note that passing FT_PEEK as the 4th param prevents the email being marked as 'read' on server */
        if ($message = imap_fetchbody($inbox,$email_number,1.1)=='') {
            $message = imap_fetchbody($inbox,$email_number,1);
        }
        // an earlier attempt...
        // Below includes FT_PEEK, which keeps messages from being excluded in subsequent runs of this code...
        /*if ($message = imap_fetchbody($inbox,$email_number,1.1,FT_PEEK)=='') {
            $message = imap_fetchbody($inbox,$email_number,1,FT_PEEK);
        }*/

        $message = trim(quoted_printable_decode($message));

        $trimmedRecipient = trim(str_replace('"','',$overview[0]->to)); // Used in CBS_Inmate table query

        // Used to add data to CBS_Email table on INSERT query, below
        $emlTimestamp = $overview[0]->date;
        $emlTo = trim(str_replace('\\','',$overview[0]->to));
        $emlTo = trim(str_replace('"','',$overview[0]->to));
        /*$emlFrom = htmlentities($overview[0]->from, ENT_QUOTES);*/
        $emlFrom = getSetMaskedEmailAddress(htmlentities($overview[0]->from, ENT_QUOTES));
        $emlSubject = $overview[0]->subject;

        // Prepare and use the current Client/recipient to retrieve full Client info from table...
        $pareEmailAdd = strtolower(strtok(str_replace('"','',$overview[0]->to),"@"));   // l.c and pre-@
        $emailAdd = $pareEmailAdd;                          // copy it to final var, so if no more processing...
        if (substr_count($pareEmailAdd,"<")>0) {            // if there still a '<' in there
            $pareEmailAdd = strtok($pareEmailAdd, "<");     // take the '<' out
            $emailAdd = trim(strtok(" "));                  // like this...
        }

        // Lookup on the Inmates (Client) table to see if there is a matching, active client...
        if (inmateFoundCount($mysqli, $emailAdd) > 0) {
?>

// ..... (Page layout, wherein I echo various parts of the email)

<?php

    } else if ($emailAdd = 'webmaster') {
        echo '<p>&nbsp;</p><p style="color:red">WEBMASTER received a contact. <a href="https://gator2005.hostgator.com:2096/cpsess9352445381/3rdparty/roundcube/?_task=mail" target="_blank">Click here to Login to HostGator email.</a></p><p>&nbsp;</p>';
    } else {
        echo '<p>&nbsp;</p>Received Invalid/Inactive/Non-Client Email: (' . $emailAdd . ')<p>&nbsp;</p><p>&nbsp;</p>';
    } // Ends the 'If Inmate Found Count' process
    $nuthin = "";

    $email_Query = 'INSERT INTO `CBS_Email` ' . 
                    'VALUES ("' . 
                            $nuthin . '","' . 
                            $emlTimestamp . '","' . 
                            $emailAdd . '","' .                     // TO
                            trim(addslashes($emlFrom)) . '","' .    // FROM
                            trim(addslashes($emlSubject)) . '","' . // SUBJECT
                            trim(addslashes($message)) . '","' .    // MESSAGE
                            $nuthin . '","' . 
                            $overview . '","' . 
                            date("Y-m-d H:i:s") . '")';
    $emailResult = $mysqli->query($email_Query) or die($mysqli->error.__LINE__);
} // Ends 'ForEach $Email' loop

    } else {
    echo "<br /><br /><br />&nbsp;&nbsp;&nbsp; ";
    ?><A HREF="javascript:history.back()"><< back</A><?php
    echo "&nbsp;&nbsp;&nbsp; NO NEW MESSAGES in '" . strtoupper($username) . "'&nbsp;&nbsp;&nbsp; ";
    ?><a href="javascript:document.location.reload();">refresh >></a>
<?php
    } // Ends the 'If $Email' process

?>
        <!-- end .content --></div>

    <div class="footer">
    <!-- end .footer --></div>

<?php

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


function getSetMaskedEmailAddress($emailFrom) {

    // connect to the inmate database
    include 'includes/dbConnect.inc';

    $Query = "SELECT * FROM `CBS_Email_Address_Masking` WHERE `unMaskedEmail` = '". $emailFrom . "'";
    $result = $mysqli->query($Query) or die($mysqli->error.__LINE__);
    $return = $result->fetch_assoc();
    $Found_maskedEmail = $return['maskedEmail'];

    $Query = "SELECT * FROM `CBS_Email_Address_Masking` WHERE `maskedEmail` = '". $emailFrom . "'";
    $result = $mysqli->query($Query) or die($mysqli->error.__LINE__);
    $return = $result->fetch_assoc();
    $Found_unMaskedEmail = $return['unMaskedEmail'];

    if ($Found_maskedEmail) {

        // Found a Masked Email for the passed Unmasked Email. Return the Masked Email.
        return $Found_maskedEmail;

    } else if ($Found_unMaskedEmail) {

        // Found an Unmasked Email for the passed Masked Email. Return the Unmasked Email.
        return $Found_unMaskedEmail;

    } else {

        // Failed to find any entries. Create a new Masked Email, and create a table entry containing the new Masked Email and the passed Unmasked Email. Return the new Masked Email.
        $randomNewMaskedEmail = rand(1,1000000000) . '@MaskedEmailAddress.com';
        $nuthin = "";
        $maskEmailQuery = 'INSERT INTO `CBS_Email_Address_Masking` VALUES ("' . 
                                $nuthin . '","' . 
                                date("Y-m-d H:i:s") . '","' . 
                                $emailFrom . '","' .                // Masked Email Address
                                $randomNewMaskedEmail . '")';           // Unmasked Email Address
        $emailResult = $mysqli->query($maskEmailQuery) or die($mysqli->error.__LINE__);

        return $randomNewMaskedEmail;

    }

}

function pageHits($clientEmail) {

    // connect to the inmate database
    include 'includes/dbConnect.inc';

    $Query = "SELECT * FROM `CBS_Inmates` WHERE `emailAddress` = '". $clientEmail . "'";
    $result = $mysqli->query($Query) or die($mysqli->error.__LINE__);
    $numberResult = $result->fetch_assoc();

    $Query = "SELECT * FROM `CBS_Hit_Counts` WHERE `number` = '". $numberResult['number'] . "'";
    $result = $mysqli->query($Query) or die($mysqli->error.__LINE__);

    $hitCount = $result->num_rows;

    Return $hitCount;

}

function featuredClientCount($clientEmail) {

    // connect to the inmate database
    include 'includes/dbConnect.inc';

    $Query = "SELECT * FROM `CBS_Inmates` WHERE `emailAddress` = '". $clientEmail . "'";
    $result = $mysqli->query($Query) or die($mysqli->error.__LINE__);
    $numberResult = $result->fetch_assoc();

    $Query = "SELECT * FROM `CBS_Featured_Clients` WHERE `number` = '". $numberResult['number'] . "'";
    $result = $mysqli->query($Query) or die($mysqli->error.__LINE__);
    $numberResult = $result->fetch_assoc();

    $hitCount = $result->num_rows;

    Return $hitCount;

}

function randomQuote() {

    // connect to the inmate database
    include 'includes/dbConnect.inc';

    $query = "SELECT * FROM `CBS_Inspirational_Quotes` ORDER BY rand() LIMIT 1";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    $quote = $result->fetch_assoc();
    $fullQuote = '<p>&quot;' . $quote['quoteText'] . '&quot;</p><p style="margin-top:-5px;"><em>&ndash;' . $quote['quoteBy'] . '</em></p>';
    return $fullQuote;
}

function spitClientsAddress($clientEmail) {

    // connect to the inmate database
    include 'includes/dbConnect.inc';

    $addressQuery = "SELECT * FROM `CBS_Inmates` WHERE `emailAddress` = '". strtok($clientEmail,"@") . "'";
    $result = $mysqli->query($addressQuery) or die($mysqli->error.__LINE__);
    $address = $result->fetch_assoc();

    $spitAddress = '<p>' . 
        strtoupper($address['firstName']) . ' ' . 
        strtoupper($address['lastName']) . ' &nbsp;' . 
        '(' . strtoupper($address['number']) . ')';

    $spitAddress .= '<br />' . 
        $address['address1'];

    $spitAddress .= '<br />' . 
        $address['address2'];

    $spitAddress .= '<br />' . 
        $address['city'];

    $spitAddress .= ', ' .
        strtoupper($address['state']);

    $spitAddress .= ' ' . 
        $address['zip'];

    $spitAddress .= '</p>';

    return $spitAddress;
}

// This function decodes and extracts the attachments from the email
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 inmateFoundCount($mysqli, $emailAdd) {

        $inmates_Query = "SELECT * FROM `CBS_Inmates` WHERE (`inmateOrAdvertiser` = 'I') AND (`deactivateAccount` = 'N') AND (`emailAddress` = '" . $emailAdd . "') LIMIT 1";
        $inmateResult = $mysqli->query($inmates_Query) or die($mysqli->error.__LINE__);
        return $inmateResult->num_rows; // RETURN THE COUNT
}

?>

4

0 回答 0