Script sometimes move wrong mail content to DT or do nothing

OSX - El Capitan 10.11.6:

I wrote a small Applescript which is invoked thru an special subject in incoming mails (mail rules).
This script should store the mail(subject, content etc.) into a subfolder of an special DT database and move it then into a subfolder in mail.

Usually the script is running fine an worked as expected.
But sometimes it stores NOT the original mail but the next incoming mail (with different mail-subject,-content etc.)into DT and sometimes nothing is stored in DT.
Sometimes the correct mail is then moved to a subfolder, but sometimes nothing happens (mail remain in the input folder).

I’ve tried several times to debug the problem, but actually I can not postpone the situtation.

Here is the code:

property pNoSubjectString : "(no subject)"
property nameDatabase : "~/Documents/DMS/MyDatabase.dtBase2"
property nameGroup : "/Office/Subfolder1/„
property nameDestinationMailbox : „Subfolder2“
property nameDestinationMailboxMail : „Folder/SubFolder“

using terms from application "Mail"
	on perform mail action with messages theSelectedMessages for rule theRule
		tell application "Mail"
			repeat with theMessage in theSelectedMessages
				set theAccount to name of account of mailbox of theMessage
				tell theMessage
					set {theDateReceived, theDateSent, theSender, theSubject, theSource, theReadFlag} to {the date received, the date sent, the sender, the subject, the source, the read status}
				end tell
				if theSubject is equal to "" then
					set theSubject to pNoSubjectString
				end if
				tell application id "DNtp"
						set theDatabase to open database nameDatabase -- Ensure that the database is open
						set theGroup to create location nameGroup & nameDestinationMailbox in theDatabase
						set existingEntries to search theSubject & ".eml" in theGroup
						set existingRecordNumber to count of existingEntries
						if ((count of existingEntries) = 0) then
							create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string), unread:(not theReadFlag)} in theGroup
						end if
					on error error_message number error_number
						if error_number is not -128 then display alert "Mail" message error_message as warning
					end try
				end tell
				-- now move the message into another mail subfolder
				move theMessage to mailbox nameDestinationMailboxMail of account theAccount -- a folder contained by another folder
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Any help is appreciated.

The script looks fine but Mail’s scripting isn’t always very reliable in my experience. You might try to remove the “move theMessage to …” line from the repeat loop and add a second repeat loop to move the messages after all of them have been imported.

a few days ago I moved the program lines as suggested.

Today, again, the wrong mail was copied to DT and the original mail remained in the incoming mailbox (instead of being moved to a subfolder in Mail).

In the meantime I upgraded to macOS Sierra. So the problem seems to concern both OS versions

Do you move emails from the inbox of an IMAP/POP3 account?

mails fetched with fetchmail from an IMAP account at the provider and stored in a local server mail account of an Apple Server Mail 5.3.1.

Via internal network connection then these mails read from an iMac by Apple Mail 10.3 from the local mail server.

On this iMac the rule is executed to store the specified mail into DT and then move the mail into a subfolder (in Apple Mail).
And in this process, the wrong mail is often stored in DT and also not always moved to the subfolderin Apple Mail.

Do you use any third-party plug-ins for Apple Mail (in addition to the one of DEVONthink Pro Office of course)? Disabling them might be worth a try.

The only other idea coming to my mind is to contact Apple support or as this seems to be an issue of Apple Mail.