How to attach files in Python emails using EmailDelivery API?

I’m stuck trying to add a file attachment to an email using the EmailDelivery API in Python. Their docs say I must use multipart/form-data encoding for attachments. Here’s what I’ve tried:

import requests

API_URL = 'https://api.emaildelivery.com/v3/send'
API_KEY = 'mykey123'

def send_email_with_attachment(file_path):
    with open(file_path, 'rb') as file:
        response = requests.post(
            API_URL,
            auth=('api', API_KEY),
            data={
                'subject': 'Test Email',
                'from': '[email protected]',
                'to': '[email protected]',
                'text': 'Email content',
                'html': '<p>Email content</p>',
                'attachment': file
            },
            headers={'Content-Type': 'multipart/form-data'}
        )
    return response

result = send_email_with_attachment('/path/to/document.pdf')
print(result.status_code)

But I’m getting a 400 Bad Request error. How can I make sure I’m using multipart/form-data correctly and attaching the file properly? Any help would be great!

I’ve had similar issues with EmailDelivery API before. The problem is likely in how you’re handling the file attachment. Instead of passing the file directly in the ‘data’ parameter, you should use the ‘files’ parameter in requests.post(), which automatically handles multipart/form-data encoding.

Here’s a modified version:

import requests

API_URL = 'https://api.emaildelivery.com/v3/send'
API_KEY = 'mykey123'

def send_email_with_attachment(file_path):
    with open(file_path, 'rb') as file:
        files = {'attachment': file}
        data = {
            'subject': 'Test Email',
            'from': '[email protected]',
            'to': '[email protected]',
            'text': 'Email content',
            'html': '<p>Email content</p>'
        }
        response = requests.post(
            API_URL,
            auth=('api', API_KEY),
            data=data,
            files=files
        )
    return response

result = send_email_with_attachment('/path/to/document.pdf')
print(result.status_code)

This approach lets the requests library manage the multipart/form-data encoding automatically, so you don’t need to set the Content-Type header manually. Try this method and see if it resolves your issue.

hey, i had similar trouble before. try using the ‘files’ parameter in requests.post() instead of putting the file in ‘data’. it handles the multipart/form-data encoding automatically. something like this:

files = {‘attachment’: open(‘/path/to/document.pdf’, ‘rb’)}
response = requests.post(API_URL, auth=(‘api’, API_KEY), data=your_data, files=files)

hope that helps!

I’ve encountered this issue before with the EmailDelivery API. The key is to use the ‘files’ parameter in requests.post() correctly. Here’s a simplified approach that should work:

import requests

API_URL = ‘https://api.emaildelivery.com/v3/send
API_KEY = ‘mykey123’

def send_email_with_attachment(file_path):
with open(file_path, ‘rb’) as file:
files = {‘attachment’: (file.name, file, ‘application/octet-stream’)}
data = {
‘subject’: ‘Test Email’,
‘from’: ‘[email protected]’,
‘to’: ‘[email protected]’,
‘text’: ‘Email content’
}
response = requests.post(API_URL, auth=(‘api’, API_KEY), data=data, files=files)
return response

This method explicitly sets the file’s MIME type, which can help avoid issues. Also, ensure your API_KEY is correct and that you have the necessary permissions to send attachments.