def send(self, target: str, subject: str, html_content_with_cids: str, inline_png_cids_filenames: {str : str}):
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = self.sender
msg['To'] = target
msg.set_content('')
msg.add_alternative(
html_content_with_cids, subtype='html'
)
for png_cid in inline_png_cids_filenames:
full_path_to_png = os.path.abspath(os.path.join(
os.path.dirname(__file__), inline_png_cids_filenames[png_cid]
))
with open(full_path_to_png, 'rb') as png_file:
file_contents = png_file.read()
msg.get_payload()[1].add_related(file_contents, 'image', 'png', cid=png_cid)
with smtplib.SMTP(self.smtp_server_host, self.smtp_server_port) as smtp_connection:
smtp_connection.starttls()
smtp_connection.login(self.username, self.password)
smtp_connection.send_message(msg)
评论列表
文章目录