Search code examples
pythonhtmlpdfembed

Embed PDF on shared drive into an HTML email


My python script is creating a PDF that is stored on a shared T: drive. I want to take this PDF and embed it into the body of an HTML email script using the file path. I have sent HTML emails before with images embedded but they have always come from an online source.

Like "src = https:// www . somewebsite.com/image.jpg"

Is there a way to embed this PDF using just the file path?

Thank you


Solution

  • Did a workaround. Made my PDFs into jpegs and then did this:

    data = open(rf'path.jpg', 'rb').read()  # read bytes from file
    data_base64 = base64.b64encode(data)  # encode to base64 (bytes)
    data_base64 = data_base64.decode()  # convert bytes to string
    code = '<img src="data:image/jpeg;base64,' + data_base64 + '">'  # embed in html
    open('output.html', 'w').write(code) #add html to a text file to be read by email