Help with automated mail workflow

I’m looking for help with a workflow that works with Apple Mail (IMAP account) on Mojave and preferably something similar to work on iOS. It would preferably we automatic via a mail rule, but would also work by kicking off a workflow via Shortcuts (iOS) or hotkey combo on MacOS.

When I receive an email with an invoice (pdf) attached, I want:

  • The attachment saved/imported into DT Pro
  • The attachment forwarded via email to a specific email address (i.e. x@x.com)
  • The original mail message archived

I can’t, for the life of me, figure out how to make this work. And ever since Workflow became Siri Shortcuts, it seems even more complicated to build a workflow than it was before.

Anyone doing something similar to this? Any suggestions on how to accomplish this?

Thanks!

Just wondering which mail service do you use?
AFAIK there’s no way of triggering Shortcuts upon event, but if you use a service that supports labeling, like GMail for instance, you could do this serverside which would then run automatically and only serve you with the desired result on you iOS device

I think you would want something like this (not this exactly, but customized for your use):

image

  1. This assumes DEVONthink Pro Office with its included scripts for use in Mail rules – the script in this example is “Add Messages to DEVONthink…”
  2. Make your own reply template, obviously
  3. Move the message to your own archive
  4. You’ll have to work on the If… portion of the rule, but something like this will find PDFs

image

1 Like

Oh My, thank you!

I totally didn’t think of the DT scripts. I saw applescript and moved on. DT has an “Add attachments…” script which is even more appropriate for this.

Ideally, I’d be able to forward just the attachment to the email address but I’ll settle for just forwarding the whole message to them for now.

Now I just have to figure out how to get something similar on iOS for when I’m not around a MacOS device.

Tonny - I use FastMail.

I don’t really know that much about FastMail but I see they do support rules & filtering so I’m pretty sure you’d be able to do it through their web interface as stated here

I’d look into doing it that way, to keep it as simple as possible

In an effort to make this more cross platform and expand to more variations of the invoice pdf, I started building out hazel rules and applescripts for this. This way, regardless of what platform I’m on and regardless of whether it’s an invoice attached to an email or a receipt downloaded after paying an invoice online, I just save it to my Actions folder in Dropbox and Hazel handles the rest.

I hacked together two applescripts to handle the two actions of:

  • Forward the pdf as an email attachment
  • Import to DTPO, OCR (again), tag, and delete from Actions folder

It’s been working pretty well so far, but the only problem with email script is that the content for the body of the email is white for some reason. So the text doesn’t show up in the email unless you highlight it and copy/paste out of there.

Any ideas why the default text color would be white?

Here’s the email script:

set subject_ to "Receipt (Automated)"
set the_content to "This is an automated receipt notification..."
set email_from to "me@nope.com"
set email_to to "you@nope.com"
tell application "Mail"
  set newMessage to make new outgoing message with properties {visible:true, subject:subject_, content:the_content}
  tell newMessage
    set visible to false
    set sender to email_from
    make new to recipient at end of to recipients with properties {address:email_to}
    # set color of the_content to {0, 0, 0}
    delay 1
    try
      make new attachment with properties {file name:theFile as alias} at after the last paragraph
    on error errmess
      log errmess
    end try
    delay 1
    send
  end tell
end tell

If I uncomment the line to set the color, it creates a new message with the text the correct color, but it fails to attach the file and seems to cause Hazel to run in a loop and never progress to the DTPO script.

Any thoughts?

This script works for me, should be adaptable to your needs

tell application "Mail"
	set theMessage to make new outgoing message with properties {visible:true, sender:"your email",subject:"subject", content:"Content\n\nSignature" & linefeed & linefeed}
	tell theMessage
		make new to recipient at end of to recipients with properties {name:"recipient", address:"johndoe@domain.com"}
	end tell
	tell content of theMessage
		make new attachment with properties {file name:theAttachment} at after last paragraph
	end tell
	delay 1
	send theMessage
end tell

Thanks. I had to modify it slightly, but at least it got me a little further.

Now I’m able to watch it and see that it is creating the message with the content the correct color. Then it attaches the file. Once the file gets attached to the message, the text of the body changes color.

So, no clue why it is changing colors when attaching files. That doesn’t happen when manually creating an email and attaching files.

Alright, I figured it out. It is an issue with the way Mojave’s dark mode works. Basically, if you have dark mode enabled then any rich format email message you create that includes any “images” gets converted into some screwy HTML stuff. This is what is causing mail to jack up the text only after the attachment gets added.

The fix:

  • Open Mail
  • Go to Preferences, then Viewing
  • Uncheck “Use dark backgrounds for messages”

This seems to keep Mail from jacking up the message formatting and the text stays the correct color.

Thanks for the help, though.

No problem, glad you worked it out.