Sendemail problem openHABian exec python binding attachment

I’m running openHABian 1.4 with openHAB 2.2 stable on a rasberry Pi 3.

I wrote a python script which downloads webcam JPGs and I’d like to send them via openHAB.
The script itself returns two paths:

[18:08:03] openhab@openHAB2-huette:/srv/openhab2-userdata$ ./webcam.py
"file:///srv/openhab2-userdata/tmp/rossbuehelW-20180323-180832.jpg",
"file:///srv/openhab2-userdata/tmp/rossbuehelO-20180323-180832.jpg"

The rule in which the script is started:

import java.util.List

rule "Webcam versenden"
when
	Channel 'astro:sun:home:rise#event' triggered END or
	Channel 'astro:sun:home:set#event' triggered START or
	Item Rossbuehel_SendWebcam1 changed to ON
then
	var String cmd = "python /srv/openhab2-userdata/webcam.py"
	logInfo("Webcam", "Kommando: " + cmd)
	var String bilder = executeCommandLine(cmd, 120000)
	logInfo("Webcam", "Bilderstring: " + bilder)
	val List<String> attachmentUrlList = newArrayList()

	var String subject = "Aktuelles Rossbuehel Bild"
	var String text = "Hier ein aktuelles Rossbuehel Bild. Hoffe, es gefällt dir."
	sendMail("john@deere.com", subject, text, attachmentUrlList)
end

So, the script is supposed to save the JPGs and send out the file list for attachmentUrlList - which it does:

2018-03-23 18:34:24.497 [INFO ] [clipse.smarthome.model.script.Webcam] - Kommando: python /srv/openhab2-userdata/webcam.py
2018-03-23 18:35:28.329 [INFO ] [clipse.smarthome.model.script.Webcam] - Bilderstring: "file:///srv/openhab2-userdata/tmp/rossbuehelW-20180323-183447.jpg",
"file:///srv/openhab2-userdata/tmp/rossbuehelO-20180323-183447.jpg"
2018-03-23 18:35:28.413 [ERROR] [rg.openhab.action.mail.internal.Mail] - Invalid attachment url.
java.net.MalformedURLException: no protocol: "file:///srv/openhab2-userdata/tmp/rossbuehelW-20180323-183447.jpg",
"file:///srv/openhab2-userdata/tmp/rossbuehelO-20180323-183447.jpg"
	at java.net.URL.<init>(URL.java:593) ~[?:?]
	at java.net.URL.<init>(URL.java:490) ~[?:?]
	at java.net.URL.<init>(URL.java:439) ~[?:?]
	at org.openhab.action.mail.internal.Mail.sendMail(Mail.java:116) ~[?:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1085) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:1060) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1046) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:991) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:141) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189) ~[?:?]
	at org.eclipse.smarthome.model.script.runtime.internal.engine.ScriptImpl.execute(ScriptImpl.java:82) ~[?:?]
	at org.eclipse.smarthome.model.rule.runtime.internal.engine.RuleEngineImpl.lambda$2(RuleEngineImpl.java:343) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:?]
	at java.lang.Thread.run(Thread.java:748) [?:?]```
But - the email is sent without any attachment. How can I transfer the "string" to the `attachmentUrlList` so, that it will be sent out as two attachments?
[18:08:03] openhab@openHAB2-huette:/srv/openhab2-userdata$ ./webcam.py
"file:///srv/openhab2-userdata/tmp/rossbuehelW-20180323-180832.jpg",
"file:///srv/openhab2-userdata/tmp/rossbuehelO-20180323-180832.jpg"

replace file by ftp

Unfortunately still the same error:

2018-03-24 07:13:18.235 [INFO ] [clipse.smarthome.model.script.Webcam] - Bilderstring: "ftp:///srv/openhab2-userdata/tmp/rossbuehelW-20180324-071237.jpg","ftp:///srv/openhab2-userdata/tmp/rossbuehelO-20180324-071237.jpg"                                                                                        2018-03-24 07:13:18.380 [ERROR] [rg.openhab.action.mail.internal.Mail] - Invalid attachment url.
java.net.MalformedURLException: no protocol: "ftp:///srv/openhab2-userdata/tmp/rossbuehelW-20180324-071237.jpg"

Hi Thomas, try only 2 // after the ftp:

unfortunately no luck with this also:

2018-03-24 09:45:42.915 [INFO ] [clipse.smarthome.model.script.Webcam] - Bilderstring: "ftp://srv/openhab2-userdata/tmp/rossbuehelW-20180324-094501.jpg",
"ftp://srv/openhab2-userdata/tmp/rossbuehelO-20180324-094501.jpg"
2018-03-24 09:45:43.016 [ERROR] [rg.openhab.action.mail.internal.Mail] - Invalid attachment url.
java.net.MalformedURLException: no protocol: "ftp://srv/openhab2-userdata/tmp/rossbuehelW-20180324-094501.jpg",
"ftp://srv/openhab2-userdata/tmp/rossbuehelO-20180324-094501.jpg"
	at java.net.URL.<init>(URL.java:593) ~[?:?]
	at java.net.URL.<init>(URL.java:490) ~[?:?]
	at java.net.URL.<init>(URL.java:439) ~[?:?]
	at org.openhab.action.mail.internal.Mail.sendMail(Mail.java:116) ~[?:?]
...
2018-03-24 09:48:12.591 [INFO ] [clipse.smarthome.model.script.Webcam] - Bilderstring: "file://srv/openhab2-userdata/tmp/rossbuehelW-20180324-094731.jpg",
"file://srv/openhab2-userdata/tmp/rossbuehelO-20180324-094731.jpg"
2018-03-24 09:48:12.666 [ERROR] [rg.openhab.action.mail.internal.Mail] - Invalid attachment url.
java.net.MalformedURLException: no protocol: "file://srv/openhab2-userdata/tmp/rossbuehelW-20180324-094731.jpg",
"file://srv/openhab2-userdata/tmp/rossbuehelO-20180324-094731.jpg"
	at java.net.URL.<init>(URL.java:593) ~[?:?]
	at java.net.URL.<init>(URL.java:490) ~[?:?]
	at java.net.URL.<init>(URL.java:439) ~[?:?]
	at org.openhab.action.mail.internal.Mail.sendMail(Mail.java:116) ~[?:?]

is there a way to be sure, the list attachmentUrlList is the right format?

The docs tell file:///path with three slashes:
https://docs.openhab.org/addons/actions/mail/readme.html#examples

Hi Thomas,

for me, this rule worked fine for a while. Unfortunatly I can’t test if this is still the case, because system is down. (code is a copy from my older backup, my system: openHABian 1.4 with openHAB 2.2 stable on a rasberry Pi 3)
Maybe it helps you:

//[Imports]
import java.util.List

//[Rules]
rule "SendCamPicture"

when
    Item MyTrigger changed to ON
then
       // Verzeichnis festlegen
        var outputDir = "/home/openhabian/KameraBilder/"  // Verzeichnis für Bilder, Besitzer muss openhab sein!

       // Bild Kamera 1 in Verzeichnis speichern
        var triggerZeit1 = now.toString ("yyyyMMdd_HHmmss")
        var inputDateiCam1 = "http://.../cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=...&pwd=..."
        var outputDateiCam1 = triggerZeit1 + "_Einfahrt.jpg"
        var outputDirDateiCam1 = outputDir + outputDateiCam1
        var cmdCam1 = "wget --output-document " + outputDirDateiCam1 + " " + inputDateiCam1
        executeCommandLine(cmdCam1,5000)     

       // Bild Kamera 6 in Verzeichnis speichern
        var triggerZeit6 = now.toString ("yyyyMMdd_HHmmss")
        var inputDateiCam6 = "http://...:...@.../snapshot.cgi"
        var outputDateiCam6 = triggerZeit6 + "_Garage.jpg"
        var outputDirDateiCam6 = outputDir + outputDateiCam6
        var cmdCam6 = "wget --output-document " + outputDirDateiCam6 + " " + inputDateiCam6
        executeCommandLine(cmdCam6,5000)     

       // Bilderliste erzeugen
        var List<String> bilderliste = newArrayList(
            "file://" + outputDirDateiCam1,
            "file://" + outputDirDateiCam6)

       // E-Mail verschicken
        sendMail("your@email.de", "CamBilder", "Hier kommen CamBilder!", bilderliste)
end
1 Like

OK now… It works.
It turns out, you can’t just throw in a string as two array-elements. So I had to change my scripts a bit:

  • the python-script which takes the JPGs writes an JSON with both filenames
  • the rule uses the filenames in the JSON

After that: file:/// (with two slashes for “file” and one for the path did it.

So, for everyone interested

the JSON-Output:

{ "filename1":"/srv/openhab2-userdata/tmp/rossbuehelW-20180324-165324.jpg", "filename2":"/srv/openhab2-userdata/tmp/rossbuehelO-20180324-165324.jpg"}

the rule (sorry for german annotations, if someone Needs it, I can translate:

Import java.util.List

rule "Webcam versenden"
when
	Channel 'astro:sun:home:rise#event' triggered END or
	Channel 'astro:sun:home:set#event' triggered START or
	Item Rossbuehel_SendWebcam1 changed to ON
then
	// Python Skript anschmeissen, das die Kamera auf Position bringt und Bilder schiesst
	var String cmd = "python /srv/openhab2-userdata/webcam.py"
	logInfo("Webcam", "Kommando: " + cmd)

	// Rückantwort des Python Skripts sind die jeweiligen Bildpfade durch | getrennt
	var String bilderjson = executeCommandLine(cmd, 120000)

	var String filename1 = transform("JSONPATH", "$.filename1", bilderjson)	
	var String filename2 = transform("JSONPATH", "$.filename2", bilderjson)	
	
	val List<String> attachmentUrlList = newArrayList(
		"file://" + filename1,
		"file://" + filename2)

	var String subject = "Aktuelles Rossbuehel Bild"
	var String text = "Hier ein aktuelles Rossbuehel Bild. Hoffe, es gefällt dir."
	sendMail("thomas@die-binders.de", subject, text, attachmentUrlList)
end

and as a bonus, here’s the sunrise picture it sends (low resolution,limited bandwidth up there):
image

Hi Thomas, glad you got it sorted.
I was basing my advice on my experience of ftp.
I didn’t read the mail action docs!!
Good job!!

the sendemail- Action also understands ftp or http adresses. So that’s not that far! :wink:
If I had a “normal” Webcam, I could just use the snapshot-URL function of it within the rule to add an attachment… But I had to buy a PTZ! :wink: (ok, I do have multiple presets in mind, so it pays after at least three)