如何使用当前的电子邮件库附加一个 txt 文件?
How to attach a txt file using the current email library?
与此问题相关的许多答案对应于电子邮件库 (EMAIL OLD DOC.). I want to send an email attaching a txt file with the current documentation (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)
您只需要使用 MIMEMultipart 作为您的容器,并将所有内容附加到它上面,所以尝试这样的操作:
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
msg = MIMEMultipart()
msg.attach(MIMEText(body, "plain"))
with open(filename, "r") as attach:
part = MIMEBase("application", "octet-stream")
part.set_payload(attach.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
msg.attach(part)
...
send_mail(message=msg, user=user, password=password, smpt_mail=smpt, port=port)
如果您想使用 EmailMessage class 以附件形式发送文本文件,可以这样做:
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['subject'] = 'Here is a text attachment'
msg['from'] = 'me@example.com'
msg['to'] = 'you@example.com'
# 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: me@example.com'
b'to: you@example.com'
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==--'
与此问题相关的许多答案对应于电子邮件库 (EMAIL OLD DOC.). I want to send an email attaching a txt file with the current documentation (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)
您只需要使用 MIMEMultipart 作为您的容器,并将所有内容附加到它上面,所以尝试这样的操作:
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
msg = MIMEMultipart()
msg.attach(MIMEText(body, "plain"))
with open(filename, "r") as attach:
part = MIMEBase("application", "octet-stream")
part.set_payload(attach.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
msg.attach(part)
...
send_mail(message=msg, user=user, password=password, smpt_mail=smpt, port=port)
如果您想使用 EmailMessage class 以附件形式发送文本文件,可以这样做:
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['subject'] = 'Here is a text attachment'
msg['from'] = 'me@example.com'
msg['to'] = 'you@example.com'
# 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: me@example.com'
b'to: you@example.com'
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==--'