IMAP PHP - 电子邮件附件被捕获到邮件正文中,如何避免这种情况?

时间:2017-05-27 08:21:42

标签: php email gmail imap

我一直在开发一个IMAP-PHP电子邮件平台,但是我遇到了一个严重的问题,如果我想收到一封带有附件的电子邮件,该附件的代码会侵入邮件正文,而我找不到分开这个的方法。

除此之外,我在过滤电子邮件方面也遇到了问题,但这是我现在不打扰的另一个问题。 下面是我用来获取和发送电子邮件并将其转发到我的数据库的代码。 代码可能不适合你们,因为它使用数据库信息进行身份验证。

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
include_once("config.php");
//getting id from url
$cookieEmail = $_COOKIE['cookieEmail'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM funcionario WHERE username='$cookieEmail'") or die(mysqli_error($mysqli));

while($res = mysqli_fetch_array($result))
{
  $username = $res['username'];
	$password = $res['pass'];
}

$inbox = imap_open($hostname,$username,$password, NULL, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or die('Cannot connect to server: ' . imap_last_error());

$emails = imap_search($inbox,'UNSEEN');

if($emails) {
    $output = '';

    foreach($emails as $email_number) {
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $structure = imap_fetchstructure($inbox, $email_number);
        $header = imap_header($inbox, $email_number);
        $frome = $header->from;
        foreach ($frome as $ide => $object) {
            $fromaddress = $object->mailbox . "@" . $object->host;
        }
        if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
            $part = $structure->parts[1];
            $message = imap_fetchbody($inbox,$email_number,2);

            if($part->encoding == 3) {
                $message = imap_base64($message);
            } else if($part->encoding == 1) {
                $message = imap_8bit($message);
            } else if($part->encoding == 2) {
                $message = imap_binary($message);
            }
            else if($part->encoding == 4){
              $message = utf8_encode(quoted_printable_decode($message));
            }
            else if($part->encoding == 5)
            {
              $message = $message;
            } else {
                $message = imap_qprint($message);
            }
        }
        $from = quoted_printable_decode(imap_utf8($overview[0]->from));
        $date = utf8_decode(imap_utf8($overview[0]->date));
        $subject = quoted_printable_decode(imap_utf8($overview[0]->subject));
        $conn= mysqli_connect("localhost","root","","emails");
        $message = strip_tags($message);
        $message = html_entity_decode($message);
        $message = htmlspecialchars($message);        mysqli_query($conn, "Call InserirTickets2('$fromaddress','$from', '$subject', '$message','$cookieEmail')");
	    mysqli_close($conn);
    }

    echo $output;
}

imap_close($inbox);
?>

需要任何帮助,我真的需要这个,因为我承受着压力。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

不知道如何将其标记为已解决,但这是我的解决方案:

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
include_once("config.php");
//getting id from url
$cookieEmail = $_COOKIE['cookieEmail'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM funcionario WHERE username='$cookieEmail'") or die(mysqli_error($mysqli));

while($res = mysqli_fetch_array($result))
{
  $username = $res['username'];
	$password = $res['pass'];
}
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
//echo $inbox;
/* grab emails */
$emails = imap_search($inbox,'ALL');


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

  /* begin output var */
  $output = '';

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

  /* for every email... */
  foreach($emails as $email_number) {
    $overview = imap_fetch_overview($inbox,$email_number,0);
            $structure = imap_fetchstructure($inbox, $email_number);
            $header = imap_header($inbox, $email_number);
            $frome = $header->from;
            foreach ($frome as $ide => $object) {
                $fromaddress = $object->mailbox . "@" . $object->host;
            }
    if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
        $part = $structure->parts[0];
        $message = imap_base64(imap_fetchbody($inbox,$email_number,"1.2"));
        if(empty($message))
        {
          $message = imap_fetchbody($inbox,$email_number,1);
        }
        if($part->encoding == 3) {
            $message = imap_base64($message);
        } else if($part->encoding == 1) {
            $message = imap_8bit($message);
            echo $message;
        } else if($part->encoding == 2) {
            $message = imap_binary($message);
        }
        else if($part->encoding == 4){
          $message = utf8_encode(quoted_printable_decode($message));
        }
        else if($part->encoding == 5)
        {
          $message = $message;
        } else {
            $message = imap_qprint($message);
        }
    }
    $from = quoted_printable_decode(imap_utf8($overview[0]->from));
        $date = utf8_decode(imap_utf8($overview[0]->date));
        $subject = quoted_printable_decode(imap_utf8($overview[0]->subject));
        $message = strip_tags($message);
        $message = html_entity_decode($message);
        $message = htmlspecialchars($message);
    echo $message;
    $conn= mysqli_connect("localhost","root","","emails");
    mysqli_query($conn, "Call InserirTickets2('$fromaddress','$from', '$subject', '$message','$cookieEmail')");
	    mysqli_close($conn);
  }
}

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