CDG
CDG

Reputation: 73

Python: What is the correct MIME type for attaching html file to email

I've got a script that sends emails with html content in them.. works as expected... I'm having trouble sending an attachment with the email.

The attachment is an html file stored in the active directory of the script... "test.html"

How do I attach the html file to the email? I've tried snippets from various other posts I've found relating to this, but each returned the same output of "no such file or directory".

code as follows:

import smtplib
import os
import email.encoders
import email.mime.text
import email.mime.base
import mimetools
import base64



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

    # me == Outgoing email address
    # you == Recipient's email address
me = "secret"
you = "secret"

    # Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "TEST"
msg['From'] = me
msg['To'] = you
emailMsg = email.MIMEMultipart.MIMEMultipart('alternative')

    # Create the body of the message (a plain-text and an HTML version).
html = """\
    <html>
    <head></head>
    <body>test</p>
</body>
</html>"""


    # Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
filename = "C:/CHRIS/ServerStatus/Oceaneering_Server_Status.html"
f = file(filename)
attachment = MIMEText(f.read(), _subtype='html')
attachment.add_header('Content-Disposition', 'attachment', filename=filename)

    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
msg.attach(attachment)

    # Send the message via local SMTP server.
mail = smtplib.SMTP('smtp.gmail.com', 587)

mail.ehlo()

    # mail.login(username, password) to Outgoing email account
mail.login('secret', 'secret')
mail.sendmail(me, you, msg.as_string())
mail.quit()

i've updated my code in hopes to get this question back on topic... i've made a little progress with the help of Dirk and this link: Attach a txt file in Python smtplib...

I've been able to physically send an attachment now, but the attachment is still coming through as a text type of file of sort and does not open as the original html file does.

So to reword my question... What is the corrective action for changing the MIME type of this code to correctly attach an .html file to an html based email?

The relative path and directory of my py script and the html file needed to be sent is as follows: C:\CHRIS\ServerStatus\

This is the output i'm receiving with the code I have: enter image description here

This is the way the html doc looks outside of the email script (The way it's supposed to look): enter image description here

Upvotes: 3

Views: 6080

Answers (1)

Dirk
Dirk

Reputation: 2388

I would encourage you to use a library rather than deal with the -rather unpythonic- built-in mail modules, such as the highly recommended envelopes:

https://tomekwojcik.github.io/envelopes/index.html

install with:

pip install envelopes

Python code:

import os
from envelopes import Envelope

filename = "C:/CHRIS/ServerStatus/Oceaneering_Server_Status.html"

envelope = Envelope(
    from_addr=(me),
    to_addr=(you),
    subject=u'Test',
    text_body=u'Plain text version',
    html_body=html
)
envelope.add_attachment(filename)

envelope.send('smtp.gmail.com', login='secret', password='secret', tls=True)

Upvotes: 2

Related Questions