I am writing code in python to send email. I use smtplib and mime for sending email and formatting the content. I convert the word document to HTML to use as content. The following mail was send with the text, but the image wasn't processed in mail. Do you have any recommendation. I have tried to embed the image into the html also but it does not work.
import os
from docx import Document
import re
import smtplib
from email.message import EmailMessage
from docxtpl import DocxTemplate
import pyperclip
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from bs4 import BeautifulSoup
from string import Template
from email.mime.application import MIMEApplication
import glob
from email.mime.image import MIMEImage
from jinja2 import Template
with open('welcome_message.htm') as f:
a=f.read()
soup=BeautifulSoup(a, 'lxml')
for div in soup.find_all('p', {'class':'delete'}):
div.decompose()
with open('Aloitusmateriaalit.zip', 'rb') as f:
part=MIMEApplication(f.read())
part['subject']='attachement'
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename('Aloitusmateriaalit.zip'))
msg.attach(part)
word=['$PERSON_NAME','$FIRST_DATE','$ADDRESS','$PEER_ADVISOR','$SUPERVISOR']
target=soup.find_all(text=re.compile('|'.join(map(re.escape,word))))
for v in target:
v.replace_with(v.replace('$PERSON_NAME', 'Duyen').
replace('$FIRST_DATE', 'Duyen').replace('$ADDRESS', 'Duyen'))
= main.render(pictures=list_of_images)
part1=MIMEText(soup, 'html')
msg.attach(part1)
msg.add_header('Content-Disposition', 'attachment', filename='data.XLSX')
msg['Subject'] ='test email'
smtpObj = smtplib.SMTP('')
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('xxxxx', 'xxxxx')
smtpObj.sendmail('xxxxx', 'xxxx', msg.as_string())
smtpObj.quit()
and the word document look something like this https://1drv.ms/w/s!Anyt3NEJ2JjqgeJtFMRGug96QkYFqA (i have deleted the content but it is not important) because the important is that the image does not show up. I save the docx as htm (welcome_message.htm) and then run the code