
Security News
Knip Hits 500 Releases with v5.62.0, Improving TypeScript Config Detection and Plugin Integrations
Knip hits 500 releases with v5.62.0, refining TypeScript config detection and updating plugins as monthly npm downloads approach 12M.
protonmail-api-client
Advanced tools
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.
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.
pip install protonmail-api-client
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", "Name of recipient <to3@outlook.com>"],
cc=["cc1@proton.me", "cc2@gmail.com", "Name of recipient <cc3@outlook.com>"],
bcc=["bcc1@proton.me", "bcc2@gmail.com", "Name of recipient <bcc3@outlook.com>"],
subject="My first message",
body=html, # html or just text
attachments=[img_attachment, pdf_attachment],
external_id="some-message-id-header-if-you-want-to-specify",
in_reply_to="message-id-of-the-mail-to-reply-to",
)
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. Polling ends in 3 cases:
None
.SystemExit
exception.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)
Instructions to solve CAPTCHA:
cv2
and NumPy
. Sometimes CantSolveImageCaptcha
exception may occur, it means you encountered a complex image, just try to log in again.proton.login(username, password, captcha_config=CaptchaConfig(type=CaptchaConfig.CaptchaType.MANUAL))
F12
)CTRL + E
)init
request in the "Network" tab and open it.Instructions to avoid CAPTCHA:
save_session
and load_session
methods.protonmail-api-client
) will be without CAPTCHA.cookies
from the browser:
F12
).CTRL + E
).auth
request in the "Network" tab and open it.Set-Cookie
items in the "Response Headers" list.REFRESH-*
, AUTH-*
.x-pm-uid
is the part after AUTH-
).proton = ProtonMail()
proton.session.headers['x-pm-uid'] = 'lgfbju2dxc1234567890mrf3tqmqfhv6q' # This is the part after `AUTH-`
proton.session.cookies['AUTH-lgfbju2dxc1234567890mrf3tqmqfhv6q'] = 'qr4uci1234567890anafsku8dd34vkwq'
proton.session.cookies['REFRESH-lgfbju2dxc1234567890mrf3tqmqfhv6q'] = '%7B%22ResponseType%22%3A%22token%22%2C%22ClientID%22%3A%22WebAccount%22%2C%22GrantType%22%3A%22refresh_token%22%2C%22RefreshToken%22%3A%22ceo5gp1234567890fghuinsxxtgmpvdduxg%22%2C%22UID%22%3A%22lgfbju2dxc1234567890mrf3tqmqfhv6q%22%7D'
password = 'YourPassword123'
proton._parse_info_after_login(password)
proton.save_session('session.pickle')
FAQs
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.
We found that protonmail-api-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Knip hits 500 releases with v5.62.0, refining TypeScript config detection and updating plugins as monthly npm downloads approach 12M.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.