I use AppleScript to create tasks in 2Do from emails received in Mail on a MacBook Pro. As part of the creation of the task I include the message ID so that I can click on it in 2Do and it brings up the relevant email.
All good so far.
I noticed that sometimes when I clicked on the URL in the task it would say that it could not find the email. to cut a long story short I discovered that when an email is sent from an iPhone, iPad and virtually anything else then all gets created fine. However when sent from a Galaxy S6 phone (and I don’t know if it’s unique to this phone) using the native email client to me the message-id of the email is missing.
This seems to be known as something that happens though I can’t determine if it is the native app or the SMTP server that is not populating the message-id. also, I don’t know if there is anything that I can do about that end of things.
If that end can’t be sorted the only alternative is to create the message-id when I create the taskp and maybe update the existing email when it has a missing message-id. Is this possible? I don’t think that it is.
So the three questions are…
Can I get the message-id included in the email in the first place?
Can I update an existing email, using AppleScript, to include a newly created message-id?
Is there anything else that i can do if neither of these are possible?
First, check if following script returns some missing value in the resulting list.
If not, then the problem is not your server or phone, but your script.
tell application "Mail" to set messageIDs to ¬
message id of messages of mailboxes & message id of messages of mailboxes of accounts
set messageIDs to flatten(messageIDs)
on flatten(listOfLists)
script o
property fl : {}
on flttn(l)
script p
property lol : l
end script
repeat with i from 1 to (count l)
set v to item i of p's lol
if (v's class is list) then
flttn(v)
else
set end of my fl to v
end if
end repeat
end flttn
end script
tell o
flttn(listOfLists)
return its fl
end tell
end flatten
It is because default timeout for executing one command is 2 seconds. It seems, you have lot of messages (I have only 127), so you need to indicate bigger timeout:
with timeout of 600 seconds
tell application "Mail" to set messageIDs to ¬
message id of messages of mailboxes & message id of messages of mailboxes of accounts
end timeout
It’s better not to limit yourself to one mailbox of one account for now (it is possible and easy), because you may not be looking where the problem exists. It is best to review all messages first, and then identify the specific problem message.
I may not have been very clear it where I am. I can identify the messages that don’t have a message-id. (I have a script which I have copied below.) So I don’t need to find the emails… I know which ones they are.
My question is really around how can I get a message-id into the email? At source, on the way or at my end? (See my original post.)
Thank you so much for taking the time to help.
The script that I have used to identify the emails without message-id is…
tell application "Mail"
set _sel to get selection
set _links to {}
repeat with _msg in _sel
set _messageURL to "message://%3c" & _msg's message id & "%3e"
set end of _links to _messageURL
end repeat
set AppleScript's text item delimiters to return
--set the clipboard to (_links as string)
display dialog "Message URL is: " & _links
end tell
Message ID, in other words, the Internet ID is automatically generated by the program sending the outgoing message. If this not happens with your Galaxy phone, then I would consider it a bug in the email program that you use on this phone.
Now about editing the Message ID in an already received message. I tried to do it in two ways, and, thank God, I failed. I think a successful Message ID replacement would be an invaluable help to spammers on my part. There is one more way to try, but I would like not to try it.
tell application "Mail"
set _sel to get selection
set _msg to item 1 of _sel
-- set _msg's message id to "SAqS6MmmlYCCfey_PD3Q@notifications.google.com"
--> ERROR: read only property
set content of (_msg's header "message-id") to "<SAqS6MmmlYCCfey_PD3Q@notifications.google.com>" as rich text
save
return _msg's message id
--> RESULT: message id not altered by me
end tell
I would not like to do this, since I will not need it. Also, it might not work either. I meant replacing the desired section of the message source with what you need. Let me remind you that the source of the message is the rich text. But I think it is possible to easily rework some (1-2 in your case) lines of it if you wish. Since the headers are not encrypted, unlike the body of the message.
I forgot to tell you that Mail.app won’t let you edit source headers. In order to edit them, you have to bring the message to the desktop as an EML file, and then edit the headers of this file in TextEdit.app.
No, not just for fun. You probably misunderstood me. The process also describes how to return an already processed message to its original place, in the original mailbox of the Mail application.
Sorry. My sense of humour. Yes, I saw that it can replace the message back in the Inbox. My challenge will be to do this all safely using AppleScript (and not manually) as part of a larger AppleScript. I’m a bit of a beginner with AppleScript…
Well, you are already asking for a ready-made script. The process consists of 8 steps that can be automated. This is going to be a large script, so you’d better put your efforts into each step separately and ask for help with them. You will be helped if you first provide your efforts (your script, even with errors).
Since the first step is difficult for a beginner, I will help you with it. But only because I have a ready-made solution (I came across this earlier).
1. Drag and drop the message out of Mail and onto the desktop.
-- get name and source of chosen message
tell application "Mail"
set aMessage to item 1 of (get selection)
set richSource to (source of aMessage) as rich text
set theSubject to subject of aMessage
delete aMessage -- it will be deleted from mailbox!!!
end tell
-- replace every ":" symbol in theSubject with "_"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set {itemList, AppleScript's text item delimiters} to {text items of theSubject, "_"}
set {theSubject, AppleScript's text item delimiters} to {itemList as text, ATID}
-- build destination file's HFS path
set outputFile to (((path to desktop folder) as text) & theSubject & ".eml")
-- write rich text to EML file
try
set fileReference to open for access file outputFile with write permission
set eof of fileReference to 0
write richSource to fileReference
close access fileReference
on error
try
close access file outputFile
end try
end try
I didn’t mean to indicate that I was wanting someone to write me a script. Apologies if that is what came across.
I can see that ‘re-creating’ the email with a message-id is possible but will take some doing. I think that I will go another route which is not so elegant but I think will solve my specific problem. (The message-id is needed as a ‘pointer’ to the original email when a new entry is created in a ‘to do’ system. So I’m going to go the route of creating a duplicate that will then get deleted.)
I do appreciate the time and effort you have put into guiding me. Thank you for that.
-- Step 1. Drag and drop the message out of Mail and onto the desktop, Creates .eml file --KniazidisR
-- get name and source of chosen message
tell application "Mail"
set aMessage to item 1 of (get selection)
set richSource to (source of aMessage) as rich text
set theSubject to subject of aMessage
set theMailBox to mailbox of aMessage
delete aMessage -- it will be deleted from mailbox!!!
end tell
-- replace every ":" symbol in theSubject with "_"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set {itemList, AppleScript's text item delimiters} to {text items of theSubject, "_"}
set {theSubject, AppleScript's text item delimiters} to {itemList as text, ATID}
-- build destination file's HFS path
set outputFile to (((path to desktop folder) as text) & theSubject & ".eml")
-- write rich text to EML file
try
set fileReference to open for access file outputFile with write permission
set eof of fileReference to 0
write richSource to fileReference
close access fileReference
on error
try
close access file outputFile
end try
end try
----------------------------------------------------------------
-- Step 2. Right-click the EML file you just dragged onto the desktop and go to Open With > TextEdit. Open In text edit
-- Step 3 Open in TextEdit, you're free to make any changes you want
-- Modify with TextEdit
tell application "Finder"
open file outputFile using ((path to applications folder as text) & "TextEdit.app")
end tell
------------------------------------
-- Step 4. Go to File > Save to save the changes to the email file, and then close down TextEdit
-- Save File name as outputFile & "saved" & pause AS until this file is created ?? Steps
-- Or Wait for modifer key after saving message for script to continue execute
-- (https://macscripter.net/viewtopic.php?pid=114479#p114479)
-----------------------------------------
-- Step 5. Right-click the file on your desktop again and select Mail from the Open With menu
-- Step 6. Select Messages > Copy to and choose the same location you originally dragged the email from
-- Its not a pure solution, but works
tell application "Mail"
activate
open outputFile
tell application "System Events"
tell process "Mail"
click menu item "Copy to “MailBox” Again" of menu 1 of menu bar item "Message" of menu bar 1
end tell
end tell
end tell
-- Step 7. Close the message window and confirm that the edited message appears in Mail.
-- Not sure How to
---------------------------------------------
-- Step 8. delete the copy you made on the desktop
-- Delete to Trash
tell application "Finder"
delete file outputFile
end tell
--Or Delete without using Trash
(*
try
do shell script "rm " & quoted form of outputFile
end try
*)