gpt4 book ai didi

python - 如何使用当前的电子邮件库附加txt文件?

转载 作者:行者123 更新时间:2023-12-02 02:46:02 25 4
gpt4 key购买 nike

与此问题相关的许多答案都对应于电子邮件库中的旧文档 ( EMAIL OLD DOC. )。我想发送一封电子邮件,附上包含当前文档的 txt 文件 ( EMAIL DOC. )。

示例:

import smtplib, ssl
from email.message import EmailMessage
from email.utils import formatdate


def mail_info(user, message):
msg = EmailMessage()
msg['From'] = user
msg['To'] = user
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = 'Subject'
msg.add_attachment(message, maintype='text', subtype='txt')
return msg

def send_mail(message, user, password, smtp_mail, port):
msg = mail_info(user, message)
context = ssl.create_default_context()

with smtplib.SMTP_SSL(host=smtp_mail, port=port, context=context) as server:
'''conect to the server'''
server.login(user=user, password=password)
server.sendmail(from_addr=user, to_addrs=user, msg=msg.as_string())

message = 'This is my email'
with open('filename.txt', 'w+') as f:
f.write(message)
attachment = f.read().encode('utf-8')
send_mail(message=attachment, user=user, password=password,
smtp_mail=smtp, port=port)

最佳答案

如果您想使用 EmailMessage 将文本文件作为附件发送类,它是这样完成的:

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg['subject'] = 'Here is a text attachment'
msg['from'] = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e5edc8edf0e9e5f8e4eda6ebe7e5" rel="noreferrer noopener nofollow">[email protected]</a>'
msg['to'] = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60190f15200518010d100c054e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>'

# Set message body.
msg.set_content('Hello\nand\ngoodbye')

# Add the attachment
with open('foo.txt', 'rb') as f:
data = f.read()
msg.add_attachment(data, maintype='text', subtype='plain')

with smtplib.SMTP('localhost', 1025) as s:
s.send_message(msg)

如果我们运行 SMTP 调试服务器,则在另一个终端中监听端口 1025

 python3 -m smtpd -n -c DebuggingServer localhost:1025

我们可以看到结果消息

b'subject: Here is a text attachment'
b'from: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="29444c694c51484459454c074a4644" rel="noreferrer noopener nofollow">[email protected]</a>'
b'to: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe87918bbe9b869f938e929bd09d9193" rel="noreferrer noopener nofollow">[email protected]</a>'
b'MIME-Version: 1.0'
b'Content-Type: multipart/mixed; boundary="===============4749170073857401373=="'
b'X-Peer: ::1'
b''
b'--===============4749170073857401373=='
b'Content-Type: text/plain; charset="utf-8"'
b'Content-Transfer-Encoding: 7bit'
b''
b'Hello'
b'and'
b'goodbye'
b''
b'--===============4749170073857401373=='
b'Content-Type: text/plain'
b'Content-Transfer-Encoding: base64'
b'MIME-Version: 1.0'
b'Content-Disposition: attachment'
b''
b'VGhpcyBpcyB0aGUgYXR0YWNobWVudAo='
b''
b'--===============4749170073857401373==--'

关于python - 如何使用当前的电子邮件库附加txt文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62785691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com