opulentfox-29 / 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.
GNU General Public License v3.0
15 stars 5 forks source link

Add attachment #18

Closed contactsystemtt closed 9 hours ago

contactsystemtt commented 5 days ago

I want to add an attachment when sending a mail.... I tried the following code

# Read the PDF file
    with open(pdf_path, 'rb') as file:
        pdf_content = file.read()

attachment_info = {
        'name': Path(pdf_path).name,  # Extract file name
        'content': pdf_content,
        'type': 'application/pdf',  
    }
new_message = proton.create_message(
        recipients=recipients,
        subject=subject,
        body=body,
        attachments=[attachment_info]  # Attach the attachment here
    )
opulentfox-29 commented 5 days ago

I haven't implemented the ability to send attachments yet. But if you want to insert a photo, you can paste a link to it, or base64 encode it, although not all mails will allow you to see the photo (protonmail will show it, gmail will not)

from base64 import b64encode

with open('image.png', 'rb') as f:
    img = f.read()

html = f"""
<div>
    <img src="https://raw.githubusercontent.com/opulentfox-29/protonmail-api-client/master/assets/1.png" height="150" width="300" alt="image.png">
    <br/>
    <img src="data:image/png;base64, {b64encode(img).decode()}" height="150" width="300" alt="image.png">
</div>
"""

new_message = proton.create_message(
    recipients=['to1@proton.me'],
    subject='hello',
    body=html,
)
message = proton.send_message(new_message, is_html=True)
opulentfox-29 commented 9 hours ago

You can now send attachments and insert pictures into letter.

# 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)