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. Howeve
Solution 1:
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 bodyif',' 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")
Solution 2:
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))
Solution 3:
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))
Post a Comment for "Python Attaching Mailbody Into The E-mail Not Working"