[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";
}
?>
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
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
Autore Titolo Forum Risposte Data
K Help: problema con uno script di booking in php! PHP 0
L [PHP] Problema Script 'Not Found' PHP 4
Punix [PHP] problema script invio e-mail PHP 2
S Problema con script php-javascript PHP 2
C Problema script php PHP 2
C Problema script php PHP 1
francesco7 [Problema] esecuzione script Upload file in php PHP 0
Pi3tro [Problema]Script php PHP 1
E Problema con uno script php PHP 1
L problema invio newsletter con script proprio in php PHP 10
N problema script php mysql multi upload immagini PHP 31
D Problema script registrazione utente php mysql PHP 14
T Problema recupero dati da file [era: help script php] PHP 9
F Problema script php PHP 4
neo996sps Problema con query e script PHP PHP 4
G Problema script php PHP 0
L problema con script php PHP 6
E Problema con script ajax+php Ajax 2
B Problema invio mail con script php verso i dominii libero PHP 9
P [PHP] problema script con checkbox! PHP 0
K [php] Problema con inner join PHP 4
K [PHP] Problema con variabili concatenate. PHP 1
T ALTRO PROBLEMA CON ARRAY PHP PHP 1
D problema php mysql PHP 1
D problema php mysql PHP 1
Z Problema di sincronizzazione PAYPAL con PHP PHP 1
A Problema checkout carrello php PHP 2
G Problema caricamento tabelle MySql da PhP PHP 0
M Problema con php per calcolo costo percentuale PHP 7
O Problema Formmail in PHP su Aruba PHP 0
WebmasterFioriniAndrea Chat e php [problema] PHP 3
L [PHP] Problema con Telegram PHP 1
WebmasterFioriniAndrea [PHP] Problema che non mi fa vedere niente PHP 2
A [PHP] Problema query insert [RISOLTO] PHP 14
N [Apache] problema con estensione php Apache 0
C [PHP] Problema con download file PHP 0
M [PHP] Problema con preg_match PHP 1
gandalf1959 [PHP] problema con l'utilizzo di Header PHP 3
K [RISOLTO] Problema Griglia Php+Mysql PHP 13
M [PHP] Problema con query select PHP 2
L Problema jQuery validation AJAX (PHP 7) PHP 6
L Problema funzione mail() PHP PHP 3
S [PHP] Problema con istruzione "use" PHP 23
A Problema PHP PHP 1
T [php] problema creazione query select-where PHP 5
M [PHP] problema if PHP 3
L [PHP] Problema su codice o server PHP 5
T [PHP] problema maggiore e minore PHP 4
Cosina [PHP] fwrite problema con le parole accentate PHP 9
M [PHP] Problema search form PHP 3

Discussioni simili