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

Attached local PDF files #110

Open
walmins opened this issue Apr 8, 2022 · 4 comments
Open

Attached local PDF files #110

walmins opened this issue Apr 8, 2022 · 4 comments

Comments

@walmins
Copy link

walmins commented Apr 8, 2022

Hello,
I try to send local PDF files but isn't work,

          with open(src_pdf, 'rb') as f:
            pdf_file = f.read()
          attachments = [
            ("my_pdf_file.pdf", pdf_file, 'application/pdf')
            ]
          pm = PMMail(
            to=user_mail, 
            bcc=pmBCC,
            sender=pmSender,
            template_id=email_template_id,
            template_model=template_model,
            api_key=pmApiKey,
            attachments=attachments,
            tag=pmTag)
          pm.send()

do you know what is wrong?
Thanks

@nicholasserra
Copy link
Collaborator

Are you getting an error of some kind?

@walmins
Copy link
Author

walmins commented Apr 8, 2022

yes, sure,

/app # ./message_send.py 
Traceback (most recent call last):
  File "/app/./message_send.py", line 33, in <module>
    message.send()
  File "/usr/local/lib/python3.10/site-packages/postmark/core.py", line 525, in send
    json.dumps(json_message, cls=PMJSONEncoder).encode('utf8'),
  File "/usr/local/lib/python3.10/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/local/lib/python3.10/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python3.10/site-packages/postmark/core.py", line 40, in default
    return super(PMJSONEncoder, self).default(o)
  File "/usr/local/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

my file:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import yaml
from postmark.core import PMMail
import os

# postmark setup
pmApiKey = os.environ['PM_API_KEY']
pmSender = os.environ['PM_SENDER']
pmBCC = os.environ['PM_BCC']
pmTag = os.environ['PM_TAG']
templateId = os.environ['TEMPLATE_ID']

src_pdf = "./sample.pdf"
with open(src_pdf, 'rb') as f:
  pdf_file = f.read()
attachments = [
    ("sample.pdf", pdf_file, 'application/pdf')
  ]
if os.path.exists(src_pdf) :
  template_model = {'year': '2022', 'month': '03', 'email': '[email protected]', 'name': 'Toto Lolo'}
  message = PMMail(
      sender=pmSender,
      # to=email,
      to='[email protected]',
      bcc=pmBCC,
      template_id=template_id,
      template_model=template_model,
      tag=pmTag,
      attachments=attachments,
      api_key=pmApiKey
      )
  message.send()

and here error in action
https://asciinema.org/a/SdXql5jIL8g2Hb8TbZnbCIKPo

@nicholasserra
Copy link
Collaborator

What happens when you do a normal read on the file, not a read-bytes?

with open(src_pdf, 'r') as f:

@nicholasserra
Copy link
Collaborator

Also take a peek at this one #93

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