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.
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