#๐Ÿ”’ Make sure to encode the file content in base64

8 messages ยท Page 1 of 1 (latest)

next ermine
#

Hello all.

read_and_send_emails.py:

from python_for_outlook.outlook import create_attachment

def send_emails(headers, attachments):
  file_path = ''
  df = ...
  endpoint = f'{MS_GRAPH_BASE_URL}/me/sendMail'

  for row in df.iterrows():
    content = '...'
    msg = {
      'message': {
        'subject': row['subject'],
        'body': {
          'contentType': 'HTML',
          'content': content
        },
        'toRecipients': [
        {
       'emailAddress': {
  'address': row['to']
       }
     }
       ],
      'ccRecipients': [
        {
  'emailAddress': {
    'address': row['cc']
  }
}
        ],
        'importance': 'high',
        'attachments': attachments
      }
    }
    response = httpx.post(endpoint, headers=headers, json=msg)

    if response.status_code != 202:
  raise Exception(f'Failed to send email: {response.text}')

def main():
    dir_attachments = Path('./python_for_onedrive/Downloads')
    attachments = [
  create_attachment(attachment)
  for attachment in dir_attachments.iterdir()
  if attachment.is_file()
    ]
    send_emails(headers, attachments)```
`outlook.py`:```
def get_mime_type(file_path):
  mime_type, _ = mimetypes.guess_type(file_path)

  return mime_type or "application/octet-stream"

def create_attachment(file_path):
  with open(file_path, 'rb') as file:
    content = file.read()
    encoded_content = base64.b64encode(content).decode('utf-8')

  return {
    '@odata.type': '#microsoft.graph.fileAttachment',
    'name': os.path.basename(file_path),
    'contentType': get_mime_type(file_path),
    'contentBytes': encoded_content
  }```
tribal jasperBOT
#

@next ermine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

next ermine
#

The following is the terminal output upon running the read_and_send_emails.py program with the Start Debugging button: {"error":{"code":"RequestBodyRead","message":"An unexpected instance annotation name '\n\t\tTake the binary content (likely read from a file) and encodes\n\t\tit into Base64 format.\n\n\t\tMake sure to encode the file content in base64 before assigning\n\t\tit to contentBytes.contentBytes' was found when reading from the JSON reader, In OData, Instance annotation name must start with @."}}

#

How could the library or libraries I'm using be the cause of this obstacle?

cloud rampart
#

can you run your code through ruff format, it's all wiggly

#

it's also missing imports

tribal jasperBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.