Package: protonmail-api-client
This is not an official python ProtonMail API client. it allows you to read, send and delete messages in protonmail, as well as render a ready-made template with embedded images.Unfortunately, I could not find an analogue of DecryptSessionKeys from OpenPG.js, so this uses Playwright to execute js. if you have any ideas write to meInstallationInstall requirements:pip install protonmail-api-clientInstall Playwright:playwright install webkitGetting StartedGet PGP private key and passphraseGo to theEmail encryption keyssection, click on "Export private key" (NOT the Account keys), create the passphrase.fromprotonmailimportProtonMailusername="YouAddress@proton.me"password="YourPassword123"proton=ProtonMail()proton.login(username,password)private_key='privatekey.YourAddress@proton.me-123...89.asc'passphrase='YourPassphrase'proton.pgp_import(private_key,passphrase=passphrase)# Get a list of all messagesmessages=proton.get_messages()# Read the latest messagemessage=proton.read_message(messages[0])print(message.sender.address)# sender addressprint(message.subject)# subjectprint(message.body)# <html><body><div>it's my image: <img src="cid:1234@proton.me">....# Render the template, images downloading, converting to BASE64 and insert into htmlproton.render(message)# This is a ready-made html page, with all the pictures, you can save it right awaywithopen('message.html','w',encoding='utf-8')asf:f.write(message.body)print(message.body)# <html><body><div>it's my image: <img src="data:image/png;base64, iVBORw0K..">....# Download file from messagefirst_file=message.attachments[0]proton.download_files([first_file])withopen(f'{first_file.name}','wb')asf:f.write(first_file.content)# Send messagerecipients=["to@gmail.com","to2@gmail.com"]# You can’t send to @proton.me/@protonmail.com yetsubject="My first message"body="<html><body>hello, i sent my first mail!</body></html>"# html or just textnew_message=proton.create_message(recipients=recipients,subject=subject,body=body)sent_message=proton.send_message(new_message)# Wait for new messagenew_message=proton.wait_for_new_message(interval=1,timeout=60,rise_timeout=False,read_message=True)if'spam'innew_message.body:# Delete spamproton.delete_messages([new_message])# Save session, you do not have to re-enter your login, password, pgp key, passphrase# WARNING: the file contains sensitive data, do not share it with anyone,# otherwise someone will gain access to your mail.proton.save_session('session.pickle')# Load sessionproton=ProtonMail()proton.load_session('session.pickle',auto_save=True)# Autosave is needed to save tokens if they are updated# (the access token is only valid for 24 hours and will be updated automatically)# Getting a list of all sessions in which you are authorizedproton.get_all_sessions()# Revoke all sessions except the current oneproton.revoke_all_sessions()event pollingEvent polling. Polling ends in 3 cases:Callback returns notNone.The callback raises theSystemExitexception.Timeout ends.For example, wait indefinitely until 2 messages arrive.defcallback(response:dict,new_messages:list):messages=response.get('Messages',[])new_messages.extend(messages)iflen(new_messages)>=2:raiseSystemExitnew_messages=[]proton.event_polling(callback,new_messages)print(new_messages)