method insertEmail documentation in bbn\User\Email
function(array $folder, array $email)
{
if (X::hasProps($email, ['from', 'uid'])) {
$cfg = $this->class_cfg['arch']['users_emails'];
$table = $this->class_cfg['tables']['users_emails'];
$existing = $this->db->selectOne(
$table,
$cfg['id'],
[
$cfg['id_user'] => $this->user->getId(),
$cfg['msg_unique_id'] => $email['message_id'],
$cfg['msg_uid'] => $email['uid']
]
);
foreach (Mailbox::getDestFields() as $df) {
if (!empty($email[$df])) {
foreach ($email[$df] as &$dest) {
if ($id = $this->retrieveEmail($dest['email'])) {
$sent_opt = X::getField(self::getFolderTypes(), ['code' => 'sent'], 'id');
if ($sent_opt === $folder['id_option']) {
$this->addSentToLink($id, Date('Y-m-d H:i:s', strtotime($email['date'])));
}
} elseif (!($id = $this->addContactFromMail($dest))) {
throw new \Exception(X::_("Impossible to add the contact") . ' ' . $dest['email']);
}
$dest['id'] = $id;
}
if ($df === 'from') {
$id_sender = $id;
}
}
}
if (!empty($id_sender)) {
$id_parent = null;
$id_thread = null;
//die(var_dump($email));
$external = null;
if (!empty($email['in_reply_to']) || !empty($email['references'])) {
$external = [
'in_reply_to' => $email['in_reply_to'] ?? null,
'references' => $email['references'] ?? null
];
}
if ($email['priority']) {
// if Flagged dont contains none of the priority flag, add it
if (!str_contains($email['Flagged'], 'Highest')
&& !str_contains($email['Flagged'], 'High')
&& !str_contains($email['Flagged'], 'Normal')
&& !str_contains($email['Flagged'], 'Low')
&& !str_contains($email['Flagged'], 'Lowest')
) {
switch ($email['priority']) {
case 1:
$email['Flagged'] .= ' Highest';
break;
case 2:
$email['Flagged'] .= ' High';
break;
case 3:
$email['Flagged'] .= ' Normal';
break;
case 4:
$email['Flagged'] .= ' Low';
break;
case 5:
$email['Flagged'] .= ' Lowest';
break;
}
// trim the space if is in first position
if (str_starts_with($email['Flagged'], ' ')) {
$email['Flagged'] = substr($email['Flagged'], 1);
}
}
}
$ar = [
$cfg['id_user'] => $this->user->getId(),
$cfg['id_folder'] => $folder['id'],
$cfg['msg_uid'] => $email['uid'],
$cfg['msg_unique_id'] => $email['message_id'],
$cfg['date'] => date('Y-m-d H:i:s', strtotime($email['date'])),
$cfg['id_sender'] => $id_sender,
$cfg['subject'] => $email['subject'] ?: '',
$cfg['size'] => $email['Size'],
$cfg['attachments'] => empty($email['attachments']) ? null : json_encode($email['attachments']),
$cfg['flags'] => $email['Flagged'] ?: null,
$cfg['is_read'] => $email['Unseen'] ? 0 : 1,
$cfg['id_parent'] => $id_parent,
$cfg['id_thread'] => $id_thread,
$cfg['external_uids'] => $external ? json_encode($external) : null,
$cfg['excerpt'] => ""
];
$id = false;
if ($existing) {
$id = $existing;
} else if ($test = $this->db->insert($table, $ar)) {
$id = $this->db->lastId();
$mb = $this->getMailbox($folder['id_account']);
$mb->selectFolder($folder['uid']);
$number = $mb->getMsgNo($email['uid']);
if ($number) {
$msg = $mb->getMsg($number, $id, $folder['id_account']);
$text = $msg['plain'];
if (empty($text)) {
$text = $msg['html'];
}
X::log($text, 'text');
} else {
$text = "";
}
if (is_null($text)) {
$text = "";
}
// update excerpt column where id is same
$this->db->update($table, [$cfg['excerpt'] => $text], [$cfg['id'] => $id]);
foreach (Mailbox::getDestFields() as $df) {
if (in_array($df, ['to', 'cc', 'bcc']) && !empty($email[$df])) {
foreach ($email[$df] as $dest) {
if (!empty($dest['id'])) {
$this->addLinkToMail($id, $dest['id'], $df);
}
}
}
}
}
}
}
}
BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions