通过AppleScript发送带有附件的电子邮件



所以我希望能够使用Applescript发送带有附件的电子邮件。我有一位老师,他经常让我们做实验室,并要求我们通过电子邮件发送,所以我决定将 hazel 和 applescript 结合起来,让这件事变得更容易。

基本上,当我将文档导出到文件夹中的pdf中时,hazel检测到它并将其发送到另一个文件夹,然后该文件夹调用发送带有附件的电子邮件所需的脚本。完成后,hazel 将文件放回原始文件夹中,并在名称后附加一个_sent,以便它不会再次发送它。(除非文件刚刚导出为 pdf,否则文件夹始终为空)

我的问题是文件没有附加到电子邮件中。(大多数情况下,我的脚本出现错误,说它没有成功运行,但这不是重点)。

我在使用自动器时遇到了同样的问题,文件没有附加。我尝试了几个代码,但总是遇到同样的问题。发送电子邮件时未附加任何文件。这是代码:

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

我在事件日志中收到的唯一消息是"缺少值"。我不明白可能缺少什么。

您混淆了文件名和文件路径 - 附件的文件名属性是要附加的文件。 您还应该在 try 语句中记录任何错误,以便您可以看到它是什么 - 例如,您将在尝试对附件使用 Finder 引用而不是别名时收到错误。

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

我尝试了 user866649 的脚本,但它对我不起作用。经过一些谷歌搜索,我发现一个小的差异起到了作用:你告诉邮件的内容添加附件。我的代码现在是:

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 "mail@example.com"
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

当然,根据需要替换用户名/文件夹/邮件地址/发件人姓名。

使用以下命令从命令行运行脚本:

osascript path/to/script

在 Mac OS Catalina 上测试。编辑:看起来邮件需要一段时间来附加文件,具体取决于大小。添加延迟似乎有助于解决问题。

如果附件仍未发送,请激活邮件。

相关内容

  • 没有找到相关文章

最新更新