简体   繁体   English

如何使用python脚本在电子邮件正文中调用文本文件数据

[英]how to call a text file data in email body by using python script

I am new in Python. 我是Python新手。 I have the below script to send email from isilon cluster. 我有以下脚本从isilon集群发送电子邮件。

#!/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 = "root@%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()   

By using the below command i can able to send the email. 通过使用以下命令,我可以发送电子邮件。 Test email is successful. 测试电子邮件成功。

python sendml.py -f xxxx@xxxx.xx -t xxxxxxxx@xxxx.xxx -s "test0" -b "test1"

But now i have to display a content of file in email body. 但是现在我必须在电子邮件正文中显示文件的内容。 Not as an attachment, the content should be displayed in email body. 内容不应该作为附件显示在电子邮件正文中。

这非常简单和棘手;)只需执行以下操作:

python sendml.py -f xxxx@xxxx.xx -t xxxxxxxx@xxxx.xxx -s "test0" -b "`cat body.txt`"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM