-1

How can I attach some files to an MIME multipart email using Python3? I want to send some attachments as "downloadable content" to my HTML-mail (with a plain text fallback). Couldn't find anything so far...

Edit: After a few trys I just made it to send my file. Thanks for the tip @tripleee. But unfortunatly my HTML is now sent as plain text...

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formatdate
from os.path import basename
import smtplib

login = "*********"
password = "*********"
server = "smail.*********:25"
files = ['Anhang/file.png']

# Create the root message and fill in the from, to, and subject headers
msg = MIMEMultipart('related')
msg['From'] = "*********"
msg['To'] = "*********"
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "*********"
msg['Reply-To'] = "*********"
msg.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')

with open('*********.txt', 'r') as plainTXT:
    plain = plainTXT.read()
plainTXT.close()
msgAlternative.attach(MIMEText(plain))

with open('*********.html', 'r') as plainHTML:
    html = plainHTML.read()
plainHTML.close()
msgAlternative.attach(MIMEText(html))

msg.attach(msgAlternative)

# Image
for f in files or []:
    with open(f, "rb") as fp:
        part = MIMEImage(
            fp.read(),
            Name=basename(f)
        )
    # closing file
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
    msg.attach(part)


# create server
server = smtplib.SMTP(server)
server.starttls()

# Login Credentials for sending the mail
server.login(login, password)

# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())

server.quit()

print("successfully sent email to %s:" % (msg['To']));
F. Geißler
  • 228
  • 2
  • 3
  • 20
  • There are many questions like this. Yours doesn't appear to contain any attempt to actually attach any images so it's hard to see what you need help with. Basically, create a `multipart/related` and inside that put the `multipart/alternative` and the binaries as `image/png` or whatever. You might want to put `Content-Disposition: attachment` if you really don't want the recipient's mail client to try to render them – tripleee Nov 02 '18 at 12:32
  • Perhaps this can help for a start? https://stackoverflow.com/questions/48562935/what-are-the-parts-in-a-multipart-email – tripleee Nov 02 '18 at 12:35
  • Changed my original post. Now the only thing to do is fixing the plain html thing. Thank you! – F. Geißler Nov 02 '18 at 14:53

1 Answers1

0

I just had to use MIMEText(html, 'html') for the attaching part of my HTML. Working code:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formatdate
from os.path import basename
import smtplib

login = "YourLogin"
password = "YourPassword"
server = "SMTP-Server:Port"
files = ['files']

# Create the root message and fill in the from, to, and subject headers
msg = MIMEMultipart('related')
msg['From'] = "FromEmail"
msg['To'] = "ToEmail"
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "EmailSubject"
msg['Reply-To'] = "EmailReply"
msg.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')

with open('YourPlaintext.txt', 'r') as plainTXT:
    plain = plainTXT.read()
plainTXT.close()
msgAlternative.attach(MIMEText(plain, 'plain'))

with open('YourHTML.html', 'r') as plainHTML:
    html = plainHTML.read()
plainHTML.close()
msgAlternative.attach(MIMEText(html, 'html'))

msg.attach(msgAlternative)

# Image
for f in files or []:
    with open(f, "rb") as fp:
        part = MIMEImage(
            fp.read(),
            Name=basename(f)
        )
    # closing file
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
    msg.attach(part)


# create server
server = smtplib.SMTP(server)
server.starttls()

# Login Credentials for sending the mail
server.login(login, password)

# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())

server.quit()

print("successfully sent email to %s:" % (msg['To']));

Thanks to @tripleee

F. Geißler
  • 228
  • 2
  • 3
  • 20