Imap email content message body seems encrypted

I have setup the imap email content sensor and it works as expected except when I get an email from my camera recorder. The message looks fine in email clients but in HA, it looks like this.

from: [email protected]
subject: HCVR ALERT
date: Tue, 13 Feb 2018 04:26:01 -0800 (PST)
body: QWxhcm0gRXZlbnQ6IE1vdGlvbiBEZXRlY3QgQ2xlYXINCkFsYXJtIGlucHV0IGNoYW5uZWwgTm8uOiAyICANCkFsYXJtIFN0b3AgVGltZShEL00vWSBIOk06Uyk6IDEzLzIvMjAxOCA0OjI2OjMwDQpBbGFybSBEZXZpY2UgTmFtZTogSENWUg0KU2VuZGVyIElQIEFkZHJlc3M6IDE5Mi4xNjguMi4zOA0K

https://dahuawiki.com/Email/Email_Notifications_Setup

That’s a base64 encoded body:

Alarm Event: Motion Detect Clear
Alarm input channel No.: 2  
Alarm Stop Time(D/M/Y H:M:S): 13/2/2018 4:26:30
Alarm Device Name: HCVR
Sender IP Address: 192.168.2.38

You just need to decode it - possibly you can use templates to do that, and pass it through | b64decode

Yep - @tinkerer is right! It is Base64 encoded, the see the same value he posted.

But I am not sure the base64 decoding is available in templates just yet… I believe it has to be implemented. You can use python_script and pass that information to python_script and you can decode there.

Thanks for steering me in the right direction. I’ll try a python script but I think i need to return the decoded value back to HA through MQTT or API. I am hoping for a simpler solution like using a command line but I don’t think shell command will return values.

openssl base64 -d <<< QWxhcm0gRXZlbnQ6IE1vdGlvbiBEZXRlY3QgQ2xlYXINCkFsYXJtIGlucHV0IGNoYW5uZWwgTm8uOiAyICANCkFsYXJtIFN0b3AgVGltZShEL00vWSBIOk06Uyk6IDEzLzIvMjAxOCA0OjI2OjMwDQpBbGFybSBEZXZpY2UgTmFtZTogSENWUg0KU2VuZGVyIElQIEFkZHJlc3M6IDE5Mi4xNjguMi4zOA0K

I decided to go with php because i used it before for mqtt. This is the php code which I will run with a shell command on the state change of the imap content sensor. It can be used to read HA info and post on mqtt for HA to consume.

<?php
require("phpMQTT.php");
include('httpful.phar');



$data = \Httpful\Request::get("http://192.168.2.181:8125/api/states/sensor.hcvr_motion")->send();
$jsondata = json_decode($data, true);
$att = $jsondata['attributes'];


$body_encoded = $att['body'];
$body_decoded = base64_decode($body_encoded);
 

  $host = "192.168.2.150"; 
  $port = 1883;
  $clientid = "xx";
  $username = "xx"; 
  $password = "xx"; 
  $mqtt = new phpMQTT($host, $port, $clientid); 


  if ($mqtt->connect(true,NULL,$username,$password)) {
    $mqtt->publish("rpi3/hcvr_motion/body",$body_decoded, 0);      
    $mqtt->close();
  }else{
    echo "Fail or time out<br />";
  }

?>

i did it much easier, just 1 line of code change to decode the body text :slight_smile:

see here, my reply to that thread