문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top