简体   繁体   English

Python:仅使用 stdlib 发送带附件的电子邮件?

[英]Python: Sending email with attachment only with the stdlib?

I want to send an email with attachment (for example a text file) with python.我想用 python 发送带有附件(例如文本文件)的电子邮件。 Is this possible with the stdlib or do I have to download and install other packages?这是否可以使用 stdlib 或我必须下载并安装其他软件包?

I would like to do this with the stdlib.我想用 stdlib 做到这一点。

thx.谢谢。 :) :)

You can try this:你可以试试这个:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM