I'm trying to make an automated task that (1) collects CSV data about wildfires from the internet, (2) reads its contents to see where wildfires in the CSV have occurred in the past 3 days, (3) visualizes them on a map, (4) and sends an email to a specific alias, with text and a map about where they have occurred.
In the email (4), I want to mention the locations of wildfires, which are in the form of a string:
print(provinces_on_fire_str)
Out[0]: "Province1, Province2"
I used Content-ID to add an image to the e-mail body, using the following code:
# set the plaintext body
msg.set_content('This is a plain text body.')
# create content-ID for the image
image_cid = make_msgid()
# alternative HTML body
msg.add_alternative("""\
<html>
<body>
<p>This e-mail contains information about fires reported in the past 3 days.<br>
The VIIRS sensor has reported wildfires in the following provinces: {provinces_on_fire_str}
This e-mail was sent automatically.
</p>
<img src="cid:{image_cid}">
</body>
</html>
""".format(image_cid=image_cid[1:-1]), subtype="html")
# attach image to mail
with open(f"path/toimage/{today}.png", 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split("/")
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid)
This returns an error, implying no such object as "provinces_on_fire_str" exists for the HTML code. Without the "provinces_on_fire_str" variable in the HTML body, the expected output email is the following (albeit this lacks the text explanation of where they occurred):
Now, the obvious thing that came to my mind is to convert the HTML body part to an f-string, so I can add the "Province1, Province2" values to the e-mail text. But adding f before the e-mail string breaks the image_cid (though the Province1, Province2 values are included in the ultimate e-mail).
.add_alternative with f-string input:
# alternative HTML body
msg.add_alternative(f"""\
<html>
<body>
<p>This e-mail contains information about fires reported in the past 3 days.<br>
The VIIRS sensor has reported wildfires in the following provinces: {provinces_on_fire_str}
This e-mail was sent automatically.
</p>
<img src="cid:{image_cid}">
</body>
</html>
""".format(image_cid=image_cid[1:-1]), subtype="html")
Output email:
How do I pass the string values of provinces_on_fire_str into the HTML code without breaking the image_cid?