0

I'm working in python creating a script that sends email message transcriptions from a chatbot.

I've contacted Send Grid and they said it is likely something wrong with my header. I've tried a bunch of different things, but none have worked or make the situation worse.

What is odd is the email sends with no problem, I just get the 406 error, which is causing some headaches for the client.

Here is the header I'm including.

    the_message = "Transcript text"
    
    headers = {
        'Authorization': f'Bearer BEARER_TOKEN',
        'Content-type': 'application/json', 
        'Accept': 'application/html'
    }

    emailData = {
        'personalizations': [{'to': [{'email': param1 }]}],
        'from': {'email': 'EMAIL@EMAIL.com'},
        'subject': param6,
        'content': [{'type': 'text/plain', 'value': the_message}],
    }
    
    try:
        response = requests.post(sendGridAPI, json=emailData, headers=headers)
        #print(response)
        if response.status_code == 202:
            return {
                'statusCode': 200,
                'body': 'Email sent successfully!',
            }
        else:
            return {
                'statusCode': response.status_code,
                'body': f'Failed to send email. Status: {response.status_code}',
            }
    except Exception as error:
        return {
            'statusCode': 500,
            'body': f'Error occurred while sending email: {str(error)}',
        }

Has anyone encountered this before?

I tried changing the Content-Type and the Accept values, which either resulted in a 400 error or stayed a 406 error. I tried changing the text I sent via email to be UTF-8. None of that worked. I think the problem is in the header I'm constructing, but I'm at a loss of how to configure it.

0 Answers0