使用 Applescript 在没有 mail.app 的情况下发送邮件 [英] Use Applescript to send mail without mail.app

查看:79
本文介绍了使用 Applescript 在没有 mail.app 的情况下发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Applescript 的新手,正在开发一个在后台运行的应用程序,我希望它在没有 mail.app 的情况下每隔一段时间发送电子邮件更新.

I'm new to Applescript and working on an app that runs in the background, and I want it to send email updates every so often WITHOUT mail.app.

我已经尝试过多次谷歌搜索,而 this 是我找到的最接近的,我不知道如何使用 Python 脚本.无论它安装在什么 Mac OSX 上,这个应用程序都需要工作,包括没有 Python 的计算机.这可能吗?

I've tried googling this numerous times, and while this is the closest I've found, I don't know how to use a Python script. This app needs to work no matter what Mac OSX it is installed in, including Computers without Python. Is that possible?

推荐答案

我也经常想做同样的事情而自己也很挣扎.我最终选择了 python 选项.Python 支持回 OS X 10.5,并且预装在许多较新版本的 OS X 上.是否有什么原因让您觉得不能使用它?您是否支持 OS 10.4.x 或更旧的机器?

I have often wanted to do the same thing and struggled myself. I ultimately landed on a python option. Python is supported back to OS X 10.5 and comes preinstalled on many of the newer versions of OS X. Is there some reason you don't feel you can use it? Are you supporting OS 10.4.x or older machines?

如果他们的 python 选项仍然是一个可能的选项,我已经在此处提供了一个如何使用它的示例.

If they python option is still a possible option, I've included an example of how to use it here.

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 服务器并可能添加与您提供的用户名一致的密码..

Here is the python script (just copy and past it into a text editor and save as PyMail.py. You'll need to change the smtp server and possibly add the password that goes with the username you're supplying...

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

这篇关于使用 Applescript 在没有 mail.app 的情况下发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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