← All ivy-nodes
IVYXSTUDIO · IVY NODE
E

Email Provider

ivy.node.email-provider · v0.2.0

ivyx✓

Sends an email message through an SMTP server and returns the recipients it went to. TLS and login are optional, so it reaches a local relay as well as a mail provider, and a failed send fails the step.

#SMTP#Email Sending#Email Message

Inputs

FieldTypeDescription
smtp_serverrequiredstringSMTP host.
smtp_portrequiredintegerSMTP port, such as 587, or 1025 for a local relay.
email_messagerequiredNot declaredThe message to send, from email-builder.
use_tlsbooleanUpgrade the connection with STARTTLS before sending.
adminstringLogin name; leave it out for a relay that takes no login.
passwordstringLogin password; reference the stored secret mail-password rather than writing it.

Outputs

FieldTypeDescription
sentrequiredbooleanTrue once the server accepted the message.
recipientsrequiredarrayThe addresses the message was sent to.

Source

python

inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
smtp_server = inp["smtp_server"]
smtp_port = int(inp["smtp_port"])
email_message = inp["email_message"]
use_tls = inp.get("use_tls", True)
admin = inp.get("admin")
password = inp.get("password")

import smtplib

# Send, and fail when sending fails. The inherited cell caught every error,
# printed it and returned normally, so a rejected login or a closed port read
# as a sent mail. TLS and login are optional so a local relay (mailpit, a
# corporate relay on 25) can be reached.
with smtplib.SMTP(smtp_server, smtp_port, timeout=30) as server:
    if use_tls:
        server.starttls()
    if admin and password:
        server.login(admin, password)
    refused = server.send_message(email_message)

recipients = [a.strip() for a in str(email_message.get("To", "")).split(",") if a.strip()]
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["sent"] = True
out["recipients"] = [r for r in recipients if r not in (refused or {})]

Tests

Requires: python:3.9

  • local-relay

    A local relay with no TLS and no login accepts the message.

  • closed-port

    A port nothing listens on fails the step instead of reporting a send.