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

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

问题描述

我正在为发送电子邮件通知的Mac写一个 Bash shell脚本通过打开一个使用Mail.app中的默认邮件帐户发送电子邮件的自动程序应用程序。自动程序应用程序还附加脚本写入的文本文件。这个解决方案的问题是

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. 发送

  2. 在GUI中可以看到它偷取焦点如果邮件不是当前的应用程序

  3. 这取决于Mail.app的帐户设置在将来有效。

我想解决这些缺点,我应该直接从脚本发送邮件,方法是输入 SMTP 设置,发送地址等直接在脚本中。我想在多台计算机上部署此脚本(10.5和10.6),而不启用 Postfix 在电脑上可以在脚本中执行此操作,以便它可以在基本的Mac 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设置的原因是localhost上的邮件通过Postfix发送的端口25将被大多数公司防火墙阻止,但如果我指定服务器和备用端口,我将不会遇到这个问题。

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

我希望这有助于。

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

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