Вложение smtplib приходит, а тело письма отсутствует - PullRequest
0 голосов
/ 11 апреля 2020

Используя Python smtplib, пытаясь отправить письмо с телом и вложением, пока вложение приходит, тело сообщения всегда отсутствует. From / To / Subject во фрагменте ниже был исключен.

msg = MIMEMultipart('')
line_2 = 2
line_3 = 3
test = ""
"Line 1 = 1 \r\r\n" \
"Line 2 = {0}\r\r\n" \
"Line 3 = {1}\r\r\n" \
"Line 4 = 4\r\r\n" \
"Line 5 = 5\r\r\\n".format(line_2, line_3)
msg.attach(MIMEText(test,'plain'))
# Attaching content:
if fname is not None:
    with open(fname, "rb") as f:
        part = MIMEApplication(f.read(), Name=basename(fname))
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(fname)
    msg.attach(part)

# Sending the email:
with smtplib.SMTP(smtp) as s:
    s.sendmail(msg["From"], msg["To"].split(",") , msg.as_string())

Я также пытался использовать email.EmailMessage, но снова я не вижу тело письма

msg = EmailMessage()
msg = MIMEMultipart('')
msg.set_content(mail_body)  

# Attaching content:
if fname is not None:
    with open(fname, "rb") as f:
        part = MIMEApplication(f.read(), Name=basename(fname))
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(fname)
    msg.make_mixed()
    msg.attach(part)

# Sending the email:
with SMTP(smtp) as smtp_server:
    smtp_server.send_message(msg)

I Я не уверен, почему я не получаю почту.

...