尝试使用 smtplib 和 email.mime.multipart 从 python 发送电子邮件,收到错误“'Series' 对象没有属性 'encode'”
Trying to send emails from python using smtplib and email.mime.multipart, getting the error "'Series' object has no attribute 'encode'"
我正在尝试制作一个采用 excel sheet 作为输入文件的 streamlit 应用程序,将其保存为工作文件,然后将邮件发送到存储的电子邮件地址。我收到此错误:
AttributeError: 'Series' 对象没有属性 'encode'
代码如下:
import streamlit as st
import pandas as pd
import numpy as np
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib,ssl
st.title("BD Triggers-Lead Generator Mail")
#uploaded_file = st.file_uploader("Choose a file")
temp_file = st.file_uploader("Enter file here!")
if temp_file:
temp_file_contents = temp_file.read()
if st.button("Save as working file"):
with open("ON_DISK_FILE.extension","wb") as file_handle:
file_handle.write(temp_file_contents)
result= st.button('Click To Send Mail')
st.write(result)
if result:
my_email= "example@gmail.com"
password= "abc@123"
server = smtplib.SMTP_SSL('smtp.gmail.com' ,465)
server.ehlo()
server.login(my_email, password)
email_list = pd.read_excel("ON_DISK_FILE.extension")
st.write(email_list)
#defining objects
names = email_list['Lead Generated']
emails = email_list['Lead generator Email']
subjects = email_list["Subject"]
ccs=email_list['CCs']
for i in range(len(emails)):
name=names[i]
email=emails[i]
subject=subjects[i]
cc=ccs[i]
msg=MIMEMultipart()
msg['Subject']=subjects
msg['From']=my_email
msg["To"]=email
msg["Cc"]=cc
text="Hi"
part1 = MIMEText(text, "plain")
msg.attach(part1)
server.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
server.close()
这是追溯:
File "c:\users\dell\anaconda3\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script
exec(code, module.__dict__)
File "C:\Users\DELL\Desktop\bdmail.py", line 81, in <module>
server.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
File "c:\users\dell\anaconda3\lib\email\message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "c:\users\dell\anaconda3\lib\email\generator.py", line 116, in flatten
self._write(msg)
File "c:\users\dell\anaconda3\lib\email\generator.py", line 195, in _write
# Write the headers. First we see if the message object wants to
File "c:\users\dell\anaconda3\lib\email\generator.py", line 222, in _write_headers
#
File "c:\users\dell\anaconda3\lib\email\_policybase.py", line 326, in fold
return self._fold(name, value, sanitize=True)
File "c:\users\dell\anaconda3\lib\email\_policybase.py", line 369, in _fold
parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
File "c:\users\dell\anaconda3\lib\site-packages\pandas\core\generic.py", line 5274, in __getattr__
我无法修复错误,有人可以帮我解决这个问题吗?
这里的错误信息是一个提示:它说你正试图在该行中处理一个 pandas Series
:
server.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
这意味着在撰写邮件时,您在某处编写了一个原始系列。罪魁祸首在这里:
msg['Subject']=subjects # subjects is the full pandas column, ie a Series
整个问题是由打字错误引起的:您可能想要:
msg['Subject']=subject
从中学到什么:
- 你已经在你跌倒的地方设置了陷阱,通过使用单数和复数形式的相同变量来避免处理索引形式(
subject
for subjects[i]
).本身没有什么不好的,但是需要非常谨慎。
- Python 错误消息通常包含很多信息 前提是您尝试理解它们:引发错误的行,通常是有关类型或内容的信息有问题的变量(这里涉及 Pandas
Series
)
我正在尝试制作一个采用 excel sheet 作为输入文件的 streamlit 应用程序,将其保存为工作文件,然后将邮件发送到存储的电子邮件地址。我收到此错误: AttributeError: 'Series' 对象没有属性 'encode'
代码如下:
import streamlit as st
import pandas as pd
import numpy as np
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib,ssl
st.title("BD Triggers-Lead Generator Mail")
#uploaded_file = st.file_uploader("Choose a file")
temp_file = st.file_uploader("Enter file here!")
if temp_file:
temp_file_contents = temp_file.read()
if st.button("Save as working file"):
with open("ON_DISK_FILE.extension","wb") as file_handle:
file_handle.write(temp_file_contents)
result= st.button('Click To Send Mail')
st.write(result)
if result:
my_email= "example@gmail.com"
password= "abc@123"
server = smtplib.SMTP_SSL('smtp.gmail.com' ,465)
server.ehlo()
server.login(my_email, password)
email_list = pd.read_excel("ON_DISK_FILE.extension")
st.write(email_list)
#defining objects
names = email_list['Lead Generated']
emails = email_list['Lead generator Email']
subjects = email_list["Subject"]
ccs=email_list['CCs']
for i in range(len(emails)):
name=names[i]
email=emails[i]
subject=subjects[i]
cc=ccs[i]
msg=MIMEMultipart()
msg['Subject']=subjects
msg['From']=my_email
msg["To"]=email
msg["Cc"]=cc
text="Hi"
part1 = MIMEText(text, "plain")
msg.attach(part1)
server.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
server.close()
这是追溯:
File "c:\users\dell\anaconda3\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script
exec(code, module.__dict__)
File "C:\Users\DELL\Desktop\bdmail.py", line 81, in <module>
server.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
File "c:\users\dell\anaconda3\lib\email\message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "c:\users\dell\anaconda3\lib\email\generator.py", line 116, in flatten
self._write(msg)
File "c:\users\dell\anaconda3\lib\email\generator.py", line 195, in _write
# Write the headers. First we see if the message object wants to
File "c:\users\dell\anaconda3\lib\email\generator.py", line 222, in _write_headers
#
File "c:\users\dell\anaconda3\lib\email\_policybase.py", line 326, in fold
return self._fold(name, value, sanitize=True)
File "c:\users\dell\anaconda3\lib\email\_policybase.py", line 369, in _fold
parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
File "c:\users\dell\anaconda3\lib\site-packages\pandas\core\generic.py", line 5274, in __getattr__
我无法修复错误,有人可以帮我解决这个问题吗?
这里的错误信息是一个提示:它说你正试图在该行中处理一个 pandas Series
:
server.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
这意味着在撰写邮件时,您在某处编写了一个原始系列。罪魁祸首在这里:
msg['Subject']=subjects # subjects is the full pandas column, ie a Series
整个问题是由打字错误引起的:您可能想要:
msg['Subject']=subject
从中学到什么:
- 你已经在你跌倒的地方设置了陷阱,通过使用单数和复数形式的相同变量来避免处理索引形式(
subject
forsubjects[i]
).本身没有什么不好的,但是需要非常谨慎。 - Python 错误消息通常包含很多信息 前提是您尝试理解它们:引发错误的行,通常是有关类型或内容的信息有问题的变量(这里涉及 Pandas
Series
)