Reputation: 133
So I wanted to be able to send an email with an attachment using applescript. I have this teacher who regularly makes us do labs and requires us to send them via email, so I decided to combine hazel and applescript to make this easier.
Basically when I export the document into a pdf in my folder, hazel detects it and sends it to another folder which then calls the script required to send the email with the attachment. Once that is done hazel puts the file back in the original folder and appends an _sent to the name so that it does not send it again. (The folder is always empty unless a file was just exported as a pdf)
My problem is that the files aren't attached to the email. (And most of the time I get an error with my script saying it didn't run successfully, but this is not the point).
I had the same problem using automator, files weren't attached. I tried several codes but always get the same problem. The email is sent with no files attached. Here is the code:
tell application "Finder"
set folderPath to folder "Macintosh HD:Users:user_directory:Desktop:Send_Mail"
set fileList to name of file 1 in folderPath
end tell
set theSubject to fileList
set theBody to "Hello sir. Here is my " & fileList
set theAddress to "Some Email"
set theAttachment to fileList
set theSender to "Some Sender"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
tell theNewMessage
set visibile to true
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress}
try
make new attachment with properties {file name:fileList} at after the last word of the last paragraph
set message_attachment to 0
log "message_attachment = " & message_attachment
on error
set message_attachment to 1
end try
#tell content
# make new attachment with properties {file name:theAttachment, path:fileList}
#end tell
#send
end tell
end tell
the Only message that I get in the event log is "missing value". I don't understand what could be missing though.
Upvotes: 9
Views: 23195
Reputation: 1359
I tried the script from user866649, but it did not work for me. After some googling I found a small difference did the trick: You tell to the CONTENT of the message to add the attachment. My code is now:
tell application "Finder"
set folderPath to folder "Macintosh HD:Users:username:folder:"
set theFile to first file in folderPath as alias
set fileName to name of theFile
end tell
set theSubject to "Filename: " & fileName
set theBody to "Filename in attachment: " & fileName
set theAddress to "[email protected]"
set theAttachment to theFile
set theSender to "Name of Sender"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
tell theNewMessage
set visibile to true
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress}
end tell
tell content of theNewMessage
try
make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
set message_attachment to 0
on error errmess -- oops
log errmess -- log the error
set message_attachment to 1
end try
log "message_attachment = " & message_attachment
end tell
delay 10
tell theNewMessage
send
end tell
end tell
Of course, replace username / folder / mail address / Name of Sender as required.
Run the script from command line with:
osascript path/to/script
Tested on Mac OS Catalina. Edit: It looks Mail is taking a while to attach the file, depending on the size. Adding the delay seemed to help in getting the problem solved.
Upvotes: 2
Reputation:
You are mixing up the file name and the file path - the file name property of the attachment is the file to attach. You should also log any errors in your try statement, so that you can see what it is - for example, you will get an error trying to use a Finder reference for the attachment instead of an alias.
tell application "Finder"
set folderPath to folder "Macintosh HD:Users:user_directory:Desktop:Send_Mail:"
set theFile to first file in folderPath as alias
set fileName to name of theFile
end tell
set theSubject to fileName
set theBody to "Hello sir. Here is my " & fileName
set theAddress to "Some Email"
set theAttachment to theFile
set theSender to "Some Sender"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
tell theNewMessage
set visibile to true
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress}
try
make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
set message_attachment to 0
on error errmess -- oops
log errmess -- log the error
set message_attachment to 1
end try
log "message_attachment = " & message_attachment
#send
end tell
end tell
Upvotes: 5