Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attachment #18

Closed
contactsystemtt opened this issue Jul 2, 2024 · 2 comments
Closed

Add attachment #18

contactsystemtt opened this issue Jul 2, 2024 · 2 comments

Comments

@contactsystemtt
Copy link

contactsystemtt commented Jul 2, 2024

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
    )
@contactsystemtt contactsystemtt changed the title Add attachment with creating message Add attachment Jul 2, 2024
@opulentfox-29
Copy link
Owner

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
Copy link
Owner

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants