<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright 2012 <NAME> <<EMAIL>>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = """
---
author: "<NAME> (@dagwieers)" 
module: mail
short_description: Send an email
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 him perform his
    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.
version_added: "0.8"
options:
  from:
    description:
      - The email-address the mail is sent from. May contain address and phrase.
    default: root
    required: false
  to:
    description:
      - The email-address(es) the mail is being sent to. This is
        a comma-separated list, which may contain address and phrase portions.
    default: root
    required: false
  cc:
    description:
      - The email-address(es) the mail is being copied to. This is
        a comma-separated list, which may contain address and phrase portions.
    required: false
  bcc:
    description:
      - The email-address(es) the mail is being 'blind' copied to. This is
        a comma-separated list, which may contain address and phrase portions.
    required: false
  subject:
    description:
      - The subject of the email being sent.
    required: true
  body:
    description:
      - The body of the email being sent.
    default: $subject
    required: false
  username:
    description:
      - If SMTP requires username
    default: null
    required: false
    version_added: "1.9"
  password:
    description:
      - If SMTP requires password
    default: null
    required: false
    version_added: "1.9"
  host:
    description:
      - The mail server
    default: 'localhost'
    required: false
  port:
    description:
      - The mail server port
    default: '25'
    required: false
    version_added: "1.0"
  attach:
    description:
      - A space-separated list of pathnames of files to attach to the message.
        Attached files will have their content-type set to C(application/octet-stream).
    default: null
    required: false
    version_added: "1.0"
  headers:
    description:
      - A vertical-bar-separated list of headers which should be added to the message.
        Each individual header is specified as C(header=value) (see example below).
    default: null
    required: false
    version_added: "1.0"
  charset:
    description:
      - The character set of email being sent
    default: 'us-ascii'
    required: false
  subtype:
    description:
      - The minor mime type, can be either text or html. The major type is always text.
    default: 'plain'
    required: false
    version_added: "2.0"
"""

EXAMPLES = '''
# Example playbook sending mail to root
- local_action: mail subject='System {{ ansible_hostname }} has been successfully provisioned.'

# Sending an e-mail using Gmail SMTP servers
- local_action: mail
                host='smtp.gmail.com'
                port=587
                username=<EMAIL>
                password='<PASSWORD>'
                to="<NAME> <<EMAIL>>"
                subject='Ansible-report'
                body='System {{ ansible_hostname }} has been successfully provisioned.'

# Send e-mail to a bunch of users, attaching files
- local_action: mail
                host='127.0.0.1'
                port=2025
                subject="Ansible-report"
                body="Hello, this is an e-mail. I hope you like it ;-)"
                from="<EMAIL> (<NAME>)"
                to="<NAME> <<EMAIL>>, <NAME> <<EMAIL>>"
                cc="<NAME> <root@localhost>"
                attach="/etc/group /tmp/pavatar2.png"
                headers=Reply-To=<EMAIL>|X-Special="Something or other"
                charset=utf8
# Sending an e-mail using the remote machine, not the Ansible controller node
- mail:
    host='localhost'
    port=25
    to="<NAME> <<EMAIL>>"
    subject='Ansible-report'
    body='System {{ ansible_hostname }} has been successfully provisioned.'
'''

import os
import sys
import smtplib
import ssl

try:
    from email import encoders
    import email.utils
    from email.utils import parseaddr, formataddr
    from email.mime.base import MIMEBase
    from mail.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
except ImportError:
    from email import Encoders as encoders
    import email.Utils
    from email.Utils import parseaddr, formataddr
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

def main():

    module = AnsibleModule(
        argument_spec = dict(
            username = dict(default=None),
            password = dict(default=None),
            host = dict(default='localhost'),
            port = dict(default='25'),
            sender = dict(default='root', aliases=['from']),
            to = dict(default='root', aliases=['recipients']),
            cc = dict(default=None),
            bcc = dict(default=None),
            subject = dict(required=True, aliases=['msg']),
            body = dict(default=None),
            attach = dict(default=None),
            headers = dict(default=None),
            charset = dict(default='us-ascii'),
            subtype = dict(default='plain')
        )
    )

    username = module.params.get('username')
    password = module.params.get('password')
    host = module.params.get('host')
    port = module.params.get('port')
    sender = module.params.get('sender')
    recipients = module.params.get('to')
    copies = module.params.get('cc')
    blindcopies = module.params.get('bcc')<commit_msg>Do not leak mail password by error<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright 2012 <NAME> <<EMAIL>>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = """
---
author: "<NAME> (@dagwieers)" 
module: mail
short_description: Send an email
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 him perform his
    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.
version_added: "0.8"
options:
  from:
    description:
      - The email-address the mail is sent from. May contain address and phrase.
    default: root
    required: false
  to:
    description:
      - The email-address(es) the mail is being sent to. This is
        a comma-separated list, which may contain address and phrase portions.
    default: root
    required: false
  cc:
    description:
      - The email-address(es) the mail is being copied to. This is
        a comma-separated list, which may contain address and phrase portions.
    required: false
  bcc:
    description:
      - The email-address(es) the mail is being 'blind' copied to. This is
        a comma-separated list, which may contain address and phrase portions.
    required: false
  subject:
    description:
      - The subject of the email being sent.
    required: true
  body:
    description:
      - The body of the email being sent.
    default: $subject
    required: false
  username:
    description:
      - If SMTP requires username
    default: null
    required: false
    version_added: "1.9"
  password:
    description:
      - If SMTP requires password
    default: null
    required: false
    version_added: "1.9"
  host:
    description:
      - The mail server
    default: 'localhost'
    required: false
  port:
    description:
      - The mail server port
    default: '25'
    required: false
    version_added: "1.0"
  attach:
    description:
      - A space-separated list of pathnames of files to attach to the message.
        Attached files will have their content-type set to C(application/octet-stream).
    default: null
    required: false
    version_added: "1.0"
  headers:
    description:
      - A vertical-bar-separated list of headers which should be added to the message.
        Each individual header is specified as C(header=value) (see example below).
    default: null
    required: false
    version_added: "1.0"
  charset:
    description:
      - The character set of email being sent
    default: 'us-ascii'
    required: false
  subtype:
    description:
      - The minor mime type, can be either text or html. The major type is always text.
    default: 'plain'
    required: false
    version_added: "2.0"
"""

EXAMPLES = '''
# Example playbook sending mail to root
- local_action: mail subject='System {{ ansible_hostname }} has been successfully provisioned.'

# Sending an e-mail using Gmail SMTP servers
- local_action: mail
                host='smtp.gmail.com'
                port=587
                username=<EMAIL>
                password='<PASSWORD>'
                to="<NAME> <<EMAIL>>"
                subject='Ansible-report'
                body='System {{ ansible_hostname }} has been successfully provisioned.'

# Send e-mail to a bunch of users, attaching files
- local_action: mail
                host='127.0.0.1'
                port=2025
                subject="Ansible-report"
                body="Hello, this is an e-mail. I hope you like it ;-)"
                from="<EMAIL> (<NAME>)"
                to="<NAME> <<EMAIL>>, <NAME> <<EMAIL>>"
                cc="Charlie Root <root@localhost>"
                attach="/etc/group /tmp/pavatar2.png"
                headers=Reply-To=<EMAIL>|X-Special="Something or other"
                charset=utf8
# Sending an e-mail using the remote machine, not the Ansible controller node
- mail:
    host='localhost'
    port=25
    to="<NAME> <<EMAIL>>"
    subject='Ansible-report'
    body='System {{ ansible_hostname }} has been successfully provisioned.'
'''

import os
import sys
import smtplib
import ssl

try:
    from email import encoders
    import email.utils
    from email.utils import parseaddr, formataddr
    from email.mime.base import MIMEBase
    from mail.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
except ImportError:
    from email import Encoders as encoders
    import email.Utils
    from email.Utils import parseaddr, formataddr
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

def main():

    module = AnsibleModule(
        argument_spec = dict(
            username = dict(default=None),
            password = dict(default=None, no_log=True),
            host = dict(default='localhost'),
            port = dict(default='25'),
            sender = dict(default='root', aliases=['from']),
            to = dict(default='root', aliases=['recipients']),
            cc = dict(default=None),
            bcc = dict(default=None),
            subject = dict(required=True, aliases=['msg']),
            body = dict(default=None),
            attach = dict(default=None),
            headers = dict(default=None),
            charset = dict(default='us-ascii'),
            subtype = dict(default='plain')
        )
    )

    username = module.params.get('username')
    password = <PASSWORD>('password')
    host = module.params.get('host')
    port = module.params.get('port')
    sender = module.params.get('sender')
    recipients = module.params.get('to')
    copies = module.params.get('cc')
    blindcopies = module.params.get('bcc')
{'fraction_non_alphanumeric': 0.08829372378780075, 'fraction_numerical': 0.0060334044588330514, 'mean_word_length': 2.5948161861941283, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '246190', 'n_tokens_mistral': 3821, 'n_tokens_neox': 3710, 'n_words': 1435}