Need help: Send email with SMTP with Python

Describe the problem/error/question

I have a system to receive emails with IMAP. However, I need to have the ability to reply (as a thread). This is not natively supported in the SEND EMAIL node. Hence, my idea about running a python script in the CODE node.

What is the error message (if any)?

I get an error:

1 item
success
message
false[Errno 26] Operation in progress

Please share your workflow

import smtplib
from email.mime.text import MIMEText

def send_email(sender, recipient, subject, body):
    try:
        # Create the email message
        msg = MIMEText(body)
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = recipient

        # Connect to the SMTP server
        smtp_server = "send.one.com"
        smtp_port = 465
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()
        server.login("[email protected]", "passwordreplaced")

        # Send the email
        server.send_message(msg)

        # Close the SMTP server connection
        server.quit()

        return [{
            "success": True,
            "message": "Email sent successfully."
        }]

    except Exception as e:
        return [{
            "success": False,
            "message": str(e)
        }]

# Check if _input.json is None
if _input.json is None:
    # Set default values for input data
    sender = "[email protected]"
    recipient = "[email protected]"
    subject = "Test Email"
    body = "This is a test email from N8N."
else:
    # Retrieve the input data from the Code node
    sender = _input.json.get("sender", "[email protected]")
    recipient = _input.json.get("recipient", "[email protected]")
    subject = _input.json.get("subject", "Test Email")
    body = _input.json.get("body", "This is a test email from N8N.")

# Call the send_email function and return the result
return send_email(sender, recipient, subject, body)

Does anyone have an idea of how to fix this?

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

n8n version: latest
Database (default: SQLite): SQLite
n8n EXECUTIONS_PROCESS setting (default: own, main): main
Running n8n via (Docker, npm, n8n cloud, desktop app): Docker (both through Easypanel.io)
Operating system: Macbook

hello @harroi

if you have a mailbox in Google/Outlook, there is a dedicated node for that task

It is custom. Meaning not Gmail or any other standard providers.

It looks like the connect is failing when trying to send, it may be worth looking into what the Python error means to see anything jumps out.