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)