在本文中,我们将为您详细介绍即使我的文件有我要查找的字符串,我仍然通过python收到错误电子邮件的相关知识,此外,我们还会提供一些关于PythonGmailAPI:如何批量下载电子邮件附件和电子邮件
在本文中,我们将为您详细介绍即使我的文件有我要查找的字符串,我仍然通过 python 收到错误电子邮件的相关知识,此外,我们还会提供一些关于Python Gmail API:如何批量下载电子邮件附件和电子邮件?、Python SQL 脚本 - 如何发送有错误的电子邮件、python – 使用Bluehost电子邮件服务器的Django电子邮件配置、Python 教程 (7)—— 一文弄懂 Python 字符串操作 (上)| 字符串查找 | 字符串分割 | 字符串拼接 | 字符串替换的有用信息。
本文目录一览:- 即使我的文件有我要查找的字符串,我仍然通过 python 收到错误电子邮件
- Python Gmail API:如何批量下载电子邮件附件和电子邮件?
- Python SQL 脚本 - 如何发送有错误的电子邮件
- python – 使用Bluehost电子邮件服务器的Django电子邮件配置
- Python 教程 (7)—— 一文弄懂 Python 字符串操作 (上)| 字符串查找 | 字符串分割 | 字符串拼接 | 字符串替换
即使我的文件有我要查找的字符串,我仍然通过 python 收到错误电子邮件
如何解决即使我的文件有我要查找的字符串,我仍然通过 python 收到错误电子邮件?
即使我的 Rout 文件的 end_str 格式完全相同,我仍然收到电子邮件警报。它根本不应该向我发送电子邮件,因为从技术上讲没有错误。为什么会发生这种情况?
import pandas as pd
import smtplib
from email.message import EmailMessage
df = pd.read_fwf(r''Service-Now-Data.Rout'',header=None)
end_str = ''#--- END ---------------------------------------------------------------------------------------------------''
cols_to_check = ["0"]
def email_alert(subject,body,to):
msg = EmailMessage()
msg.set_content(body)
msg[''subject''] = subject
msg[''to''] = to
user = "DataScienceScriptAlerts@cfi.com"
msg[''from''] = user
server = smtplib.SMTP("pmixx36.corp.hhhegroup.com",2525)
server.starttls()
#server.login(user,password)
server.send_message(msg)
server.quit()
if __name__ == ''__main__'':
for col in cols_to_check:
if not df[0].str.contains(end_str).any():
body = "There Service-Now-Data.R script in xxx had errors on the last execution" + col + "."
print(body)
email_alert("Service-Now-Data failure alert","jtl.t@cfi.com")
df的内容明明有end_str
df[0]
Out[3]:
0 R version 4.0.4 (2021-02-15) -- "Lost Library ...
1 copyright (C) 2021 The R Foundation for Statis...
2 Platform: x86_64-w64-mingw32/x64 (64-bit)
3 R is free software and comes with ABSOLUTELY N...
397 >
398 > #--- END -----------------------------------...
399 >
400 > proc.time()
401 user system elapsed
402 76.10 4.92 374.51
Name: 0,Length: 403,dtype: object
正如你所看到的,在 df 中存在 end_str 并且它仍然在下面说 false
print(df[0].str.contains(end_str))
0 False
397 False
398 False
399 False
400 False
401 False
402 False
Name: 0,dtype: bool
解决方法
我无法重现您的错误
import pandas as pd
import numpy as np
end_str = ''#--- END ---------------------------------------------------------------------------------------------------''
df = pd.DataFrame([end_str])
print(df[0].str.contains(end_str))
0 True
Name: 0,dtype: bool
这意味着您的 end_str 与文件中包含的内容不匹配
我建议在 rout 文件中仔细检查您的结束字符串,以确保没有任何中断。或者将字符串缩短为''END''
import pandas as pd
import numpy as np
end_str = ''END''
df = pd.DataFrame([''#--- END ---------------------------------------------------------------------------------------------------''])
print(df[0].str.contains(end_str))
0 True
Name: 0,dtype: bool
Python Gmail API:如何批量下载电子邮件附件和电子邮件?
如何解决Python Gmail API:如何批量下载电子邮件附件和电子邮件??
我有以下代码,用于下载 Gmail 电子邮件及其附件。它返回它的附件。
def gmailAPIDownloadAttachments(self,messageID,userID="me"):
try:
service = self.gmailAPIService
self.GLogger.info("Attempting to download attachments from messageID (" +str(messageID)+ ")")
message = self.gmailAPIGetFullMessage(messageID,userID=userID)
if message is False:
self.GLogger.error("Failed to extract message (" +str(messageID)+ ") for downloading attachments")
return False
attachmentList = list()
payload = message[''payload'']
if ''parts'' in payload:
parts = payload[''parts'']
for part in parts:
if part[''filename'']:
if ''data'' in part[''body'']:
data = part[''body''][''data'']
else:
att_id = part[''body''][''attachmentId'']
att = service.users().messages().attachments().get(userId=userID,messageId=messageID,id=att_id).execute()
data = att[''data'']
file_data = base64.urlsafe_b64decode(data.encode(''UTF-8''))
filename = part[''filename'']
extSearch = filename.find(''.'')
if extSearch == -1:
ext = ""
partFileName = filename[0:extSearch]
else:
ext = filename[extSearch+1:]
partFileName = filename[0:extSearch]
theAttachment = Attachment(filename,partFileName,ext,file_data)
attachmentList.append(theAttachment)
self.GLogger.info("Successfully downloaded attachments from messageID (" +str(messageID)+ ")")
return(attachmentList)
except:
self.GLogger.error("Encountered an error while attempting to download email attacments from messageID (" +str(messageID)+ ")")
tb = traceback.format_exc()
self.GLogger.exception(tb)
return False
我了解如何将获取消息转换为批处理。例如,这是批量获取消息的方式:
from apiclient.http import BatchHttpRequest
import json
batch = BatchHttpRequest()
#assume we got messages from Gmail query API
for message in messages:
batch.add(service.users().messages().get(userId=''me'',id=message[''id''],format=''raw''))
batch.execute()
for request_id in batch._order:
resp,content = batch._responses[request_id]
message = json.loads(content)
#handle your message here,like a regular email object