通过AppleScript的发送带有附件的电子邮件 [英] Sending emails with attachments through AppleScript

查看:2000
本文介绍了通过AppleScript的发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我希望能够与使用AppleScript附件发送电子邮件。
我有这样的老师谁经常让我们做实验室,要求我们通过电子邮件发送他们,所以我决定结合榛子和AppleScript使它更容易些。

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.

基本上当我导出文档转换为PDF我的文件夹中,榛子检测到它,并把它发送到另一个文件夹,然后调用与附件发送电子邮件所需的脚本。一旦做到这一点榛把文件放回原来的文件夹,并追加一个_sent的名称,这样就不会再次发送。 (该文件夹总是空的,除非文件只是导出为PDF格式)

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

我不得不使用自动化时,同样的问题,文件没有连接。我试了codeS,但总是得到相同的问题。电子邮件是不附加任何文件发送。这里是code:

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.

推荐答案

您在混合起来的文件名和文件路径 - 在文件名附件的属性是附加的文件。你也应该记录任何错误在你的try语句,这样您就可以看到它是什么 - 例如,你会得到一个错误尝试使用的附件而不是一个别名一个Finder参考

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

这篇关于通过AppleScript的发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆