Skip to content

Instantly share code, notes, and snippets.

@speters
Created October 12, 2025 17:16
Show Gist options
  • Select an option

  • Save speters/5ac294be59d33d97509ca83e6e0e63dc to your computer and use it in GitHub Desktop.

Select an option

Save speters/5ac294be59d33d97509ca83e6e0e63dc to your computer and use it in GitHub Desktop.
Download last month's charging log from WARP3 as PDF via http API and send it via email.
#!/usr/bin/env python3
#
# warpmail.py
#
# Downloads last month's charging log from WARP3 as PDF via http API and sends it via email.
#
# Crontab example:
# 0 5 1 * * /home/me/warpmail.py >/dev/null 2>&1
#
# Configuration is done in /etc/smtpclient.ini or ~/.smtpclient.ini as follows:
#
# [warpmail]
# smtphost = smtp.example.com
# smtpuser = [email protected]
# smtppass = secret11!
# sender_email = [email protected]
# warphost = 192.168.178.186
# user_filter = 4
# letterhead = Acme Corp.\n- Dienstwagen X-YZ123E-\nKleingrossstadt
# receiver_email = [email protected]
# car = X-YZ123E
import smtplib
import ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime, timedelta
import configparser
import requests
import json
from os import path
default_warphost = "192.168.178.186"
#warphost = "127.0.0.1:8080"
configsection = "warpmail"
config = configparser.ConfigParser({'smtphost': '',
'smtpuser': '',
'smtppass': '',
'warphost': default_warphost,
'user_filter': 4,
'letterhead': '',
'car':''
})
config.read(['/etc/smtpclient.ini',path.expanduser('~/.smtpclient.ini')])
if not config.has_section(configsection):
try:
section=config.sections()[0]
except IndexError:
section = 'DEFAULT'
smtphost = config.get(configsection, 'smtphost')
smtpuser = config.get(configsection, 'smtpuser')
smtppassword = config.get(configsection, 'smtppass')
sender_email = smtpuser
try:
sender_email=config.get(configsection, 'sender_email')
except configparser.NoOptionError:
pass
warphost = config.get(configsection, 'warphost')
receiver_email = smtpuser
try:
receiver_email = config.get(configsection, 'receiver_email')
except configparser.NoOptionError:
pass
user_filter = config.get(configsection, 'user_filter')
car = config.get(configsection, 'car')
letterhead = config.get(configsection, 'letterhead').replace('\\n', "\n")
prev_month_end = datetime.today().replace(day=1,hour=0,minute=0,second=0,microsecond=0) - timedelta(microseconds=1)
prev_month_begin = prev_month_end.replace(day=1,hour=0,minute=0,second=0,microsecond=0)
warp_prev_month_begin = int(prev_month_begin.timestamp()//60)
warp_prev_month_end = int(prev_month_end.timestamp()//60)
data={"api_not_final_acked":True,
"english":False,
"start_timestamp_min":warp_prev_month_begin,"end_timestamp_min":warp_prev_month_end,
"user_filter": user_filter,
"letterhead": letterhead}
headers= {
'Content-Type': 'application/json; charset=utf-8',
'Accept-Language': 'de-DE,de;q=0.8,en-US;q=0.5,en;q=0.3'
}
res = requests.put('http://{host}/charge_tracker/pdf'.format(host=warphost), json.dumps(data))
msg = MIMEMultipart()
msg["Subject"] = "Ladelog {car} {m}/{y}".format(y=prev_month_begin.year, m=prev_month_begin.month, car=car)
msg["From"] = sender_email
msg["To"] = receiver_email
filename = "Ladelog_{car}_{y}-{m}.pdf".format(y=prev_month_begin.year, m=prev_month_begin.month, car=car)
html = """\
<html>
<body>
<h2>Ladelog <em>{car}</em> {m}</h2>
<p>Zeitraum: {b} bis {e}.</p>
<p>Das Ladelog und die Verbrauchsdaten sind als PDF angehängt.</p>
</body>
</html>
""".format(car=car, m=prev_month_begin.strftime('%m/%Y'), b=prev_month_begin.strftime('%Y-%m-%d %H:%M:%S'),e=prev_month_end.strftime('%Y-%m-%d %H:%M:%S'))
part = MIMEText(html, "html")
msg.attach(part)
part = MIMEBase("application", "pdf")
part.set_payload(res.content)
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
"attachment", filename=filename
)
msg.attach(part)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtphost, 465, context=context) as server:
server.login(smtpuser, smtppassword)
server.sendmail(
sender_email, receiver_email, msg.as_string()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment