I'm trying to develop a E-Mail-Provider-Script.
With the following function the E-Mails should be saved in the MySQL database directly, but my efforts to save only the TEXT / HTML section failed. What am I doing wrong( likely with imap_fetchbody() )?
function getEmailsImap($mailserver, $port, $user, $pass)
{
$imap = imap_open( "{" . $mailserver . ":" . $port . "}INBOX", $user, $pass );
$check = imap_mailboxmsginfo($imap);
$totalrows = imap_num_msg($imap);
//iterate through all unread mails
for ($index = 0; $index < $totalrows; $index++)
{
$header = imap_header($imap, $index + 1);
//get mail subject
$subject = $header->subject;
//get mail sent date
$date = date(DateTime::ISO8601 , $header->udate);
//get email authors
$email = "{$header->from[0]->mailbox}@{$header->from[0]->host}";
//get body
$body = imap_fetchbody($imap, $index+1, "1.2"); /*** I think this might be the mistake **/
//get user
$to = $header->to[0]->mailbox;
$user = explode("@", $to)[0];
$id = (int)mysql_fetch_row(mysql_query("SELECT `id` FROM `fd_emails` ORDER BY `id` DESC LIMIT 1"))[0];
$new_id = $id+1;
$sql = mysql_query("INSERT INTO `fd_emails` (`id`, `subject`, `text`, `sender`, `user`, `date`)
VALUES ('$new_id', '$subject', '$body', '$email', '$user', '$date');");
imap_delete($imap, $index + 1);
}
//close connection to mailbox
imap_expunge($imap);
imap_close($imap);
return true;
}
Many thanks in advance.