Softaken IMAP Attachments Extractor Software is the only solution. Results from the Extract IMAP Server Attachments App are always 100 % accurate. IMAP attachment save allows you to extract all of the files you choose. This utility can export attachments from a variety of IMAP accounts, including Gmail, Office 365, G Suite, AOL, Hotmail, and Yahoo, as well as export multiple IMAP accounts. Once you've completed the simple 3–4 steps, you can use the software with ease. All Windows OS versions are supported by this software. Users can try out all of the software's features for free by installing the software's demo edition.
Sep 6, 2021
2
Level 2
Save an IMAP attachment to the file system
Good evening,
I am trying to do the following in a method: From an IMAP mailbox I read all mails and create an entry in a database (are tasks) from their content. This works wonderfully. Now I would like to save the attachments in the mail on the file system and note them in the DB, for this I currently do the following:
$mbox = imap_open("{" . env("IMAP_HOST") . ":" . env("IMAP_PORT") . "/imap/ssl}", env("IMAP_USER"), env("IMAP_PASS"));
$MC = imap_check($mbox);
$result = imap_fetch_overview($mbox, "1:{$MC->Nmsgs}", 0);
foreach ($result as $overview) {
$structure = imap_fetchstructure($mbox, $overview->msgno);
$header = imap_header($mbox, $overview->msgno);
$date = date('Y-m-d', $header->udate);
.......
$attachments = array();
foreach ($structure->parts as $i => $s) {
if (isset($s->disposition)) {
if ($s->disposition == "attachment" && $s->subtype != 'PGP-SIGNATURE') {
foreach ($s->dparameters as $files) {
if ($files->attribute == 'filename') {
$attachment = array('filename' => $files->value);
$attachment['type'] = strtoupper($s->subtype);
$attachment['content'] = imap_fetchbody($mbox, $overview->msgno, $i + 1);
$attachments[] = $attachment;
}
}
}
}
}
This loop reads out all attachments accordingly and writes them into an array. With the following part I want to save the file on the server and leave an entry in the database accordingly.
$runner = 0;
foreach ($attachments as $a) {
if ($runner < 4) {
$runner++;
$fileModel = new File;
$fileName = time() . '_' . uniqid() . '_' . $a['filename'];
!!! $filePath = $request->file('file')->storeAs('assets', $fileName, 'local');!!!
$fileModel->name = time() . '_' . uniqid() . '_' . $a['filename'];
$fileModel->file_path = '/storage/' . $filePath;
$fileModel->customname = $a['filename'];
$fileModel->save();
$fileId = $fileModel->id();
$salestask = Salestask::find($taskid);
$salestask->files()->attach($fileId);
}
}
imap_delete($mbox, $overview->msgno);
}
imap_expunge($mbox);
}
It's about the part with the exclamation marks in native PHP I would do that:
file_put_contents('/path/to/folder/' . $a['filename'], base64_decode($a['content']));
However, what is the best way to implement this in Laravel?
Please or to participate in this conversation.