Skip to main content

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.

Project description

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.

[!NOTE] Congratulations, no need more to execute OpenPGP.js via playwright 🎉🎉🎉

Installation

pip install protonmail-api-client

Using

from protonmail import ProtonMail

username = "YouAddress@proton.me"
password = "YourPassword123"

proton = ProtonMail()
proton.login(username, password)

# Get a list of all messages
messages = proton.get_messages()

# Read the latest message
message = proton.read_message(messages[0])
print(message.sender.address)  # sender address
print(message.subject)  # subject
print(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 html
proton.render(message)
# This is a ready-made html page, with all the pictures, you can save it right away
with open('message.html', 'w', encoding='utf-8') as f:
    f.write(message.body)
print(message.body)
# <html><body><div>it's my image: <img src="data:image/png;base64, iVBORw0K..">....

# Download file from message
first_file = message.attachments[0]
proton.download_files([first_file])
with open(f'{first_file.name}', 'wb') as f:
    f.write(first_file.content)

# Create attachments
with open('image.png', 'rb') as f:
    img = f.read()
with open('resume.pdf', 'rb') as f:
    pdf = f.read()

img_attachment = proton.create_attachment(content=img, name='image.png')
pdf_attachment = proton.create_attachment(content=pdf, name='resume.pdf')

html = f"""
<html>
    <body>
        <h2>Hi, I'm a python developer, here's my photo:</h2>
        <img {img_attachment.get_embedded_attrs()} height="150" width="300">
        <br/>
        Look at my resume, it is attached to the letter.
    </body>
</html>
"""

# Send message
new_message = proton.create_message(
    recipients=["to1@proton.me", "to2@gmail.com"],
    subject="My first message",
    body=html,  # html or just text
    attachments=[img_attachment, pdf_attachment],
)

sent_message = proton.send_message(new_message)

# Wait for new message
new_message = proton.wait_for_new_message(interval=1, timeout=60, rise_timeout=False, read_message=True)
if 'spam' in new_message.body:
    # Delete spam
    proton.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 session
proton = 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 authorized
proton.get_all_sessions()

# Revoke all sessions except the current one
proton.revoke_all_sessions()

event polling

Event polling. Polling ends in 3 cases:

  1. Callback returns not None.
  2. The callback raises the SystemExit exception.
  3. Timeout ends.

For example, wait indefinitely until 2 messages arrive.

def callback(response: dict, new_messages: list):
    messages = response.get('Messages', [])
    new_messages.extend(messages)
    if len(new_messages) >= 2:
        raise SystemExit

new_messages = []
proton.event_polling(callback, new_messages)
print(new_messages)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

protonmail_api_client-1.8.0.tar.gz (60.0 kB view details)

Uploaded Source

Built Distribution

protonmail_api_client-1.8.0-py3-none-any.whl (48.3 kB view details)

Uploaded Python 3

File details

Details for the file protonmail_api_client-1.8.0.tar.gz.

File metadata

  • Download URL: protonmail_api_client-1.8.0.tar.gz
  • Upload date:
  • Size: 60.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for protonmail_api_client-1.8.0.tar.gz
Algorithm Hash digest
SHA256 af9e7856dcbc744ef712b69fbf90eb7cb0f7be58092d3089653ba150ad46b991
MD5 32bde3211b19ee4a3f0a5e1e2931e94f
BLAKE2b-256 404227ff918cbadb05a8fe45c5d925a4c0e178f9c4d966a2b5d2469fcc61a19c

See more details on using hashes here.

File details

Details for the file protonmail_api_client-1.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for protonmail_api_client-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f92e2444047327dca36fde358c9ca37516f0165d39b90d86165c293329ad1d6c
MD5 e3af9e1b40f5a66c1a1b929019d69ef5
BLAKE2b-256 98c4124954d31b9dbe200847d5028b3d605d32c8934ba4353e9d21d27db9358e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page