如何在不使用smtplib的情况下使用Python中嵌入的图像文件发送电子邮件?
我目前正在从事一个项目,在该项目中,我使用Python的默认库和套接字编程(mail FROM、RCPT TO、DATA等)而不是smtplib构建了一个邮件客户端。我已尝试通过套接字连接以二进制形式发送图像:如何在不使用smtplib的情况下使用Python中嵌入的图像文件发送电子邮件?,python,sockets,email,Python,Sockets,Email,我目前正在从事一个项目,在该项目中,我使用Python的默认库和套接字编程(mail FROM、RCPT TO、DATA等)而不是smtplib构建了一个邮件客户端。我已尝试通过套接字连接以二进制形式发送图像: if file_path != '': imageFile = open(file_path, 'rb') client_socket.send(imageFile.read()) 这不管用。然后,我尝试制作一条没有smtplib就可以发送的MIME消息: def cr
if file_path != '':
imageFile = open(file_path, 'rb')
client_socket.send(imageFile.read())
这不管用。然后,我尝试制作一条没有smtplib就可以发送的MIME消息:
def create_message(from_var, to_var, cc_var, subject_var, message, file_path):
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject_var
msgRoot['From'] = from_var
msgRoot['To'] = to_var
msgRoot.preamble = 'This is a multi-part message in MIME format.'
# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText(message)
msgAlternative.attach(msgText)
# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText(message + '<img src="cid:' + file_path + '">', 'html')
msgAlternative.attach(msgText)
# This example assumes the image is in the current directory
fp = open(file_path, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<' + file_path + '>')
msgRoot.attach(msgImage)
return msgRoot.as_string()
def create_message(from_var,to_var,cc_var,subject_var,message,file_path):
#创建根消息并填写发件人、收件人和主题标题
msgRoot=MIMEMultipart('related')
msgRoot['Subject']=Subject\u var
msgRoot['From']=From_var
msgRoot['To']=To_var
msgRoot.preamble='这是一条MIME格式的多部分消息。'
#将消息正文的普通版本和HTML版本封装在
#“可选”部分,以便消息代理可以决定要显示的内容。
msgAlternative=MIMEMultipart('备选方案')
msgRoot.attach(msgaltentive)
msgText=MIMEText(消息)
msgAlternative.attach(msgText)
#我们通过下面给出的ID引用IMG SRC属性中的图像
msgText=MIMEText(消息+“”,'html')
msgAlternative.attach(msgText)
#本例假定映像位于当前目录中
fp=打开(文件路径“rb”)
msgImage=MIMEImage(fp.read())
fp.close()
#如上所述定义图像的ID
msgImage.add_头('Content-ID','')
msgRoot.attach(msgImage)
返回msgRoot.as_string()
摘自。不幸的是,这也不起作用,返回此错误堆栈:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 209, in <lambda>
toVariable.get(), ccVariable.get(), subjectVariable.get(), textEntry.get('1.0', 'end-1c'), gFilePath))
File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 164, in validate_input
send_mail(mail_server, from_val, to_arr, cc_arr, subject_val, message, val_filePath)
File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 117, in send_mail
client_socket.send(create_message(from_var, to_var, cc_var, subject_var, message, file_path))
File "C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py", line 50, in create_message
return msgRoot.as_string()
File "C:\Python27\lib\email\message.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python27\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27\lib\email\generator.py", line 115, in _write
self._write_headers(msg)
File "C:\Python27\lib\email\generator.py", line 164, in _write_headers
v, maxlinelen=self._maxheaderlen, header_name=h).encode()
File "C:\Python27\lib\email\header.py", line 410, in encode
value = self._encode_chunks(newchunks, maxlinelen)
File "C:\Python27\lib\email\header.py", line 370, in _encode_chunks
_max_append(chunks, s, maxlinelen, extra)
File "C:\Python27\lib\email\quoprimime.py", line 97, in _max_append
L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'
Tkinter回调中出现异常
回溯(最近一次呼叫最后一次):
文件“C:\Python27\lib\lib tk\Tkinter.py”,第1486行,在uu调用中__
返回self.func(*args)
文件“C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py”,第209行,在
toVariable.get(),ccVariable.get(),subjectVariable.get(),textEntry.get('1.0','end-1c'),gFilePath))
文件“C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py”,第164行,在validate_输入中
发送邮件(邮件服务器、发件人、收件人、抄送收件人、主题收件人、邮件、收件人文件路径)
文件“C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py”,第117行,在send_mail中
client_socket.send(创建_消息(from_var,to_var,cc_var,subject_var,message,file_path))
文件“C:/Users/Archer/Google Drive/PycharmProjects/SMTPClient/SMTPClient.py”,第50行,在create_消息中
返回msgRoot.as_string()
文件“C:\Python27\lib\email\message.py”,第137行,格式为字符串
g、 展平(自,unixfrom=unixfrom)
文件“C:\Python27\lib\email\generator.py”,第83行,扁平化
自我书写(msg)
文件“C:\Python27\lib\email\generator.py”,第115行,以书面形式
自写头(msg)
文件“C:\Python27\lib\email\generator.py”,第164行,在写标题中
v、 maxlinelen=self.\u maxheaderlen,header\u name=h).encode()
文件“C:\Python27\lib\email\header.py”,第410行,编码
value=self.\u编码\u块(newchunks,maxlinelen)
文件“C:\Python27\lib\email\header.py”,第370行,分块编码
_最大附加(块、s、maxlinelen、额外)
文件“C:\Python27\lib\email\quoprimime.py”,第97行,在\u max\u append中
L.append(s.lstrip())
AttributeError:“list”对象没有属性“lstrip”
我使用Tkinter作为GUI,但我认为问题是从调用msgRoot.as_string()开始的。此时此刻我不知所措,因此非常感谢您提供的任何帮助。使用它可以更轻松地为您处理图像。充分披露:我是开发者
import yagmail
yag = yagmail.SMTP(from_var, 'yourpassword')
yag.send(to_var, subject_var, contents = [message, file_path])
请注意,这将发送文本消息
,然后发送一张图片,图片由文件路径
神奇地加载
您必须首先安装它:
pip install yagmail
对于其他优秀功能,请查看github上的。它将更易于为您处理图像。充分披露:我是开发者
import yagmail
yag = yagmail.SMTP(from_var, 'yourpassword')
yag.send(to_var, subject_var, contents = [message, file_path])
请注意,这将发送文本消息
,然后发送一张图片,图片由文件路径
神奇地加载
您必须首先安装它:
pip install yagmail
对于其他优秀的特性,请查看github上的。在这种情况下,msgRoot.preamble以及MIMEMultipart()构造函数中的“相关”输入以及备用HTML文本在将消息压缩为单个字符串时导致了问题。如果其他人遇到此问题,我已包含我的固定代码:
def create_message(from_var, to_var, cc_var, subject_var, message, file_path):
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart()
msgRoot['Subject'] = subject_var
msgRoot['To'] = ','.join(to_var)
msgRoot['From'] = from_var
if cc_var != '':
msgRoot['CC'] = ','.join(cc_var)
msgText = MIMEText(
message
)
msgRoot.attach(msgText)
if file_path != '':
file_name = file_path.rpartition('\\')[2]
# This example assumes the image is in the current directory
fp = open(file_path, 'rb')
msgImage = MIMEImage(fp.read(), _subtype='jpg')
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<' + file_name + '>')
msgRoot.attach(msgImage)
stringMessage = msgRoot.as_string()
return stringMessage
def create_message(from_var,to_var,cc_var,subject_var,message,file_path):
#创建根消息并填写发件人、收件人和主题标题
msgRoot=MIMEMultipart()
msgRoot['Subject']=Subject\u var
msgRoot['To']=','.连接(To_var)
msgRoot['From']=From_var
如果cc_var!='':
msgRoot['CC']=','.join(CC_var)
msgText=MIMEText(
消息
)
msgRoot.attach(msgText)
如果文件路径!='':
file\u name=file\u path.r分区('\\')[2]
#本例假定映像位于当前目录中
fp=打开(文件路径“rb”)
msgImage=MIMEImage(fp.read(),_subtype='jpg')
fp.close()
#如上所述定义图像的ID
msgImage.add_头('Content-ID','')
msgRoot.attach(msgImage)
stringMessage=msgRoot.as_string()
返回字符串消息
在这种情况下,当需要将消息压缩为单个字符串时,msgRoot.preamble以及MIMEMultipart()构造函数中的“相关”输入和备用HTML文本似乎会导致问题。如果其他人遇到此问题,我已包含我的固定代码:
def create_message(from_var, to_var, cc_var, subject_var, message, file_path):
# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart()
msgRoot['Subject'] = subject_var
msgRoot['To'] = ','.join(to_var)
msgRoot['From'] = from_var
if cc_var != '':
msgRoot['CC'] = ','.join(cc_var)
msgText = MIMEText(
message
)
msgRoot.attach(msgText)
if file_path != '':
file_name = file_path.rpartition('\\')[2]
# This example assumes the image is in the current directory
fp = open(file_path, 'rb')
msgImage = MIMEImage(fp.read(), _subtype='jpg')
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<' + file_name + '>')
msgRoot.attach(msgImage)
stringMessage = msgRoot.as_string()
return stringMessage
def create_message(from_var,to_var,cc_var,subject_var,message,file_path):
#创建根消息并填写发件人、收件人和主题标题
msgRoot=MIMEMultipart()
msgRoot['Subject']=Subject\u var
msgRoot['To']=','.连接(To_var)
姆斯格罗