我想从电子邮件内容中获取特定数据并将其保存到mysql数据库中的相应字段。我使用imap通过我的脚本打开电子邮件帐户,但不知道如何选择特定数据并将其保存到数据库。
需要一些帮助。
这是我的代码
<?php
$server = "{imap.gmail.com:993/ssl}INBOX";
$username = "abc@gmail.com";
$password = "**********";
$mbox = imap_open("$server", "$username", "$password") or die(print_r(imap_errors()));
$num = imap_num_msg($mbox);
if($num > 0) {
$header = imap_headerinfo($mbox, 1);
$from = $header->from;
echo '<div>';
foreach($from as $id=>$object) {
//echo '<span>';
echo $fromaddress = $object->mailbox . "@" . $object->host;
}
echo $subject = $header->subject;
echo $date = $header->date;
echo '<br/>';
echo $body = imap_body($mbox, $num);
echo '</div>';
$db = mysql_connect("localhost", "root", "") or die ('Unable to connect. Check your connection parameters.');
mysql_select_db('emails', $db) or die(mysql_error($db));
$query = "INSERT INTO emails (fromaddress, subject, date, body) VALUES('$fromaddress', '$subject', '$date', '$body')";
mysql_query($query, $db) or die(mysql_error($db));
//imap_delete($mbox, 1);
imap_expunge($mbox);
header('refresh: 3');
} else {
header('refresh: 20');
}
imap_close($mbox);
?>