相关疑难解决方法(0)

在Python smtplib中附加一个txt文件

我发送的纯文本电子邮件如下:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def send_message():
    msg = MIMEMultipart('alternative')
    s = smtplib.SMTP('smtp.sendgrid.net', 587)
    s.login(USERNAME, PASSWORD)

    toEmail, fromEmail = to@email.com, from@email.com
    msg['Subject'] = 'subject'
    msg['From'] = fromEmail
    body = 'This is the message'

    content = MIMEText(body, 'plain')
    msg.attach(content)
    s.sendmail(fromEmail, toEmail, msg.as_string())
Run Code Online (Sandbox Code Playgroud)

除了这条消息,我还想附上一个txt文件'log_file.txt'.我如何在这里附上一个txt文件?

python email smtp

29
推荐指数
2
解决办法
4万
查看次数

标签 统计

email ×1

python ×1

smtp ×1