PHP and IMAP

MarthaJ Sayers
MarthaJ Sayers used Ask the Experts™
on
I have a question. I am extracting data from email messages using PHP v 5.6.25 using imap.
I know I can extract the header information to determine the content type - i.e.
Content-Type: text/html or Content-Type: text/html.
But I am a bit confused on a point here. Does it return the content-type for each email ??
I know from the PHP manual that it uses the url, but this is for a specific
email address i.e. mymail@what.com.
Can you use the specific email address of the sender for the url ??


The data is contained within a table - it may or may not have html styles i.e. font-size etc. but in both cases within a table. The table structures are a bit different so I need to determine which is which for extracting the data.
Would the
The reason I am asking is the messages can be of two flavors -

1) Message has styling inbedded in table

2) Message does not have  styling inbedded in table

ANy help would be appericiated...thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
Have you been able to get the message body?  

Are both scenario's consistent in that the format is always the same? if it is you can simply look for a clue like

<table>
<tr><td>item</td></tr>
</table>

Open in new window

vs
<table>
<tr><td style="color:red;">item</td></tr>
</table>

Open in new window


If you see a td style=, then process one way vs the other.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
1) But I am a bit confused on a point here. Does it return the content-type for each email ??

Each email containing MIME parts will contain at least one Content-Type header.

Simple text messages may or may not.

2) Tip: Best to use a MIME Parser to parse all your messages. MIME Parsers are indispensable, as there are some many types of MIME messages. Far better to use an exiting MIME Parser, than try to write your own.

MIME Parsers automagickally handle your 2x additional questions.

Author

Commented:
David and Scott - thank you both for responding - you both have helped very much and I appericiate it too.
I don't have alot of exprience with this but gaining everyday. I don't have alot of experience with emails but gaining experience everyday.

David -
      Your input helped to clarify somethings I wondered about. I am going to research a MIME Parsers - never thought there being one. Thank you. And for the tip.


Scott -
Oh yes, I can get the message body in both cases. I have two function to strip out data.
One without and one with. I just thought there might be a simplier way and am still curcious
about the use of the individuals  email address etc.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
David, how does a mime parser distinguish between html with inline styles or html without inline styles?
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
If that is the case, are you able to create a function to look for "td style=" to find separate what you need?

If so, can you post some small samples of code to work with?

Author

Commented:
Scott - Oh yes, I just checked to see if the '<td>' did or did not contain 'style'.
if it did, stripped it out and continued process. Just a little function did the trick.
At that point, the structure of the plain (without style to begin with) and the stripped table have exactly
same structure so that was easy. I lucked out !
However it created a problem with inserting a date in MSSql that was working when I only had the stripped table.
Now that I implemented the plain coding - they both use the same inserting function which now has problem with inserting a date that came from plain table into a datetime column ! Worked fine with just the stripped table. Go figure..fixed one thing....broke another...sigh:)
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
fixed one thing....broke another...

Probably more like, fixed one thing.... uncovered another...   :)

It sounds like your initial issues is resolved.  For your date issue, that is something else of course. What you might want to do is just before you have your insert statement, echo out your date and then die() to see what it looks like. Make sure that you have removed any white space. Sometimes to test I will do something like, echo "|".$date."|" to help view.  As a precaution, you can $date=trim($date);  After that, validate the input. With mssql server, you can input m/d/yyyy

Author

Commented:
Good suggestions. I haven't developed programs for maybe the last two years. Just some design for mobile devices - nothing major.
 I thought the samething that whitespace might be the culpit. And I think part of it was.
I also had to use the following when inserting into my table.
I am not sure what date format they want i.e. mm/dd/yyyy - haven't got a response on that one from
client yet.  I think everything is as I need it now -
Thank you again for your suggestion. I appericiate it !

 	(CONVERT(datetime, '$wrkFixedEmailSentDate', 103)),

Open in new window

Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
I am not sure what date format they want

I think what you have should work. The date in sql server will always get stored as yyyy-mm-dd.  Of course, you can output it however you want.

Glad you have it going!

Author

Commented:
Thank you both for responding. I just thought there ight be an easier way then what I was doing.
You both were very helpful and your suggestions were informative.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
@Scott asked, "David, how does a mime parson distinguish between html with inline styles or html without inline styles?"

By magic... or rather I've looked at the code of several PERL MIME Parsers + they always make my head spin.

Lots of logic to switch between various MIME encoding approaches.

Digging into something like the PERL module MIME::Parser is a good starting point to understand how complex decoding works.
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
David, I did not mean literally how does it work as far as the internals, I meant, I don't believe the mime type can distinguish between html with inline css or external. That is my question. If you have used this before, what would you look for to use a mime parser to distinguish between html that  has inline css, css in a style tag or no css? Pseudo code?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial