Log of sent emails to Obsidian?

Hi, I’m just getting my arms around some automation, and I would love your help.

I would like to send salient information(subject, recipients, timestamp) to my Obsidian daily note every time I send an email from Airmail.

I have an idea I could do it with applescript and Keyboard maestro, but I’m not advanced enough in my coding to do it. Does anyone else do this? Could someone write it for me???

Thank you!

This is the tricky part — I don’t think there’s a trigger for this.

You could certainly do something manually with selected messages. The workflow would then be:

  1. Send an email
  2. Select the conversation
  3. Manually run the automation

The automation would then:

  • Take in the subject/recipient/link to the conversation/etc., and
  • add it to your notes in whatever fashion you desire

Thanks, Ryan! What about every time something adds to the sent mail folder? Or these are from the apple script dictionary for Airmail:

sendmessage v : send message

sendmessage [outgoing message] : outgoing message
outgoing message n
elements
contains [to recipients], [cc recipients], [bcc recipients], [mail attachments]; contained by [application]

id (text, r/o) : The unique identifier.
name (text) : the item’s name
subject (text) : Subject of the email
sender (text) : Sender of the message
content (text) : The content of the message
htmlContent (text) : The HTML content of the message
signature (text) : Signature of the message
htmlSignature (text) : The HTML signature of the message
responds to
[sendmessage], [compose]

Not sure if this will work, but in the Automation section there is a way to trigger an action when you get an email from a particular sender. So I’m thinking you can CC yourself and trigger the action when you get an email from your email address.

Ah, good point. I suppose you could find some way to compose and send the message via scripting, and to do the note-logging as well. Hmm.

The trick then is, how will you compose the message…? hmmm!

Hey, I think I’m getting closer. Airmail has a rule that I can use:
If: all Outgoing
Then: Run Applescript

So, here’s what I wrote, and it checks out, except it doesn’t send anything to the clipboard. Help??
Thank you!!!

on run {}
using terms from application “Airmail”
tell application “Airmail”
try
set selectedMessageUrl to (message URL) as Unicode text
end try

		try
			set subject to (subject of this_message) as Unicode text
			if this_subject is "" then error
		on error
			set this_subject to "No Subject"
		end try
		
		try
			set to_recipient to (recipient) as Unicode text
			if to_recipient is "" then error
		on error
			set to_recipient to "No Recipient"
		end try
		
		try
			set cc_recipient to (cc recipient) as Unicode text
			if cc_recipient is "" then error
		on error
			set to_recipient to "No CC Recipient"
		end try
		
		try
			set bcc_recipient to (bcc recipient) as Unicode text
			if bcc_recipient is "" then error
		on error
			set to_recipient to "No BCC Recipient"
		end try
		
	end tell
end using terms from
set selection to the clipboard

end run

I see a few issues.

  1. You have a bracket after your on run — that shouldn’t be there.
  2. In a few places, you’ve declared a variable using a reserved keyword (i.e. subject, selectedMessageURL, and selected). Make sure you keep your variables unique.
  3. When you set selection at the end—what is selection? It may be an object (i.e., it can’t be coerced straight into text). It would be better to get the data you want from the selected message and make that into a string, then set the clipboard to that string.
  4. You re-use to_recipient a few times instead of e.g., cc_recipient. I suspect this was a copy-and-paste error.
  5. You were setting selection to the clipboard when it should be set the clipboard to selection.

However, there’re some bigger challenges going on here…

I tried to fix these issues and then share the resulting script. Unfortunately, Airmail’s deceptively complicated.

For instance, you can’t just assume you only have one to recipient. You need to actually get to recipients and then iterate through the list, getting the name of eachToRecipient and address of eachToRecipient in turn.

That’s where I got stuck. Airmail’s returning something odd for each of these functions—I can’t seem to get a good name or address from a recipient.

Here’s what I was trying. It was starting to take up too much of my time, so I’m afraid I have to stop for now.

on run
	tell application "Airmail"
		try
			set theMessageURL to (message URL) as text
		end try
		
		try
			set theMessageSubject to (subject of selected message) as text
			if this_subject is "" then error
		on error
			set this_subject to "No Subject"
		end try
		set theMessage to selected message
		
		set theToRecipients to get theMessage's to recipients
		set theCCRecipients to theMessage's cc recipients
		set theBCCRecipients to theMessage's bcc recipients
		
		if theToRecipients is not equal to {} then -- if theToRecipients variable is not an empty list, there must be To recipients
			set toRecipientListAsText to "" -- initialize the toRecipientListAsText variable as blank
			repeat with eachRecipient in theToRecipients -- iterate through the list of to recipients
				set theRecipientAddressAndName to (("[" & the name of eachRecipient) & "](sendto:" & address of eachRecipient) & ")" -- take the name and address and make it a sendto link in markdown format
-- ⚠️ The above line errors
				if toRecipientListAsText is "" then -- this is the first recipient, so don't start with a separator
					set toRecipientListAsText to theRecipientAddressAndName
				else -- this is not the first recipient in the list, so add a separator
					set toRecipientListAsText to toRecipientListAsText & ", " & theRecipientAddressAndName
				end if
			end repeat
		else
			set toRecipientListAsText to "No To: recipients"
		end if
		
		-- repeat the pattern above for cc, bcc.
		
	end tell
	set resultingText to "[" & theMessageSubject & "](" & theMessageURL & ")" & "; To: " & toRecipientListAsText -- add cc recipients, bcc recipients variables.
	set the clipboard to resultingText
end run

Thank you so much for this. I appreciate your help and your time.

For sure! It’s too bad Airmail’s so frustrating sometimes. It’s still one of the most powerful options available.

Of course, if you’re willing to adapt your hopes and dreams, you can get something similar working pretty easily:

on run
	tell application "Airmail"
		set theMessage to selected message
		set theSubject to selected message's subject
		set theLink to selectedMessageUrl
		
		set messageTaskLine to "- [>] Waiting for a response to '[" & theSubject & "](" & theLink & ")'"
		set the clipboard to messageTaskLine
		
	end tell
end run

This’ll turn a selected message into a line like this…
- [>] Waiting for a response to '[Some Email Conversation Subject](airmail://message?some-link-to-the-conversation)'
…which renders into…

…which in Obsidian with the Minimal theme (and my custom colour palette) will look like…

Screen Shot 2022-05-25 at 12.27.28 PM

1 Like

Hey, Ryan,

I decided it might be easier in apple mail, and with a new os update, I’m able to use it again (long story). Would you mind taking a look and offering suggestions? Right now, the compiler seems happy, but nothing is sent to the clipboard.

Thanks again!
using terms from application “Mail”
on run
tell application “Mail”
set theSubject to subject
set theLink to MessageUrl
set recipientName to recipient_name
set ccrecipientName to cc_recipient
set bccrecipientName to bcc_recipient

		if msg_attr's this_subject is "" then ¬
			set msg_attr's this_subject to "No subject"
		
		if msg_attr's this_recipient is "" then ¬
			set msg_attr's this_recipient to "No recipient"
		
		if msg_attr's this_ccrecipient is "" then ¬
			set msg_attr's this_ccrecipient to "No cc recipient"
		
		if msg_attr's this_bccrecipient is "" then ¬
			set msg_attr's this_bccrecipient to "No bcc recipient"
		
	end tell
	
	set SentMail to "- Mail sent to '(recipientName)(ccrecipientName)(bccrecipientName)[" & theSubject & "](" & theLink & ")'"
	set the clipboard to SentMail
	
end run

end using terms from

It’s hard for me to test this as I don’t have Mail set up, but I bet you can debug whatever it is pretty easily.

The best approach for debugging something like this is to build backwards. Start with a simple script that outputs just the end result:

on run
    tell application "Mail"
        set the clipboard to "Some subject line"
    end tell
end run

Then, if that works, add in the preceding bit of code — single variable, for instance — and test again.

on run
    tell application "Mail"
        set messageSubject to subject
        set the clipboard to messageSubject
    end tell
end run

When in doubt, use log and display dialog invocations to explore your script as it executes.

on run
    tell application "Mail"
        set messageSubject to subject
        display dialog "The test email subject line is: " & messageSubject -- This will interrupt execution to show what the script is retrieving for 'subject'
        log "The test email subject line is: " & messageSubject -- This will save data to Script Editor's log, without interrupting execution
        set the clipboard to messageSubject
    end tell
end run

The abstract takeaway is to build incrementally and test as you go. That way you’re solving specific problems when debugging, and you always know when you’ve added code that introduced a bug.

Hope this helps!