Ok, I'm trying, in php, using the imap commands to read emails from a mailbox and then do something with them (doesn't matter what). I'm getting the sending host, the subject and the sender and then the body of the email. If the email is in mime type 'quoted-printable for Text/Html' then I can read it fine using imap_qprint($text). However, if is in mime type '8bit for Text/Html', I try using imap_8bit($text) and still get garbage.
This is the code
<?php
$codes = array("7bit","8bit","binary","base64","quoted-printable","other");
$stt = array("Text","Multipart","Message","Application","Audio","Image","Video","Other");
# Connect to the mail server and grab headers from the mailbox
$mail = imap_open('{remote.nh-group.co.uk:110/pop3}', 'itsupport', 'xxxx');
$headers = imap_headers($mail);
?>
<html>
<head>
<title>Reading a Mailbox including multipart emails from within
PHP</title>
</head>
<body>
<?
$pictures = 0;
$html = "";
# loop through each email
for ($n=1; $n<=count($headers); $n++) {
$header = imap_header($mail, $n);
$st = imap_fetchstructure($mail, $n);
$multi = $st->parts;
$nparts = count($multi);
if ($nparts == 0) {
$html2 .= "* SINGLE part email<br>";
} else{
$html2 .= "* MULTI part email<br>";
}
# look at the main part of the email, and subparts if they're present
for ($p=0; $p<=$nparts; $p++) {
$text =imap_fetchbody($mail,$n,$p);
if ($p == 0) {
$it = $stt[$st->type];
$is = ucfirst(strtolower($st->subtype));
$ie = $codes[$st->encoding];
} else {
$it = $stt[$multi[$p-1]->type];
$is = ucfirst(strtolower($multi[$p-1]->subtype));
$ie = $codes[$multi[$p-1]->encoding];
}
# Report on the mimetype
$mimetype = "$it/$is";
$mimeshow .= "<br /><b>Part $p ... ";
$mimeshow .= "Encoding: $ie for $mimetype</b><br />";
# decode content if it's encoded (more types to add later!)
if ($ie == "8bit") {
$body = imap_8bit($text);
}
if ($ie == "base64") {
$body = imap_base64($text);
}
if ($ie == "quoted-printable") {
$body = imap_qprint($text);
}
# If it's a .jpg image, save it (more types to add later)
if ($mimetype == "Image/Jpeg") {
$picture++;
$fho = fopen("imx/mp$picture.jpg","w");
fputs($fho,$realdata);
fclose($fho);
# And put the image in the report, limited in size
$html2 .= "<img src=/demo/imx/mp$picture.jpg width=150><br />";
}
# Add the start of the text to the message
$shorttext = substr($text,0,8000);
if (strlen($text) > 8000) $shorttext .= " ...\n";
$html2.= nl2br(htmlspecialchars($shorttext))."<br>";
}
echo $mimeshow.'<br>';
$subject=$header->subject;
echo '<br>Subject: '.$subject.'<br>';
echo '<br>Sender: '.$header->senderaddress.'<br>';
$splitheader = get_object_vars($header);
$host=$splitheader[from][0]->host;
echo '<br>Header: '.$host.'<br>';
echo $body.'<br>';
}
?>
</body>
</html>
I have two emails in the inbox. The first is encoded as singlepart 8bit and the second as multipart with quoted-printable
The results are as follows
Part 0 ... Encoding: 8bit for Text/Html
Subject: This is a test for ticket content Ticket #5916 (#5916#)
Sender: NHG & Clinicare IT & T Support
Header: nh-group.co.uk
MIME-Version: 1.0 Received: from hsmx05.antispameurope.com (83.246.65.101) by remote.nh-group.co.uk (192.168.1.19) with Microsoft SMTP Server (TLS) id 14.3.123.3; Wed, 1 Nov 2017 19:03:01 +0000 Received: from delivery.mailspamprotection.com (184.154.208.35) by mx-gate68-hz2.hornetsecurity.com; Wed, 01 Nov 2017 20:03:01 +0100 Received: from ns1.ukm17.siteground.biz ([77.104.173.217] helo=3Dukm17.siteground.biz)=09by se6.mailspamprotection.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256)=09(Exim 4.89)=09(envelope-from )=09id 1e9yHu-0004pY-L8=09for it.support@nh-group.co.uk; Wed, 01 Nov 2017 14:02:57 -0500 Received: from [127.0.0.1] (port=3D57646 helo=3Dukm17.siteground.biz)=09by ukm17.siteground.biz with smtp (Exim 4.89_3-0adfb60-XX)=09(envelope-from )=09id 1e9yHt-0003cX-9r=09for it.support@nh-group.co.uk; Wed, 01 Nov 2017 19:02:53 +0000 To: Subject: This is a test for ticket content Ticket #5916 (#5916#) X-PHP-Originating-Script: 1028:class-phpmailer.php Date: Wed, 1 Nov 2017 19:02:53 +0000 From: NHG & Clinicare IT & T Support Reply-To: NHG & Clinicare IT & T Support Message-ID: <8611c203ec1ee6173e753fe4ae3b72c0@corporate.nhg-uk.com> X-Mailer: PHPMailer 5.2.22 (https://github.com/PHPMailer/PHPMailer) X-Mailer: Awesome Support/4.3.2 Content-Type: text/html; charset=3D"utf-8" Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with a= ny abuse report X-AntiAbuse: Primary Hostname - ukm17.siteground.biz X-AntiAbuse: Original Domain - nh-group.co.uk X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - ukm17.siteground.biz X-Get-Message-Sender-Via: ukm17.siteground.biz: none X-Originating-IP: 77.104.173.217 X-SpamExperts-Domain: ukm17.siteground.biz X-SpamExperts-Username: 77.104.173.217 Authentication-Results: mailspamprotection.com; auth=3Dpass smtp.auth=3D77.= 104.173.217@ukm17.siteground.biz X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Outgoing-Evidence: Combined (0.09) X-Recommended-Action: accept X-Filter-ID: EX5BVjFpneJeBchSMxfU5sGO2s1tBb9jz5hUPxV9beEXv9krsgRhBn0ayn6qsU= c7BhTjUfSYCP0C 5wUMIAUTyTj+Ouslq06/9+FJoQ8Ic94JRiSFlfpo3YNCL6gbYMZBRNZBKQKXP9KnqRgOefBfIV= 9l 2lYoidX0QKN0Rp6WDa9d16bf9psWBhnIw8QWTHqFnHl2TRtBFGxCwNLr/WIXTmbEBG2scSEjB4= Cc a3THUkvgvpnunQOsgZPpnjjHtFttCMKZw1tYrnr7rJHHUpEAaqg2HAG29ucnwP0M3OWpRjoXRu= Wz sGtazoE/vPfv1xaxhZcKP/NtHQBPU7HC7F6FoNMf/xm4p5WihZYpa+IEJ/oLosXQp02uZERM5C= VD wD65x9537/7rZ2H1fP4wGxurimbdDoD390nzzgliUnnZRwVrO92GYpEzQCukKCDE/yxRejx+ZE= H8 3D3uT7G9vVxSoPhN0btnfLiBKgWuU86DjRdgvyO/J5YD0SxG9XoKqN48Eo6Fw5tldaeQTD7oX0= Ib JnLsGIY0M3GhDMueGnYVlMD3E51XI8hjMr6/q+SExB92w4mbOkVfblh5aYJR7+ZK4eH0+hutvA= HU mPi9Ge+4V1D7jZKXiOlXLdJnBjsNteQCLP3pDMhin+HwPDUna1dtsRiS8klEbdK8qT/SBsZL7w= 6z GAl0Dy9kR6GQ/UE9MSfeA9Gi3qOpaZFBK5mqSVIqVdOvR+fzwQc+c1DSL3/EWMOJs7x5Cx3mJJ= wS y6OTbpWyPpCPz0F+cvcALILN+oGg/Nnx0s/oQjWWHy7qx4hhxCQ7BHN3temh9QhbVJQq9MPSrP= 7L a/OGl9nRw/r+05rAIG9Wqbj6q4DLIKylFpcDvqmbspbAlPF70Oi5kWG5WrRZ6uKzfnujlezufX= KT FyKo6u83H3djlBrH/4eh02WHu233Y80OmAux3oN13+ztUznegQV1FFKCsIxMyfJ8+4W3u3gmpA= Nv 1DYnwFyGOs036orcJFgN1Xap1jpFsMB9RjtbAFxvtxLn+3ki2FSeoRDxSTwJWw42swm4bO6gac= pM pzImK0dZ8eeB0y9de0/NHY/Z5iLUIRMUDzeIb3XIhs66JoTzHLoQrA7fQYs8PbcVGX4Rd96fmx= 9N Rd596h9beN4f2wGT8e5STWXPcr9VvNyyyTnK4eETwT5QBDJUDAGkO208F/IJRA8ZF8C5AnJBxS= FD JKA40LeuppcmthdVdcHKN13hYPpnTUitS3rpEbXqWxAzJMHFAVLVMimS1MiSrJJu X-Report-Abuse-To: spam@quarantine1.mailspamprotection.com X-antispameurope-sender: tyxanhos@ukm17.siteground.biz X-antispameurope-recipient: it.support@nh-group.co.uk X-antispameurope-MSGID: 3ed646545a31a61c9d2f470be8b360b1-3afa340f6fdab52b50= c154a5acbc7065 X-antispameurope-Mailarchiv: E-Mail archived by www.antispameurope.com for:= it.support@nh-group.co.uk X-antispameurope-Mailarchivtype: inbound X-antispameurope-Virusscan: CLEAN X-antispameurope-disclaimer: This E-Mail was scanned by www.antispameurope.= com E-Mailservice on mx-gate68-hz2 with 03396F0021B X-antispameurope-date: 1509562979 X-antispameurope: INCOMING: X-antispameurope-Connect: delivery.mailspamprotection.com[184.154.208.35],T= LS=3D1;EMIG=3D0 X-antispameurope-detected-infomail: yes X-antispameurope-WC: 10:602:1:10043:0:145:0:0:0:0:0:0:7:2:1:2:3:118:122:143= :0:1:0:0:0:99:0:0:0:4:0:0:0:0:0:0:1:0:0:0:0:1 X-antispameurope-Spamstatus: CLEAN X-antispameurope-REASON: Score:-6.1 =09* 0.0 HTML_MESSAGE BODY: HTML included in message =09* 0.1 MIME_HTML_ONLY BODY: Message only has text/html MIME parts =09* -4.3 BAYES_00 BODY: Bayesian spam probability is 0 to 1% =09* [score: 0.0005] =09* -1.9 ASE_FP_2009_01 ASE 2009 negativer Score Return-Path: tyxanhos@ukm17.siteground.biz X-MS-Exchange-Organization-AuthSource: NHS-EX01.nhs.local X-MS-Exchange-Organization-AuthAs: Anonymous
Part 0 ... Encoding: 7bit for Multipart/Alternative
Part 1 ... Encoding: 7bit for Text/Plain
Part 2 ... Encoding: quoted-printable for Text/Html
Subject: test
Sender: Will Evans
Header: tyxan.com
test
--
Will Evans
m: +xxxxxxxx
w: web address
As you can see, the second email $body gives me some normal formatted text I can use. The first however gives me...well, something!
Any thoughts greatly appreciated.