function store_email_into_folder($msg, $folder = 'SentFromDolibarr')
{
    global $user, $db;
    $mailboxconfig = new Usermailboxconfig($db);
    $mailboxconfig->fetch_from_user($user->id);
    $user->mailbox_imap_login = $mailboxconfig->mailbox_imap_login;
    $user->mailbox_imap_password = $mailboxconfig->mailbox_imap_password;
    $user->mailbox_imap_host = $mailboxconfig->mailbox_imap_host;
    $user->mailbox_imap_port = $mailboxconfig->mailbox_imap_port;
    $user->mailbox_imap_ssl = $mailboxconfig->mailbox_imap_ssl;
    $user->mailbox_imap_ssl_novalidate_cert = $mailboxconfig->mailbox_imap_ssl_novalidate_cert;
    $user->mailbox_imap_ref = $mailboxconfig->get_ref();
    $user->mailbox_imap_connector_url = $mailboxconfig->get_connector_url();
    $mbox = imap_open($user->mailbox_imap_connector_url . $folder, $user->mailbox_imap_login, $user->mailbox_imap_password);
    $check = imap_check($mbox);
    $before = $check->Nmsgs;
    $result = imap_append($mbox, $user->mailbox_imap_connector_url . $folder, $msg);
    $check = imap_check($mbox);
    $after = $check->Nmsgs;
    if ($result == FALSE) {
        if (imap_createmailbox($mbox, imap_utf7_encode($user->mailbox_imap_ref . $folder))) {
            $mbox = imap_open($user->mailbox_imap_connector_url . $folder, $user->mailbox_imap_login, $user->mailbox_imap_password);
            $check = imap_check($mbox);
            $before = $check->Nmsgs;
            $result = imap_append($mbox, $user->mailbox_imap_connector_url . $folder, $msg);
            $check = imap_check($mbox);
            $after = $check->Nmsgs;
        }
    }
    imap_close($mbox);
}
// Change this following line to use the correct relative path from htdocs (do not remove DOL_DOCUMENT_ROOT)
//require_once(DOL_DOCUMENT_ROOT."/../dev/skeleton/skeleton_class.class.php");
require_once DOL_DOCUMENT_ROOT . '/core/lib/usergroups.lib.php';
require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
require_once dirname(__FILE__) . '/class/usermailboxconfig.class.php';
$id = GETPOST('id', 'int');
$action = GETPOST('action');
$langs->load("companies");
$langs->load("members");
$langs->load("bills");
$langs->load("users");
$langs->load("dolimail@dolimail");
$fuser = new User($db);
$fuser->fetch($id);
$mailboxconfig = new Usermailboxconfig($db);
$mailboxconfig->fetch_from_user($id);
// If user is not user read and no permission to read other users, we stop
if ($fuser->id != $user->id && !$user->rights->user->user->lire) {
    accessforbidden();
}
// Security check
$socid = 0;
if ($user->societe_id > 0) {
    $socid = $user->societe_id;
}
$feature2 = $socid && $user->rights->user->self->creer ? '' : 'user';
if ($user->id == $id) {
    $feature2 = '';
}
// A user can always read its own card
$result = restrictedArea($user, 'user', $id, '&user', $feature2);