Sending emails with attachments through AppleScript
Asked Answered
I

3

8

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.

Ionium answered 8/4, 2012 at 13:45 Comment(0)
C
5

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
Cassycast answered 8/4, 2012 at 18:39 Comment(4)
It worked. Thank you so much!! From what I understand, you can't use the real file, you have to use an alias right?Ionium
In my script, having the Finder get the first file of the folder results in a Finder reference (file "x" of folder "y" of disk "z"). You usually have better luck using a standard file descriptor (such as an alias) when mixing applications - in this case, I coerced the path to an alias because Mail doesn't like it if you use a Finder reference for the attachment file.Cassycast
This isn't working for me. The file is still not getting attached.Hiers
I had the same problem as Adam Moisa. But fixed it. see my answer for solution.Ora
O
2

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.

Ora answered 6/4, 2020 at 16:14 Comment(0)
S
-1

If attachments still aren't sending, activate mail.

Sanguineous answered 12/10, 2018 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.