从的Bash shell脚本发送邮件 [英] Sending mail from a Bash shell script

查看:725
本文介绍了从的Bash shell脚本发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个猛砸为Mac shell脚本,通过打开的Automator应用程序发送电子邮件通知发送出去的电子邮件在Mail.app默认的邮件帐户。该应用程序的Automator也十分脚本写到一个文本文件。这种解决方案的问题是

I am writing a Bash shell script for Mac that sends an email notification by opening an automator application that sends email out with the default mail account in Mail.app. The automator application also attaches a text file that the script has written to. The problems with this solution are


  1. 发送时它是在GUI中可见

  2. 它偷走焦点IF邮件不是当前应用程序

  3. 这是依赖于Mail.app的帐户设置是在未来有效

我想避开这些缺点,我应该直接从脚本发送邮件通过输入 SMTP 设置,地址送等直接在脚本。美中不足的是,我想没有扶持后缀部署在多台计算机(10.5和10.6),该脚本计算机。是否有可能做到这一点的脚本,以便将基地Mac和NBSP上运行; OS  X安装的10.5。和10.6?

I figure to get around those shortcomings I should send the mail directly from the script by entering SMTP settings, address to send to, etc. directly in the script. The catch is I would like to deploy this script on multiple computers (10.5 and 10.6) without enabling Postfix on the computer. Is it possible to do this in the script so it will run on a base Mac OS X install of 10.5. and 10.6?

更新:我已经找到了Sendmail的在 -bs 选项,它似乎是我所需要的,但我在如何指定设置的损耗

Update: I've found the -bs option for Sendmail which seems to be what I need, but I'm at a loss of how to specify settings.

此外,为了澄清,我想指定SMTP设置的原因是,从25端口本地主机通过邮件后缀发送出去会被大多数企业防火墙阻止,但如果我指定的服务器和备用端口我赢了T碰到了这个问题。

Also, to clarify, the reason I'd like to specify SMTP settings is that mails from localhost on port 25 sent out via Postfix would be blocked by most corporate firewalls, but if I specify the server and an alternate port I won't run into that problem.

推荐答案

因为Mac  OS  X包括Python中,考虑使用Python脚本,而不是一个Bash脚本。我没有测试发送部分,但它符合标准的例子

Since Mac OS X includes Python, consider using a Python script instead of a Bash script. I haven't tested the sending portion, but it follows the standard example.

# Settings

SMTP_SERVER = 'mail.myisp.com'
SMTP_PORT = 25
SMTP_USERNAME = 'myusername'
SMTP_PASSWORD = '$uper$ecret'
SMTP_FROM = 'sender@example.com'
SMTP_TO = 'recipient@example.com'

TEXT_FILENAME = '/script/output/my_attachment.txt'
MESSAGE = """This is the message
to be sent to the client.
"""

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

msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
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)
msg.attach(attachment)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)

# Now send the message
mailer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
# EDIT: mailer is already connected
# mailer.connect()
mailer.login(SMTP_USERNAME, SMTP_PASSWORD)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()

我希望这有助于。

I hope this helps.

这篇关于从的Bash shell脚本发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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