themartorana / python-postmark

Postmark library for Python 2.6 and greater
http://davemartorana.com
MIT License
199 stars 83 forks source link

Attached local PDF files #110

Open walmins opened 2 years ago

walmins commented 2 years ago

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 commented 2 years ago

Are you getting an error of some kind?

walmins commented 2 years ago

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': 'toto@toto.local', 'name': 'Toto Lolo'}
  message = PMMail(
      sender=pmSender,
      # to=email,
      to='mymail@mail.local',
      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 commented 2 years ago

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

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

nicholasserra commented 2 years ago

Also take a peek at this one https://github.com/themartorana/python-postmark/issues/93