0

I have a .forward.postix that is piping incoming emails to a shell account, and consequently to a PHP script that parses the emails - awesome.

As of now, and based on everything I've seen online, I'm using the explode('From: ', $email); approach to split everything down into the vars I need for my database.

Enter attachments! Using the same approach, I'm doing this to pull out a jpg attachment - but it appears not every email client formats the raw source the same way and it's causing some headaches!

$contentType1 = explode('Content-type: image/jpg;', $email); //start ContentType $contentType2 = explode("--Boundary_", $contentType1[1]); //end ContentType $jpg1 = explode("\n\n", $contentType2[0]); //double return starts base64 blob $jpg2 = explode("\n\n", $jpg1[1]); //double return marks end of base64 blob $image = base64_decode($jpg2[0]); //decode base64 blob into jpg raw 

Here's my issue: Not all mail clients - like GMail for example - list an attachment as 'Content-type: image/jpg'. GMail puts in 'Content-Type: IMAGE/JPEG' and sometimes sends it as 'Content-Type: image/jpeg'! Which don't match my explode...

My Question: Does someone know a better way of finding the bounds of a base64 blob? or maybe a case-insensitive way of exploding the 'Content-Type:' so I can then try against image/jpg or image-jpeg to see what matches?

2
  • On an additional note: I'd advise you to use Regex to match your header statements instead of explode. Commented Nov 30, 2012 at 20:06
  • Have you tried using PHP builtin IMAP function? I have successfully implemented a solution with it in the past Commented Nov 30, 2012 at 20:46

1 Answer 1

1

You can try to split your string using preg_split(). Maybe something like this

$pattern = '#content-type: image/jpe?g#i'; $split_array = preg_split($pattern, $email); 
Sign up to request clarification or add additional context in comments.

4 Comments

See, i KNEW there would be a way of writing a pattern that would solve all my problems - but those patterns make NO sense to me and I cannot for the life of me find a "dictionary" of what does what. I'm guessing the e? makes the e optional, and the i at the end makes it case-insensitive? Thank you friend!
@JoelHackney Yes the ? indicates that there can be 0 or 1 or the preceding pattern component. The pattern modifier i makes the match case insensitive as you guessed.
@JoelHackney Please note my revised answer. I just noticed that I made the search for image.jpe?g (like a filename) and not image/jpe?g (content type)
Yeah i was just about to say I couldn't get the previous pattern to work - with your revision, it works perfectly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.