Search code examples
phpgmail-imap

How to remove Junk characters coming in gmail attachments in php?


I have marked the junk characters in the image and I want the code to remove it and start reading the data after it.

enter image description here


Solution

  • That ugly looking text is not junk but something that makes a *.doc file a DOC file that it is (i.e. formatting). You can't really just echo that file using PHP.

    You can display it using a some PHP doc viewer library though or if you can find some API online to convert DOC to TXT.

    You can also make the user download it. Use file_put_content() to store that attachment into a doc file like below :

    if(file_put_content("attachment.doc", $email['attachment'])){
        header("Location: attachment.doc");
    }