如何为Python错误报告获取当前用户的电子邮件地址?(How to get current users email address for Python error reporting?)

编程入门 行业动态 更新时间:2024-10-24 20:12:37
如何为Python错误报告获取当前用户的电子邮件地址?(How to get current users email address for Python error reporting?)

我们目前正在使用我刚刚在线发现的超级基本Python脚本,通过部门电子邮件地址的HTML发送错误报告。 但是,就目前而言,它会将电子邮件我们的电子邮件地址发送我们的电子邮件地址。 我希望可以通过某种方式从当前用户的地址发送电子邮件。 以下是我们得到的结果:

def sendMail(sender, recipient, subject, html, text): import MimeWriter, mimetools, cStringIO import smtplib out = cStringIO.StringIO() htmlin = cStringIO.StringIO(html) txtin = cStringIO.StringIO(text) writer = MimeWriter.MimeWriter(out) writer.addheader("From", sender) writer.addheader("To", recipient) writer.addheader("Subject", subject) writer.addheader("MIME-Version", "1.0") writer.startmultipartbody("alternative") writer.flushheaders() subpart = writer.nextpart() subpart.addheader("Content-Transfer-Encoding", "quoted-printable") pout = subpart.startbody("text/plain", [("charset", 'us-ascii')]) mimetools.encode(txtin, pout, 'quoted-printable') txtin.close() subpart = writer.nextpart() subpart.addheader("Content-Transfer-Encoding", "quoted-printable") pout = subpart.startbody("text/html", [("charset", 'us-ascii')]) mimetools.encode(htmlin, pout, 'quoted-printable') htmlin.close() writer.lastpart() msg = out.getvalue() server = smtplib.SMTP('smtp.office365.com',587) server.ehlo() server.starttls() server.ehlo() server.login("sample@email.com","sample") server.sendmail(sender, recipient, msg) server.quit()

然后我们使用一个简单的尝试/除了execfile脚本来运行一切:

try: execfile('\\\\path.py') except: print 'ATTENTION: An error has been detected in a script process.' traceback.print_exc(file=sys.stdout) import sys, cgitb recipient = ['sample@email.com','sample2@email.com','sample3@email.com','sample4@email.com'] for rec in recipient: Utils.sendMail('sample@email.com', rec, 'ATTENTION: An error has been detected in a script process...', cgitb.html(sys.exc_info()), cgitb.text(sys.exc_info())) sys.exit()

在这里我们定义了我们要发送的地址:

for rec in recipient: Utils.sendMail('sample@email.com', rec, 'ATTENTION: An error has been detected in a script process...', cgitb.html(sys.exc_info()), cgitb.text(sys.exc_info())) sys.exit()

我们可以在这里实施某种AD脚本吗? 我们已经有大约30人使用我们的流程,所以无论何时出现错误,我们所得到的都是一封包含错误信息的电子邮件,并且没有提及其所属的用户。

We're currently using a super basic Python script that I found online a while ago to send an error report via HTML from out departmental email address. However, as it currently stands, it sends the email from our email address to our email address. I'm hoping there might be some way to send the email from the address of the current user. Here's what we've got:

def sendMail(sender, recipient, subject, html, text): import MimeWriter, mimetools, cStringIO import smtplib out = cStringIO.StringIO() htmlin = cStringIO.StringIO(html) txtin = cStringIO.StringIO(text) writer = MimeWriter.MimeWriter(out) writer.addheader("From", sender) writer.addheader("To", recipient) writer.addheader("Subject", subject) writer.addheader("MIME-Version", "1.0") writer.startmultipartbody("alternative") writer.flushheaders() subpart = writer.nextpart() subpart.addheader("Content-Transfer-Encoding", "quoted-printable") pout = subpart.startbody("text/plain", [("charset", 'us-ascii')]) mimetools.encode(txtin, pout, 'quoted-printable') txtin.close() subpart = writer.nextpart() subpart.addheader("Content-Transfer-Encoding", "quoted-printable") pout = subpart.startbody("text/html", [("charset", 'us-ascii')]) mimetools.encode(htmlin, pout, 'quoted-printable') htmlin.close() writer.lastpart() msg = out.getvalue() server = smtplib.SMTP('smtp.office365.com',587) server.ehlo() server.starttls() server.ehlo() server.login("sample@email.com","sample") server.sendmail(sender, recipient, msg) server.quit()

Then we're using a simple try/except execfile script to run everything from:

try: execfile('\\\\path.py') except: print 'ATTENTION: An error has been detected in a script process.' traceback.print_exc(file=sys.stdout) import sys, cgitb recipient = ['sample@email.com','sample2@email.com','sample3@email.com','sample4@email.com'] for rec in recipient: Utils.sendMail('sample@email.com', rec, 'ATTENTION: An error has been detected in a script process...', cgitb.html(sys.exc_info()), cgitb.text(sys.exc_info())) sys.exit()

It's here where we define the address we're sending to:

for rec in recipient: Utils.sendMail('sample@email.com', rec, 'ATTENTION: An error has been detected in a script process...', cgitb.html(sys.exc_info()), cgitb.text(sys.exc_info())) sys.exit()

Is there some kind of AD script we can implement here? We've got about 30 people using our processes so whenever an error pops up all we get is an email with the error and no reference to who it belongs to.

最满意答案

在Python中使用AD并不容易,而且在没有安装任何第三方模块的情况下更是如此。

添加在电子邮件正文或主题中运行脚本的用户名是否足以代替?:

import getpass user = getpass.getuser()

然后在execfile脚本中:

Utils.sendMail( 'sample@email.com', rec, 'ATTENTION: %s has run a script which has errored...' % user, cgitb.html(sys.exc_info()), cgitb.text(sys.exc_info()) )

请注意,这不应该用于任何合法的身份验证目的,因为它不能防止用户欺骗等等。

It is not easy to work with AD in Python, and it's even tougher to do without installing any 3rd party modules.

Would adding the username that ran the script in the email body or subject suffice instead?:

import getpass user = getpass.getuser()

Then in the execfile script:

Utils.sendMail( 'sample@email.com', rec, 'ATTENTION: %s has run a script which has errored...' % user, cgitb.html(sys.exc_info()), cgitb.text(sys.exc_info()) )

It is important to note that this should not be used for any legitimate authentication purposes, as it does not protect against user-spoofing and the likes..

更多推荐

本文发布于:2023-08-06 22:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误报告   何为   电子邮件地址   用户   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!