[英]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. 我有一个下面的脚本,它对于rsync正常工作,该脚本实际上将文件从远程主机拉到服务器,该文件从其在
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. 但是
rsync
可以正常工作,但是邮件部分msg = 'message + "rsync process completed"'
基本上不作为电子邮件发送。
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. 邮件服务工作正常,但是包含消息
msg = message + "rsync process completed"
的msg
变量未正常发送带有主题的剩余电子邮件,该问题似乎正在起作用,问题似乎出在msg
部分未正确调用或打开的地方。
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.
当我使用下面的方法时,其中包含了
To
address, Subject
和Message即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! 可以正常工作并发送电子邮件,但如果我想包含
msg
或说要在此处公开msg
部分,那么真正的问题仍然是相同的,那怎么办!
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. 您能否尝试使用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). 问题在于,
msg
是在sync
函数内部计算的(可以在循环中调用),然后被遗忘(意味着未存储)。
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 :-) 只是随便放一点对我有用的电子邮件,因为smtplib抛出了我较早应用的一些错误,下面是有效的电子邮件部分,可能有人会在找一样的东西:-)
Thnx much to @Serge and @Vijay helping on this.. 非常感谢@Serge和@Vijay对此提供帮助。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.