Skip to Content
ReferenceActionsSMTP

SMTP Action

The smtp action delivers mail to an SMTP server. It is built for measuring and exercising delivery rather than for sending hand-written notifications: the message body is generated, and its size is set with length.

Basic Syntax

An SMTP step names the server, the envelope addresses, and what to send.

steps: - name: Send a probe mail uses: smtp with: addr: "localhost:2525" from: "sender@example.com" to: "recipient@example.com" subject: "Delivery probe" session: 1 message: 1 length: 500 test: res.code == 0 && res.sent > 0

Parameters

The fields below describe the delivery. All of them accept template expressions.

ParameterTypeRequiredDefaultDescription
addrStringYes-SMTP server as host:port
fromStringYes-Envelope sender
toStringYes-Envelope recipient. Several recipients are separated by commas, such as a@example.com, b@example.com
subjectStringNo""Subject line
myhostnameStringNoThe machine’s host name, or localhost if it is unknownHostname sent in the EHLO / HELO command. Many servers refuse a client that greets with localhost, so set a name the server accepts
sessionIntegerNo1Number of SMTP sessions to open at the same time
messageIntegerNo1Messages to send in total, divided among the sessions
lengthIntegerNo0Number of * characters appended to the generated body
starttlsStringNoautoauto switches to TLS with STARTTLS when the server offers it, required fails a session whose server does not, and off never uses it
insecure_skip_tlsBooleanNofalseAccept any certificate after STARTTLS, such as a self-signed one

After STARTTLS the server’s certificate is verified against the host in addr, so a self-signed or mismatched one fails the session unless insecure_skip_tls is set. A connection that starts in TLS, as on port 465, is not supported. There are no parameters for authentication, CC/BCC, a custom body or HTML. To include a report in the run output, use the step’s echo.

The generated message has From, To, Date and Subject headers and a body of This is a test mail. followed by length * characters, broken into lines of 80.

Response Object

After the step, res reports how much was delivered.

FieldTypeDescription
res.codeInteger0 when no session failed and at least one message was delivered
res.sentIntegerMessages the server accepted, including those a session delivered before it failed
res.failedIntegerSessions that failed
res.totalIntegerMessages attempted, which is message
res.errorStringError message, when delivery failed
res.maildataStringThe generated message, when it is text
res.filepathStringPath to the generated message, when it is binary
rt.durationStringTime the delivery took, such as "3.4ms"
rt.secFloatTime the delivery took in seconds
statusIntegerSame as res.code

A server that refuses the mail, for example with 550 to RCPT TO, has answered, so the step goes on to its test with res.code set to 1 and the reply in res.error. When no session can reach the server at all, the step ends with an error.

SMTP Examples

The examples below send generated messages and then read the counts back from the step outputs.

Several Sessions and Messages

message is the total, divided among the sessions: here one session delivers 2 messages and the other 1.

steps: - name: Deliver 3 messages over 2 sessions id: bulk uses: smtp with: addr: "{{vars.smtp_addr}}" from: "{{vars.from_addr}}" to: "{{vars.to_addr}}" subject: "Bulk delivery test" myhostname: probe-client.local session: 2 message: 3 length: 750 test: res.code == 0 && res.sent == 3 outputs: sent: res.sent elapsed: rt.duration

Reporting the Result

The counts captured as outputs can be printed by a later step.

- name: Delivery summary uses: hello echo: | Sent: {{outputs.bulk.sent}} Took: {{outputs.bulk.elapsed}}
Updated at