community.general.mail (8.5.0) — module

Send an email

Authors: Dag Wieers (@dagwieers)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

This module is useful for sending emails from playbooks.

One may wonder why automate sending emails? In complex environments there are from time to time processes that cannot be automated, either because you lack the authority to make it so, or because not everyone agrees to a common approach.

If you cannot automate a specific step, but the step is non-blocking, sending out an email to the responsible party to make them perform their part of the bargain is an elegant way to put the responsibility in someone else's lap.

Of course sending out a mail can be equally useful as a way to notify one or more people in a team that a specific action has been (successfully) taken.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Example playbook sending mail to root
  community.general.mail:
    subject: System {{ ansible_hostname }} has been successfully provisioned.
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Sending an e-mail using Gmail SMTP servers
  community.general.mail:
    host: smtp.gmail.com
    port: 587
    username: username@gmail.com
    password: mysecret
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Send e-mail to a bunch of users, attaching files
  community.general.mail:
    host: 127.0.0.1
    port: 2025
    subject: Ansible-report
    body: Hello, this is an e-mail. I hope you like it ;-)
    from: jane@example.net (Jane Jolie)
    to:
    - John Doe <j.d@example.org>
    - Suzie Something <sue@example.com>
    cc: Charlie Root <root@localhost>
    attach:
    - /etc/group
    - /tmp/avatar2.png
    headers:
    - Reply-To=john@example.com
    - X-Special="Something or other"
    charset: us-ascii
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Sending an e-mail using the remote machine, not the Ansible controller node
  community.general.mail:
    host: localhost
    port: 25
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Sending an e-mail using Legacy SSL to the remote machine
  community.general.mail:
    host: localhost
    port: 25
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: always
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Sending an e-mail using StartTLS to the remote machine
  community.general.mail:
    host: localhost
    port: 25
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: starttls
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Sending an e-mail using StartTLS, remote server, custom EHLO, and timeout of 10 seconds
  community.general.mail:
    host: some.smtp.host.tld
    port: 25
    timeout: 10
    ehlohost: my-resolvable-hostname.tld
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: starttls

Inputs

    
cc:
    default: []
    description:
    - The email-address(es) the mail is being copied to.
    - This is a list, which may contain address and phrase portions.
    elements: str
    type: list

to:
    aliases:
    - recipients
    default: root
    description:
    - The email-address(es) the mail is being sent to.
    - This is a list, which may contain address and phrase portions.
    elements: str
    type: list

bcc:
    default: []
    description:
    - The email-address(es) the mail is being 'blind' copied to.
    - This is a list, which may contain address and phrase portions.
    elements: str
    type: list

body:
    description:
    - The body of the email being sent.
    type: str

host:
    default: localhost
    description:
    - The mail server.
    type: str

port:
    default: 25
    description:
    - The mail server port.
    - This must be a valid integer between 1 and 65534
    type: int

attach:
    default: []
    description:
    - A list of pathnames of files to attach to the message.
    - Attached files will have their content-type set to C(application/octet-stream).
    elements: path
    type: list

secure:
    choices:
    - always
    - never
    - starttls
    - try
    default: try
    description:
    - If V(always), the connection will only send email if the connection is Encrypted.
      If the server doesn't accept the encrypted connection it will fail.
    - If V(try), the connection will attempt to setup a secure SSL/TLS session, before
      trying to send.
    - If V(never), the connection will not attempt to setup a secure SSL/TLS session,
      before sending
    - If V(starttls), the connection will try to upgrade to a secure SSL/TLS connection,
      before sending. If it is unable to do so it will fail.
    type: str

sender:
    aliases:
    - from
    default: root
    description:
    - The email-address the mail is sent from. May contain address and phrase.
    type: str

charset:
    default: utf-8
    description:
    - The character set of email being sent.
    type: str

headers:
    default: []
    description:
    - A list of headers which should be added to the message.
    - Each individual header is specified as C(header=value) (see example below).
    elements: str
    type: list

subject:
    aliases:
    - msg
    description:
    - The subject of the email being sent.
    required: true
    type: str

subtype:
    choices:
    - html
    - plain
    default: plain
    description:
    - The minor mime type, can be either V(plain) or V(html).
    - The major type is always V(text).
    type: str

timeout:
    default: 20
    description:
    - Sets the timeout in seconds for connection attempts.
    type: int

ehlohost:
    description:
    - Allows for manual specification of host for EHLO.
    type: str
    version_added: 3.8.0
    version_added_collection: community.general

password:
    description:
    - If SMTP requires password.
    type: str

username:
    description:
    - If SMTP requires username.
    type: str

message_id_domain:
    default: ansible
    description:
    - The domain name to use for the L(Message-ID header, https://en.wikipedia.org/wiki/Message-ID).
    - Note that this is only available on Python 3+. On Python 2, this value will be ignored.
    type: str
    version_added: 8.2.0
    version_added_collection: community.general