Fix transactionality for attachment handling:

New prepareAttachments method makes it easier to prepare attachments and split out prepared and
already-saved attachments, before starting a transaction and committing them to the database.