Skip to content

Commit

Permalink
Do not add paperclip to conversations when email contains only embedd…
Browse files Browse the repository at this point in the history
…ed images and do not show embedded images as attachments in conversations
  • Loading branch information
freescout-help-desk committed Oct 7, 2023
1 parent 1d79d7f commit 5420cd4
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions app/Console/Commands/FetchEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,11 @@ public function saveCustomerThread($mailbox, $message_id, $prev_thread, $from, $
$conversation->created_at = $now;
}

$prev_has_attachments = $conversation->has_attachments;
// Update has_attachments only if email has attachments AND conversation hasn't has_attachments already set
// Prevent to set has_attachments value back to 0 if the new reply doesn't have any attachment
if (!$conversation->has_attachments && count($attachments)) {
// Later we will check which attachments are embedded.
$conversation->has_attachments = true;
}

Expand Down Expand Up @@ -986,7 +988,7 @@ public function saveCustomerThread($mailbox, $message_id, $prev_thread, $from, $
$thread->has_attachments = true;

// After attachments saved to the disk we can replace cids in body (for PLAIN and HTML body)
$thread->body = $this->replaceCidsWithAttachmentUrls($thread->body, $saved_attachments);
$thread->body = $this->replaceCidsWithAttachmentUrls($thread->body, $saved_attachments, $conversation, $prev_has_attachments);
$body_changed = true;
}

Expand Down Expand Up @@ -1063,6 +1065,12 @@ public function saveUserThread($mailbox, $message_id, $prev_thread, $user, $from
break;
}

$prev_has_attachments = $conversation->has_attachments;
if (!$conversation->has_attachments && count($attachments)) {
// Later we will check which attachments are embedded.
$conversation->has_attachments = true;
}

// Save extra recipients to CC
$conv_cc = $conversation->getCcArray();
$conversation->setCc(array_merge($cc, $to));
Expand Down Expand Up @@ -1118,7 +1126,7 @@ public function saveUserThread($mailbox, $message_id, $prev_thread, $user, $from
$thread->has_attachments = true;

// After attachments saved to the disk we can replace cids in body (for PLAIN and HTML body)
$thread->body = $this->replaceCidsWithAttachmentUrls($thread->body, $saved_attachments);
$thread->body = $this->replaceCidsWithAttachmentUrls($thread->body, $saved_attachments, $conversation, $prev_has_attachments);
$body_changed = true;
}

Expand Down Expand Up @@ -1155,8 +1163,8 @@ public function saveAttachments($email_attachments, $thread_id)
$email_attachment->getMimeType(),
Attachment::typeNameToInt($email_attachment->getType()),
$email_attachment->getContent(),
'',
false,
$uploaded_file = '',
$embedded = false,
$thread_id
);
if ($created_attachment) {
Expand Down Expand Up @@ -1276,8 +1284,10 @@ public function separateReply($body, $is_html, $is_reply)
return $result;
}

public function replaceCidsWithAttachmentUrls($body, $attachments)
public function replaceCidsWithAttachmentUrls($body, $attachments, $conversation, $prev_has_attachments)
{
$only_embedded_attachments = true;

foreach ($attachments as $attachment) {
// webklex:
// [type] => image
Expand Down Expand Up @@ -1306,10 +1316,29 @@ public function replaceCidsWithAttachmentUrls($body, $attachments)
// [img_src] =>
// [size] => 2326
if ($attachment['imap_attachment']->id && (isset($attachment['imap_attachment']->img_src) || strlen($attachment['imap_attachment']->content ?? ''))) {
$body = str_replace('cid:'.$attachment['imap_attachment']->id, $attachment['attachment']->url(), $body);
$cid = 'cid:'.$attachment['imap_attachment']->id;
if (strstr($body, $cid)) {
$body = str_replace($cid, $attachment['attachment']->url(), $body);
// Set embedded flag for the attachment.
$attachment['attachment']->embedded = true;
$attachment['attachment']->save();
} else {
$only_embedded_attachments = false;
}
} else {
$only_embedded_attachments = false;
}
}

if ($only_embedded_attachments
&& $conversation
&& $conversation->has_attachments
&& !$prev_has_attachments
) {
$conversation->has_attachments = false;
$conversation->save();
}

return $body;
}

Expand Down

0 comments on commit 5420cd4

Please sign in to comment.