[PHP] Problema script ricezione e invio posta...

matmilan

Nuovo Utente
27 Nov 2012
17
0
1
Ciao a tutti, ho scritto questo script per leggere tutte le mail non lette e inoltrarle ma ho problemi nell'Ottenere il corpo del messaggio... Dove sbaglio? Come decodifico correttamente il corpo?

PHP:
<?php

$imap_host_name = '###';
$imap_user_name = '###';
$imap_password = '###';

/* connect to Mailer Server to Read Emails */
$inbox = imap_open($imap_host_name,$imap_user_name,$imap_password) or die('Cannot connect to IMAP: ' . imap_last_error());
//Read all the Emails
$emails = imap_search($inbox,'UNSEEN');

if($emails) {
   rsort($emails);
   foreach($emails as $email_number) {
      
       $overview = imap_fetch_overview($inbox,$email_number,0);
       $message = getBody($inbox,$email_number);
       $structure = imap_fetchstructure($inbox,$email_number);
       $header = imap_headerinfo($inbox, $email_number);
      
       $mailMittente = $header->from[0]->mailbox . "@" . $header->from[0]->host;
       $nomeMittente = $overview[0]->from;
       $mailDestinatario = "###";
       $oggetto = $overview[0]->subject;
      
       require('gestisciAllegato.php');
      
       invia($mailMittente,$nomeMittente,$mailDestinatario,$oggetto,$message,$file_attachments);
   }
}

imap_close($inbox);

function invia($mailMittente,$nomeMittente,$mailDestinatario,$oggetto,$corpoMail,$file_attachments) {
        require 'phpmailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;

        //$mail->SMTPDebug = 3;                               // Enable verbose debug output

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = '###';                                          // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '###';                 // SMTP username
        $mail->Password = '###';                           // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to

        $mail->From = $mailMittente;
        $mail->FromName = $nomeMittente;
        $mail->addAddress($mailDestinatario, $nomeMittente);     // Add a recipient
      
       // Aggiungo gli allegati se esistenti
       if(!empty($file_attachments)){
       foreach($file_attachments as $file){
           $mail->AddAttachment($file);
       }
}
      
        $mail->isHTML(true);                              // Set email format to HTML

        $mail->Subject = $oggetto;
        $mail->Body    = $corpoMail;

        if(!$mail->send()) {
                echo 'Problema nella spedizione'.'<br>';
                echo 'Errore: ' . $mail->ErrorInfo;
        } else {
                echo 'Messaggio spedito!';
        }
}

function getBody($uid, $imap) {
    $body = get_part($imap, $uid, "TEXT/HTML");
    // if HTML body is empty, try getting text body
    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";
}
?>
 
in uso con gmail, funziona
PHP:
<?php
require_once 'myUtils/show_vars.php';

require_once '_Config_MAIL.php';

SMTPservice(2);  // sceglie il servizio IMAP da cui ricevere le mail, da "_Config_MAIL.php"

$inbox = imap_open ($eM_IMAP, $eM_Username, $eM_Password);  // connect to Mailer Server to Read Emails

$emails = imap_search($inbox,'UNSEEN');  //Read all the Emails

if($emails)
{
    rsort($emails);
    echo "<h3>emails</h3>" . show_var($emails) . "<br /><br />";

    foreach($emails as $email_number)
    {
/*
(empty)    - Entire message - Root Message Part (multipart/related)
0    - Message header
1    - MULTIPART/ALTERNATIVE
1.1    - TEXT/PLAIN
1.2    - TEXT/HTML
2    - MESSAGE/RFC822 (entire attached message)
2.0    - Attached message header - The background stationary (image/gif)
2.1    - TEXT/PLAIN
2.2    - TEXT/HTML
2.3    - file.ext
*/
        $overview  = imap_fetch_overview($inbox, $email_number,0);
        $message   = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 2));
        $structure = imap_fetchstructure($inbox, $email_number);
        $header    = imap_headerinfo($inbox, $email_number);

        echo "<h3>messaggio : "   . $email_number . "</h3>";
        echo "<h3>overview</h3>"  . show_var($overview);
        echo "<h3>message</h3>"   . $message;
        echo "<h3>structure</h3>" . show_var($structure);
        echo "<h3>header</h3>"    . show_var($header);

ps, "show_vars" se ti interessa lo trovi negli snippets php
 

Discussioni simili