Page 1 of 1

Automatically email new files in attachment fails

PostPosted: Wed Nov 20, 2024 6:48 am
by Wout
Hi

I'm new to Hazel and currently in a trial period on the verge of buying this product. I want to use it for automatically sending invoices on iCloud to my accounting software by sending new files on a folder to an email recipient so that it uploads to the accounting package.

I created this action but it ends up in sending the email correctly with the sender/recipient/subject and content but the file itself that is the trigger of the action is not uploaded/attached.

Can someone help me out on this?

Kind regards

Wout

Script:

-- Convert Hazel's provided file path to a file reference for Mail
set theAttachment1 to (POSIX file theFile)

-- Define the email subject and body
set subject_ to "Nieuw Aankoopfactuur"
set the_content to "Er is een nieuw aankoopfactuur geüpload. Zie bijlage."

-- Create and send the email
tell application "Mail"
-- Create a new outgoing message
set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return, sender:"test@account.com"}

tell newMessage
-- Add recipient
make new to recipient at end of to recipients with properties {address:"recipient@mail.be"}

-- Attach the file
try
make new attachment with properties {file name:theAttachment1} at after the last paragraph
on error errMsg
display dialog "Error attaching file: " & errMsg
end try

-- Set visibility to false for silent sending
set visible to false

-- Send the email
send
end tell
end tell

Re: Automatically email new files in attachment fails

PostPosted: Wed Nov 20, 2024 9:48 am
by Mr_Noodle
First off, please do not post questions to the Tips forum. The title of the forum specifically states this. I've moved this topic to a more appropriate section.

Have you tried the script outside of Hazel?

Re: Automatically email new files in attachment fails

PostPosted: Wed Nov 20, 2024 10:06 am
by Wout
Thanks, I indeed was inside the tips section, apologies.

Yes, I just edited the script to retreive a specific file on my Downloads folder and ran in in Scripteditor manually and it does include the attachment.

Code: Select all
set theFile to "/Users/woutwerk/Downloads/creditnota_gusting_20241114_2024-0023.pdf"
set theAttachment1 to (POSIX file theFile)

set subject_ to "Nieuw Aankoopfactuur"
set the_content to "Er is een nieuw aankoopfactuur geüpload. Zie bijlage."

tell application "Mail"
   set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return, sender:"wout@xxx.be"}
   tell newMessage
      make new to recipient at end of to recipients with properties {address:"aankoop-077198edezzef8158@ccc.be"}
      make new attachment with properties {file name:theAttachment1} at after the last paragraph
      set visible to true -- Set to true if you want to preview the email
      send
   end tell
end tell

Re: Automatically email new files in attachment fails

PostPosted: Wed Nov 20, 2024 6:58 pm
by Lachlan Williams
Hi. Here's a script I created to email an attachment which works. The script uses some inputs from Hazel including the email address. Importantly,

Code: Select all
on hazelProcessFile(theFile, inputAttributes)
   
   --Set variables based in input attributes from Hazel
   set recipientEmail to item 1 of inputAttributes as string --Property Manager email address
   set recipientName to item 2 of inputAttributes as string --Property Manager first name
   set theProperty to item 3 of inputAttributes as string --Property short name (eg: '21 Jump')
   set theAddress to item 4 of inputAttributes as string --Property full address (eg: '21 Jump Street, Beverley Hills')
   
   -- Strip any enclosing double quotes from theAddress
   set theAddress to ReplaceChars(theAddress, "\"", "")
   
   -- Get the file name from the file path
   set fileInfo to info for theFile
   set fileName to name of fileInfo
   
   -- Compose email body
   set emailBody to "Hi " & recipientName & "," & return & return & ¬
      "Please find attached the latest Water Rates notice for " & theAddress & "." & return & return & ¬
      "Could you please invoice the tenants for water consumption ASAP". " & ¬
      "Thanks!" & return & return & ¬
      "Lachlan"
   
   -- Compose the email in Apple Mail
   tell application "Mail"
      set newMessage to make new outgoing message with properties {subject:"Water rates for " & theProperty, content:emailBody, visible:true}
      tell newMessage
         -- Add recipient
         make new to recipient at end of to recipients with properties {address:recipientEmail}
         
         -- Attach the file
         try
            make new attachment with properties {file name:theFile} at after the last paragraph
         on error errMsg
            display dialog "There was an error attaching the file '" & fileName & "'" & return & errMsg
         end try
      end tell
      
      -- Display the new email (not sending automatically, user can review)
      activate
   end tell
   
end hazelProcessFile

-- Helper function to replace characters in a string
on ReplaceChars(theText, searchString, replaceString)
   set AppleScript's text item delimiters to searchString
   set textItems to every text item of theText
   set AppleScript's text item delimiters to replaceString
   set theText to textItems as text
   set AppleScript's text item delimiters to ""
   return theText
end ReplaceChars

Re: Automatically email new files in attachment fails

PostPosted: Thu Nov 21, 2024 3:59 am
by Wout
Lachlan Williams wrote:Hi. Here's a script I created to email an attachment which works. The script uses some inputs from Hazel including the email address. Importantly,

Code: Select all
on hazelProcessFile(theFile, inputAttributes)
   
   --Set variables based in input attributes from Hazel
   set recipientEmail to item 1 of inputAttributes as string --Property Manager email address
   set recipientName to item 2 of inputAttributes as string --Property Manager first name
   set theProperty to item 3 of inputAttributes as string --Property short name (eg: '21 Jump')
   set theAddress to item 4 of inputAttributes as string --Property full address (eg: '21 Jump Street, Beverley Hills')
   
   -- Strip any enclosing double quotes from theAddress
   set theAddress to ReplaceChars(theAddress, "\"", "")
   
   -- Get the file name from the file path
   set fileInfo to info for theFile
   set fileName to name of fileInfo
   
   -- Compose email body
   set emailBody to "Hi " & recipientName & "," & return & return & ¬
      "Please find attached the latest Water Rates notice for " & theAddress & "." & return & return & ¬
      "Could you please invoice the tenants for water consumption ASAP". " & ¬
      "Thanks!" & return & return & ¬
      "Lachlan"
   
   -- Compose the email in Apple Mail
   tell application "Mail"
      set newMessage to make new outgoing message with properties {subject:"Water rates for " & theProperty, content:emailBody, visible:true}
      tell newMessage
         -- Add recipient
         make new to recipient at end of to recipients with properties {address:recipientEmail}
         
         -- Attach the file
         try
            make new attachment with properties {file name:theFile} at after the last paragraph
         on error errMsg
            display dialog "There was an error attaching the file '" & fileName & "'" & return & errMsg
         end try
      end tell
      
      -- Display the new email (not sending automatically, user can review)
      activate
   end tell
   
end hazelProcessFile

-- Helper function to replace characters in a string
on ReplaceChars(theText, searchString, replaceString)
   set AppleScript's text item delimiters to searchString
   set textItems to every text item of theText
   set AppleScript's text item delimiters to replaceString
   set theText to textItems as text
   set AppleScript's text item delimiters to ""
   return theText
end ReplaceChars



Hi Lachlan

Thanks a lot for trying to help out. Whenever I do this and eg add the start 'on hazelProcesFile' it mentions it expects end for some reason. I can't figure out why he can't compile the syntax...

See: https://photos.app.goo.gl/NnamF7CsSkxL58BG7

Any ideas?

Re: Automatically email new files in attachment fails

PostPosted: Fri Nov 22, 2024 9:57 am
by Mr_Noodle
I believe that script has to be in an external file and not embedded as it uses other handlers.

Re: Automatically email new files in attachment fails

PostPosted: Sun Nov 24, 2024 7:53 pm
by Lachlan Williams
Mr_Noodle wrote:I believe that script has to be in an external file and not embedded as it uses other handlers.


Yes, I have it set up as an external script.