2

I'm piping my incoming emails to a handler.php. I can pipe it successfully and it's working, but when it gets to getting out the variables from mail header, for example the "Subject", or "To", or the "message body", I'm experiencing some problems. Here is the code I got from here

here is the code:

 <?php
//Assumes $email contains the contents of the e-mail
//When the script is done, $subject, $to, $message, and $from all contain appropriate values

//Parse "subject"
$subject1 = explode ("\nSubject: ", $email);
$subject2 = explode ("\n", $subject1[1]);
$subject = $subject2[0];

//Parse "to"
$to1 = explode ("\nTo: ", $email);
$to2 = explode ("\n", $to1[1]);
$to = str_replace ('>', '', str_replace('<', '', $to2[0]));

$message1 = explode ("\n\n", $email);

$start = count ($message1) - 3;

if ($start < 1)
{
    $start = 1;
}

//Parse "message"
$message2 = explode ("\n\n", $message1[$start]);
$message = $message2[0];

//Parse "from"
$from1 = explode ("\nFrom: ", $email);
$from2 = explode ("\n", $from1[1]);

if(strpos ($from2[0], '<') !== false)
{
    $from3 = explode ('<', $from2[0]);
    $from4 = explode ('>', $from3[1]);
    $from = $from4[0];
}
else
{
    $from = $from2[0];
}
?> 

For Gmail emails, it gets the subject, from, to, and the message body fine, but it's not working for the incoming emails from Yahoo.

Is there any universal php class which is compatible with all famous email service providers? what if someone sends an email from RoundCube, or another email sender? How I could successfully detect the variables?

Thanks!

russellsayshi
  • 2,449
  • 5
  • 17
  • 21
behz4d
  • 1,819
  • 5
  • 35
  • 59
  • The email headers _should_ be standard. You need to have a look at emails from Yahoo and others that don't work and try to see what the difference is. It could be anything from a missing space after `Subject:` to a case-sensitivity issue. The headers should be similar enough that if you can track down a few bugs, it will work for all emails. I'm afraid I can't suggest a catch-all library – Basic Sep 25 '12 at 22:20
  • @Basic it's not that simple! for example the above code will get this text included in the email body(from Yahoo) "--1375426159-60184550-1348612182=:61766 Content-Type: text/html; charset=us-ascii", how I'm supposed to check all the email clients? how such web apps as WHMCS/Kayako support works? this should be interesting! – behz4d Sep 25 '12 at 22:36

2 Answers2

1

The message format you describe in your comments is Multi-Part Mime encoding.

There are a whole slew of things to consider - what if the email is in HTML and plaintext, has embedded images, attachments, etc, etc.

If the version of PHP you're using has been built with the MailParse extensions, they should give you a fairly simple set of tools to use.

There's also the Mime Email Parser available on Google code which I haven't used before but seems fairly simple.

Basic
  • 26,321
  • 24
  • 115
  • 201
1

If you need something quite simple, this is a starting point:

list($headers, $message) = explode("\n\n", $email);

$header = imap_rfc822_parse_headers($headers);

// You can now access
$header->from;
$header->to;
$header->subject;

The email part (even when alone) could also be parsed using imap_rfc822_parse_adrlist().

Savageman
  • 9,257
  • 6
  • 40
  • 50