简体   繁体   English

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

[英]IMAP PHP - Email attachments getting bugged into the message body, how to avoid this?

I've been developing an IMAP-PHP email platform but I have a serious problem, if I am trying to get an email that has attachments, the code of that attachment will invade the message body, and I can't find a way to separate this. 我一直在开发一个IMAP-PHP电子邮件平台,但是我遇到了一个严重的问题,如果我想收到一封带有附件的电子邮件,那个附件的代码会侵入邮件正文,而我找不到办法将此分开。

Besides that I also have a problem filtering emails properly, but that's another question that I won't bother with right now. 除此之外我还有一个问题是正确过滤电子邮件,但这是我现在不打扰的另一个问题。 Below is the code I use to fetch and email and forward it to a database of mine. 下面是我用来获取和发送电子邮件并将其转发到我的数据库的代码。 The code might not work for you guys because it's using database information to authenticate. 代码可能不适合你们,因为它使用数据库信息进行身份验证。

 <?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); ?> 

Any help would be needed, I really need this because I'm under pressure. 我需要任何帮助,因为我处于压力之下。 Thank you in advance! 先感谢您!

No idea how to mark this as solved, but here is my solution: 不知道如何将其标记为已解决,但这是我的解决方案:

 <?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); ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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