phpimap-open

How to read body content from email in php using imap


I am using imap_open() php function for reading emails from my mail account and it is working, I am able to get the header, subject, body and attachment.

But I want to get some content from body (sample body below):-

Ticket : 123456789
Special Instructions: Lorem ipsum dolor sit amet, consectetur adipiscing elit

Above is my email body content getting by imap_body($mail, $id) into html, and I want to get Ticket number 123456789 into a variable and similarly the value on the right of the : for Special Instructions into another variable.


Solution

  • As you said that you are able to fetch content, and content look like this:-

    .....
    Ticket : 123456789
    Special Instructions: Lorem ipsum dolor sit amet, consectetur adipiscing elit
    ......
    

    So to get elements separatly:-

    First explode with \n (new line)

    Now you will get an array. Now apply foreach() and explode again with :

    Note:- you need to use strpos() to get first occurrence of : and then explode string through that position (if more than one : are there in array elements)