简体   繁体   中英

python attaching mailBody into the e-mail not working

I have a below script which is working fine for rsync, this script actually pulling the files from the remote host(s) to the server from where its running under dest Folder.

However rsync works fine but the mail part basically the msg = 'message + "rsync process completed"' is not being sent as an e-mail.

Somehow i'm not to figure out what's wrong i'm doing!

import os
import sys
import subprocess
import argparse
import smtplib

#Dir Structure
dst = "/infralogs/external_dns_logs"
rsync_user = "root"
mailFrom = 'robo@helisis.com'
mailTo = 'robo@helisis.com'
mailSub = 'Rsync status'
msg = ""

parser = argparse.ArgumentParser()
parser.add_argument("-n","--hosts",dest="hosts",help="enter remote host/hosts name, comma seperated",metavar="HOSTS")
parser.add_argument("-s","--src",dest="source",help="source file/directory",metavar="SOURCE")
parser.add_argument("-e","--exclude",dest="exclude",help="Exclude files/Directories, comma seperated list",metavar="EXCLUDE")

if len(sys.argv) < 7:
    print(len(sys.argv))
    parser.print_help()
    parser.exit()

args = parser.parse_args()

def sync(host,dst):
    exclude = ""
    if not os.path.exists(dst):
        os.mkdir(dst)
    if ',' in args.exclude:
        for excl in args.exclude.split(','):
            exclude = exclude + " --exclude " + excl
        cmd = "rsync -e 'ssh -o StrictHostKeyChecking=no' -auPz %s %s@%s:%s %s/"%(exclude,rsync_user,host,args.source,dst)
    else:
        cmd = "rsync -e 'ssh -o StrictHostKeyChecking=no' -auPz --exclude %s %s@%s:%s %s/"%(args.exclude,rsync_user,host,args.source,dst)
    print(cmd)
    message = cmd
    p = subprocess.Popen(cmd,shell=True)
    p.wait()
    print("DONE")
    msg = message + "rsync process completed"

mailBody = "From: %s\nTo: %s\nSubject: %s\n\n%s" %(mailFrom,mailTo,mailSub,msg)

if ',' in args.hosts:
    for host in args.hosts.split(','):
        dest = dst + "/" + host
        sync(host,dest)
else:
    dest = dst + "/" + args.hosts
    sync(args.hosts,dest)

try:
    Mail = smtplib.SMTP('mailserver.global.helisis.com', 25, 'localhost.helisis.com')
    Mail.sendmail(mailFrom,mailTo,mailBody,msg)
    print("Mail Sent to %s" %(mailTo))
except:
    print("Mail Failed")

Result output on the terminal from above script:

 $ ./log_rsync -n remote_Server -s /var/log/infoSec/ -e "null" bind.log 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=7/9) default.log 12769 100% 12.18MB/s 0:00:00 (xfer#2, to-check=6/9) general.log 9553 100% 4.56MB/s 0:00:00 (xfer#3, to-check=5/9) lame-servers.log 663 100% 129.49kB/s 0:00:00 (xfer#4, to-check=4/9) network.log 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=3/9) notify.log 3286 100% 356.55kB/s 0:00:00 (xfer#6, to-check=2/9) queries.log 578 100% 47.04kB/s 0:00:00 (xfer#7, to-check=1/9) query-errors.log 0 100% 0.00kB/s 0:00:00 (xfer#8, to-check=0/9) sent 176 bytes received 4303 bytes 814.36 bytes/sec total size is 26849 speedup is 5.99 DONE Mail Sent to robo@helisis.com 

Mail Service is working fine, However the msg variable which contains the message msg = message + "rsync process completed" that is not getting send rest email with subject is working, Problem looks to be with msg part that not being called or opened correctly.

Just an alternative way!

While i'm applying th below method where i have included To address , Subject and Message ie Rsync Process Completed Successfully. which works and sends an e-mail but the real question again remains same if i want to include msg or say want to disclose msg part here, how could that be done!

 EmailSender="robo@localhost.helisis.com" EmailReceiver="robo@helisis.com" msgBody='''From: dnsmailer <netrobo@helisis.com> To: To Person <robo@helisis.com> Subject: rsync Status from infra-syslog Rsync Process Completed Succesfully. ''' if ',' in args.hosts: for host in args.hosts.split(','): dest = dst + "/" + host sync(host,dest) else: dest = dst + "/" + args.hosts sync(args.hosts,dest) try: smtpobj=smtplib.SMTP('mailserver.global.helisis.com', 25, 'localhost.helisis.com') smtpobj.sendmail(EmailSender,EmailReceiver,msgBody) except Exception as e: print(e) 

Could you please try sending msg using MIMEMultipart. I should work in this way.

import smtplib
from email.mime.multipart import MIMEMultipart

EmailSender = "robo@localhost.helisis.com"
EmailReceiver = "robo@helisis.com"

msgBody = '''From: dnsmailer <netrobo@helisis.com>
To: To Person <robo@helisis.com>
Subject: rsync Status from  infra-syslog

Rsync Process Completed Succesfully.
'''

try:
    Mail = smtplib.SMTP('mailserver.global.helisis.com', 25, 'localhost.helisis.com')
    mail_obj = MIMEMultipart()
    mail_obj["From"] = EmailSender
    mail_obj["To"] = EmailReceiver
    mail_obj["Subject"] = "rsync Status from  infra-syslog."
    mail_obj.preamble = "rsync Status from  infra-syslog. "
    msgBody = "Rsync Process Completed Successfully!"  # Message body
    mail_obj.attach(MIMEText(message, 'plain'))
    Mail.sendmail(from_addr=[EmailSender], to_addrs=[EmailReceiver], msg=mail_obj.as_string())
    print("Mail Sent to %s" % (EmailReceiver))
except Exception as error:
    print("Mail Failed - {}".format(error))

The problem is that msg is computed inside the sync function (which can be called in a loop) and then just forgotten (meaning not stored).

So when you want to use it in your mail body, it is no longer available. You must use it when it is available and directly aliment the mail body, or better store it in a list and then use it to build the mail body. The code could become:

...
def sync(host,dst):
    ...
    message = cmd
    p = subprocess.Popen(cmd,shell=True)
    p.wait()
    print("DONE")
    return message + " rsync process completed"  # returns the msg to the caller

msglist = []             # a list to store the messages for the mail body

if ',' in args.hosts:
    for host in args.hosts.split(','):
        dest = dst + "/" + host
        msglist.append(sync(host,dest))
else:
    dest = dst + "/" + args.hosts
    msglist.append(sync(args.hosts,dest))

msg = "\n".join(msglist)                 # combine all messages, one per line
mailBody = "From: %s\nTo: %s\nSubject: %s\n\n%s" %(mailFrom,mailTo,mailSub,msg)

try:
    Mail = smtplib.SMTP('mailserver.global.helisis.com', 25, 'localhost.helisis.com')
    Mail.sendmail(mailFrom,mailTo,mailBody,msg)
    print("Mail Sent to %s" %(mailTo))
    Mail.bye()                  # cleaner to say goodbye to server...
    Mail.close()
except:
    print("Mail Failed")

Just taking the liberty to put the e-mail piece here which worked for me as smtplib throwing some errors which i applied earlier, below is the working e-mail portion, may be someone will be looking for same :-)

Thnx much to @Serge and @Vijay helping on this..

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib


email_sender = "syslogger@helisis.com"
email_receiver = "robo@helisis.com, robo01@helisis.com, robo02@helisis.com"


try:
    Mail = smtplib.SMTP('mailserver.global.helisis.com', 25, 'localhost.helisis.com')
    mail_obj = MIMEMultipart('alternative')
    mail_obj["From"] = email_sender
    mail_obj["To"] = email_receiver
    mail_obj["Cc"] = "someone@helisis.com"
    mail_obj["Subject"] = "rsync Status from  infra-syslog."
    mail_obj.preamble = "rsync Status from  infra-syslog. "
    msgBody = "Rsync Process Completed Successfully!"  # Message body
    mail_obj.attach(MIMEText(msg, 'plain'))
    Mail.sendmail(from_addr=[email_sender], to_addrs=[email_receiver],msg=mail_obj.as_string())
    print("Mail Sent to %s" % (email_sender))
except Exception as error:
    print("Mail Failed - {}".format(error))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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