How do I to attach an image to Send an Email using a webhook?

Describe the question

What I want to know is how do I to attach an image to Send an Email using a webhook? I tried sending an email using a webhook being called from Python, and it worked perfectly with the expressions an everything, the workflow was a webhook and then its connected to the Send Email node, but I don’t know how to attach a file. I even tried to ask to ChatGPT and it suggested me some things, but they are a little bit outdated I think, after some hours of trying it recommend me to make a function to decode from base64 the file, however it didn’t work. If someone could help me or show me a better way to do it, go ahead, I’m open to ideas.

Please share your workflow

This is the last workflow I did:

This is the last code that I send to call the webhook and send the email:

import requests
import datetime
import base64

# Set the webhook URL from the n8n Webhook node
webhook_url = "http://mywebhook.com"

# Set the email data you want to send
email_data = {
    "to_email": ['[email protected]', '[email protected]'],
    "cc_email": '[email protected]',
    "subject": "Cool Subject",
    "text": f"Stuff made, today date: {datetime.datetime.now().strftime('%d-%m-%Y')}."
}

# Set the path to the file you want to upload
file_path = "/path/to/attachment/obreros_con_errores.csv"

# Read the file data and prepare the payload
with open(file_path, "rb") as file:
    file_data = file.read()
    encoded_file_data = base64.b64encode(file_data).decode("utf-8")

email_data["file_data"] = encoded_file_data
email_data["file_name"] = file_path.split("/")[-1]

# Send a POST request to the webhook URL with the email data
response = requests.post(webhook_url, json=email_data)

# Print the response
if response.status_code == 200:
    print("Email sent successfully!")
else:
    print(f"Error: {response.status_code}")
    print(response.text)

Hi @Youngermaster, welcome to the community :tada:

I’m sorry to hear you’re having trouble. Let’s try to sort this :slight_smile:

Are you already receiving a binary object in your n8n webhook node? If not, could you enable the Binary Data option on your webhook node like below? This should get you a binary object:

You can then give this binary object a proper file name and attach it to your email using a workflow like so:

Hope this helps!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.