1

I'm a bit new to using the IMAP Extension in PHP to check for emails. I don't fully understand all the encoding used in emails. I would like to extract plain text from Multipart messages. I don't need the pictures, formatting and other stuff - just the text.

I've tried using the following code:

$emailBody = imap_fetchbody($connection, $messageNumber, 1);
echo strip_tags($emailBody);

and then I get the following output:

------=_Part_1806898_1525547978.1599580326255 Content-Type: multipart/alternative; boundary="----=_Part_1806899_1364040934.1599580326255" ------=_Part_1806899_1364040934.1599580326255 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Welcome Onboard Hi there! A professional email address is the first step to setting up your business. Your email comes with the following cool features - 1 GB storage / mailbox - Built-in calendar - Feature rich webmail - Rich Android / iOS apps - Free trial of video calling - Free trial of read receipts to track your emails - Advanced features like rules, forwarding and auto responders Let's get you started with Flockmail. Happy emailing :) ------=_Part_1806899_1364040934.1599580326255 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Flockmail @import url(http://fonts.googleapis.com/css?family=3DOpen+Sans:800,= 700,400,500,600,300); img + div { display:none; } Hi there! A professio= nal email address is the first step to setting up your business.=20 =20 = = Your email comes with the following cool features = = = = 1GB storage/mailbox = = = = Built-in calendar = = = = Feature rich webmail client = = = = Rich Android/iOS apps = = = = Free trial of video calling = = = = Free trial of read receipts to track your emails = = = = Advanced features like rules, forwarding and = auto responders = =20 = ------=_Part_1806899_1364040934.1599580326255-- ------=_Part_1806898_1525547978.1599580326255--

I'm not sure how to get ride of all the nonsense at the beginning and at the end, and just keep the plain text of the email? I've already searched other posts, none of the solutions here: Extract body text from Email PHP are working for me.

Williamz902
  • 129
  • 5
  • 1
    I suggest you read my answer at https://stackoverflow.com/questions/37787767/fetch-imap-body-message-by-telnet/37794152#37794152 for some background reading regarding the body layout of messages. You can't assume that all messages have their body in Part 1. You have to grab metadata to learn the format and encoding of the message before you can know how to deal with it. If you have a fast connection, it's probably easier to just download it and use a fully featured MIME parser. – Max Feb 10 '21 at 16:34

1 Answers1

0

$emailBody = imap_fetchbody($connection, $messageNumber, 1);

$m= strip_tags($emailBody);

$m1 = preg_replace('/\s+/', ' ', $m);

echo strip_tags($m1);

Robot
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 03 '22 at 01:44