使用Applescript发送邮件而不使用mail.app

时间:2014-08-08 19:40:33

标签: email applescript

我是Applescript的新手,正在开发一个在后台运行的应用程序,我希望它经常发送电子邮件更新而不用mail.app。

我已经尝试了多次谷歌搜索,虽然this是我发现的最接近的,但我不知道如何使用Python脚本。这个应用程序需要工作,无论它安装在哪个Mac OSX,包括没有Python的计算机。这可能吗?

3 个答案:

答案 0 :(得分:1)

我经常想做同样的事情并且自己挣扎。我最终登陆了python选项。 Python支持OS X 10.5,并预装在许多较新版本的OS X上。您有什么理由不觉得可以使用它吗?您是否支持OS 10.4.x或更旧的机器?

如果他们的python选项仍然是一个可能的选项,我已经在这里提供了一个如何使用它的例子。

property PyMail : "/Users/TBD/Documents/Sample_Python_Email/PyMail.py"                      -- PATH TO PYTHON MAIL SCRIPT

on run
    set emailTo to "youraddress@somedomain.com" -- MULTIPLE ADDRESS SHOULD BE A COMMA     DELIMITED STRING LIKE THIS -- "address1@gmail.com, address2@hotmail.com"
    set emailFrom to "Your Name <your.name@somedomain.com>"
    set subject to "Demo Email"
    set message to "Hi user,
            I hope things are going well"
    set pathToAttchment to "/Users/TBD/Desktop/prof.jpg" -- POSIX PATH TO FILE, LEAVE AS EMPTY STRING FOR NO ATTACHMENT
    set username to "smtpusername" -- MAY OR MAY NOT BE REQUIRED IN YOUR CASE

    sendPyMail(emailTo, emailFrom, subject, message, pathToAttchment, username)

end run



on sendPyMail(emailTo, emailFrom, subject, message, attachment, username)
    try
        do shell script "python " & quoted form of PyMail & " " & quoted form of emailTo & " " & quoted form of emailFrom & " " & quoted form of subject & " " & quoted form of message & " " & quoted form of attachment & " " & quoted form of username
        return true
    on error
        return false
    end try
end sendPyMail

这是python脚本(只需复制并将其复制到文本编辑器中并保存为PyMail.py。您需要更改smtp服务器并可能添加密码,该密码与您的用户名一致。重新供应......

import sys

SMTP_TO = sys.argv[1]
SMTP_TO = SMTP_TO.split(',')
SMTP_FROM = sys.argv[2]
SUBJECT = sys.argv[3]

MESSAGE = sys.argv[4]

TEXT_FILENAME = sys.argv[5]

SMTP_USERNAME = sys.argv[6]



SMTP_SERVER = 'smtp.domainx.com'
SMTP_PORT = 25

SMTP_PASSWORD = ''


# now construct the message
import smtplib, email
from email import encoders
import os

msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)

if TEXT_FILENAME != "":
attachment = email.MIMEBase.MIMEBase('text', 'plain')
attachment.set_payload(open(TEXT_FILENAME).read())
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
encoders.encode_base64(attachment)

msg.attach(body)

if TEXT_FILENAME != "":
    msg.attach(attachment)

msg.add_header('From', SMTP_FROM)
msg.add_header('To', ';'.join(SMTP_TO))
msg.add_header('Subject', SUBJECT)

mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mailer.sendmail(SMTP_FROM, SMTP_TO, msg.as_string())
mailer.close()

答案 1 :(得分:1)

在终端

中运行此功能
echo "this is the body" | mail -s "this is the subject" "to@address"

注意:这假设您有本地安装的MTA

您的AppleScript将是:

do shell script "echo 'this is the body' | mail -s 'this is the subject' 'to@address'"

答案 2 :(得分:0)

对python一无所知,我使用TextWrangler并剪切并粘贴上面的两个脚本。 第二个在第1行崩溃,返回语法错误,如下所示:

第1行     属性PyMail:“/ Users /.../PyMail.py” - PYTHON MAIL SCRIPT的路径                   ^ SyntaxError:语法无效

胡萝卜指针位于'PyMail'中的'l'下。 我注释掉了第1行,它为第3行返回了相同的消息。