include an image in a email with python

Posted on
Tue Dec 20, 2022 3:19 pm
Hans Kiesewetter offline
Posts: 11
Joined: Nov 19, 2014

include an image in a email with python

I'm able to compose a HTML-formatted body of an email with python and send it with Email+, but how do I include an image located in a certain folder?

My script in python2 used in Indigo7 does not work in Indigo 2022.2.

old method: (Don't remember where I found this coding, I'm not a programmer myself :? )
.....
theBody ="some text with HTML formatting"
theBody=theBody + "<p><img src='cid:image1'><br>"
theBody=theBody + "<img src='cid:image2'></p>"
msgText = MIMEText(theBody, 'html')
fp = open(os.path.expanduser("~/Documents/INDIGOplotD/Temperatuur-minute-S2.png"), "rb")
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
fp = open(os.path.expanduser("~/Documents/INDIGOplotD/vermogen-minute-S2.png"), "rb")
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image2>')
msgRoot.attach(msgImage)
.......

Posted on
Tue Dec 20, 2022 3:31 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: include an image in a email with python

Use Email+ plugin scripting:

Code: Select all
props = {   
    'emailTo': 'address1, address2',
    'emailCC': 'address3, address4',
    'emailBCC': 'address5, address6',
    'emailSubject': 'Message Subject',
    'emailAttachments': 'file1, file2, file3',
    'emailFormat': 'plain'   (or 'html')
    'emailMessage': 'Message text'
}

emailPlugin = indigo.server.getPlugin("com.indigodomo.email")
if emailPlugin.isEnabled():
    emailPlugin.executeAction("sendEmail", deviceId=12345678, props=props)


The deviceId is the ID of the SMTP device that was created when you upgraded to 2022.X.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Dec 20, 2022 3:50 pm
Hans Kiesewetter offline
Posts: 11
Joined: Nov 19, 2014

Re: include an image in a email with python

Hi FLyingDiver.
Thanks for the quick reply
Your example is exactly is the way I finally send my email now. The problem is that I want to include an image somewhere in the body of the mail, not as separate attachment
So I have to include some code to include it in "message text". So somewhere in the code for theBody in the below example. Using an URL referring to a location on my harddrive does not work for the receiver of the mail.

My new code (without an image)
....
theBody=theBody + "<tr><td>- Oven: " + tdr + str(Teller2) + "</td><td>kWh</td></tr>"
theBody=theBody + "<tr><td>- Wasmachine:" + tdr + str(Teller3) + "</td><td>kWh</td></tr>"
theBody=theBody + "</table></p>"
theBody=theBody + "<p> ***** some code for an image ***</p>

props = {
'emailTo': 'J.Kiesewetter@nospam.nl',
'emailSubject': 'Dagrapport',
'emailFormat': 'html',
'emailMessage': theBody
}

plugin = indigo.server.getPlugin("com.indigodomo.email")
if plugin.isEnabled():
plugin.executeAction("sendEmail", deviceId= 1117910686, props=props)

Posted on
Tue Dec 20, 2022 3:53 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: include an image in a email with python

You're going to need to compose the email in another client then look at the exact structure of the HTML generated. You probably need to base64 encode the actual image then include that string in the body.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest