1
我是Python新手。我有以下腳本來發送來自isilon
羣集的電子郵件。如何使用python腳本在電子郵件正文中調用文本文件數據
#!/usr/bin/env python
#
# Helper script to send mail using the Isilon libraries
#
import sys
from optparse import OptionParser
import socket
from isi.app.lib.emailer import Emailer, EmailAttachmentFromFile
# Emailer.send_email(to_addresses(list), message(string), from_address=None(string), subject=None(string),
# attachments=None(list), headers=None(list), charset="us-ascii"(string))
def main():
usage = '%prog: [-f sender] -t recipient [ -t recipient ... ] [-s subject] [-b body] [-a attachment]'
argparser = OptionParser(usage = usage, description = 'Send email from a cluser node')
argparser.add_option('-f', '--from', '--sender', dest='sender',
help="email sender (From:)")
argparser.add_option('-t', '--to', '--recipients', dest='recipients',
action = 'append', help="email recipient (To:)")
argparser.add_option('-s', '--subject', dest='subject',
help="email subject (Subject:)")
argparser.add_option('-b', '--body', dest='body',
help="email body (default stdin)")
argparser.add_option('-a', '--attachment', '--file', dest='attfiles',
action = 'append', help="attachment filename")
(options, args) = argparser.parse_args()
if options.sender is None:
fqdn = socket.getfqdn()
sender = "[email protected]%s" % fqdn
else:
sender = options.sender
if options.recipients is None:
argparser.error("Unable to send mail without at least one recipient");
sys.exit(1);
else:
recipients = options.recipients
if options.subject is None:
subject = 'No subject specified'
else:
subject = options.subject
if options.body is None:
lines = sys.stdin.readlines()
body = ''.join(lines)
else:
body = options.body
if options.attfiles is None:
atts = None
else:
atts = []
for attfile in options.attfiles:
att = EmailAttachmentFromFile(attfile)
atts.append(att)
try:
Emailer.send_email(recipients, body, sender, subject, attachments = atts)
except:
print "Error sending email."
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()
通過使用下面的命令我可以發送電子郵件。測試電子郵件成功。
python sendml.py -f [email protected] -t [email protected] -s "test0" -b "test1"
但現在我必須在電子郵件正文中顯示文件的內容。不作爲附件,內容應顯示在電子郵件正文中。
感謝您的及時回覆。我嘗試了相同的,但我得到的輸出爲'cat body.txt'。文本的內容不在電子郵件的正文中。 –