I need to create an app that will extract VAT numbers that our clients send us for verification. They send nothing more with e-mails. That's for purpose of creating extended statistics.
What I need is to have a mail's body without any headers before the content I need, that is VAT number, as simple as that.
This is my script that creates the list of 30 recent e-mails:
<?
if (!function_exists('imap_open')) { die('No function'); }
if ($mbox = imap_open(<confidential>)) {
$output = "";
$messageCount = imap_num_msg($mbox);
$x = 1;
for ($i = 0; $i < 30; $i++) {
$message_id = ($messageCount - $i);
$fetch_message = imap_header($mbox, $message_id);
$mail_content = quoted_printable_decode(imap_fetchbody($mbox,$message_id, 1));
iconv(mb_detect_encoding($mail_content, mb_detect_order(), true), "UTF-8", $mail_content);
$output .= "<tr>
<td>".$x.".</td>
<td>
".$fetch_message->from[0]->mailbox."@".$fetch_message->from[0]->host."
</td>
<td>
".$fetch_message->date."
</td>
<td>
".$fetch_message->subject."
</td>
<td>
<textarea cols=\"40\">".$mail_content."</textarea>
</td>
</tr>";
$x++;
}
$smarty->assign("enquiries", $output);
$smarty->display("module_mail");
imap_close($mbox);
} else {
print_r(imap_errors());
}
?>
I've worked with imap_fetchbody, imap_header and so on to retrieve the desired content but it turns out that most of e-mails have got something else (like headers) before the content, ie.
--=-Dbl2eWTUl0Km+Tj46Ww1
Content-Type: text/plain;
------=_NextPart_001_003A_01D14F7A.F25AB3D0
Content-Type: text/plain;
--=-ucRIRGamiKb0Ot1/AkNc
Content-Type: text/plain;
I need to get rid of everything that's before the VAT number included in the mail's message but I don't know how. Some emails don't have these headers, some do. And since we're working with clients from all over the Europe, it really confuses me and leaves powerless.
Another problem is that some clients just copy-paste VAT numbers from various websites and that means these VAT numbers are often pasted with the original style (bold/background/changed colour et cetera). That might be the reason for my PS below.
I would appreciate every help that'd lead me to solving this problem.
Thank you in advance.
PS. Just for a record. With imap_fetchbody($mbox,$message_id, 1)
I need to use 1
to have the whole content. Changing 1
to anything else results in displaying NO email content at all. Literally.
instead of a space, orspan
tags etc. Is there a reason you cant just create a simple form on your company website instead, so you can control the format of the data? – Erythrocytemailto:
link? That way the users email client opens with preformed content? – Erythrocyteour VAT number: #######
with instructions to copy and paste the result. With that in place, i would try and capture the numbers from email using the above regex, and if it failed, send an autoreply asking the customer to visit the above mentioned page – Erythrocyte