2017-07-30 19 views
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" 

しかし、今では電子メール本文にファイルの内容を表示する必要があります。添付ファイルではなく、電子メール本文にコンテンツを表示する必要があります。

答えて

0

それは非常にシンプルかつトリッキーです;)ちょうどこの操作を行います。

python sendml.py -f [email protected] -t [email protected] -s "test0" -b "`cat body.txt`" 
+0

はあなたの返事をいただき、ありがとうございます。私は同じことを試みましたが、出力を 'cat body.txt'としています。テキストの内容は電子メールの本文には入っていません。 –

関連する問題