
    Vh=                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlm Z  d Z!e"dk(  r e!        yy)    )absolute_importdivisionprint_functiona  
author:
  - Dag Wieers (@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 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.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  sender:
    description:
      - The email-address the mail is sent from. May contain address and phrase.
    type: str
    default: root
    aliases: [from]
  to:
    description:
      - The email-address(es) the mail is being sent to.
      - This is a list, which may contain address and phrase portions.
    type: list
    elements: str
    default: root
    aliases: [recipients]
  cc:
    description:
      - The email-address(es) the mail is being copied to.
      - This is a list, which may contain address and phrase portions.
    type: list
    elements: str
    default: []
  bcc:
    description:
      - The email-address(es) the mail is being 'blind' copied to.
      - This is a list, which may contain address and phrase portions.
    type: list
    elements: str
    default: []
  subject:
    description:
      - The subject of the email being sent.
    required: true
    type: str
    aliases: [msg]
  body:
    description:
      - The body of the email being sent.
    type: str
  username:
    description:
      - If SMTP requires username.
    type: str
  password:
    description:
      - If SMTP requires password.
    type: str
  host:
    description:
      - The mail server.
    type: str
    default: localhost
  port:
    description:
      - The mail server port.
      - This must be a valid integer between V(1) and V(65534).
    type: int
    default: 25
  attach:
    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).
    type: list
    elements: path
    default: []
  headers:
    description:
      - A list of headers which should be added to the message.
      - Each individual header is specified as V(header=value) (see example below).
    type: list
    elements: str
    default: []
  charset:
    description:
      - The character set of email being sent.
    type: str
    default: utf-8
  subtype:
    description:
      - The minor mime type, can be either V(plain) or V(html).
      - The major type is always V(text).
    type: str
    choices: [html, plain]
    default: plain
  secure:
    description:
      - If V(always), the connection will only send email if the connection is Encrypted. If the server does not 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
    choices: [always, never, starttls, try]
    default: try
  timeout:
    description:
      - Sets the timeout in seconds for connection attempts.
    type: int
    default: 20
  ehlohost:
    description:
      - Allows for manual specification of host for EHLO.
    type: str
    version_added: 3.8.0
  message_id_domain:
    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
    default: ansible
    version_added: 8.2.0
a&	  
- name: Example playbook sending mail to root
  community.general.mail:
    subject: System {{ ansible_hostname }} has been successfully provisioned.
  delegate_to: localhost

- 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

- 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

- 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.

- 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

- 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

- 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
N)encoders)	parseaddr
formataddr
formatdate
make_msgid)MIMEBase)MIMEMultipart)MIMEText)Header)AnsibleModule)PY3)	to_nativec                     t        t        dfi dt        d      dt        dd      dt        dd	      d
t        dd	      dt        dd 	      dt        dddg      dt        dddgdg      dt        ddg       dt        ddg       dt        dddg      dt        d      dt        ddg       dt        ddg       d t        dd!	      d"t        dd#d$d#g%      d&t        dd'g d(%      d)t        dd*	      d+t        dd,	      ddgg-      } | j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d
      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	| j                  j                  d      }
| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d       }| j                  j                  d"      }| j                  j                  d&      }| j                  j                  d)      }| j                  d+   }d.}d/}t	        |      \  }}|s|
}	 |d0k7  rO	 t
        rt        j                  ||||1      }nt        j                  ||2      }|j                  ||      \  }}d}|sLt
        rt        j                   ||||1      }nt        j                   ||2      }|j                  ||      \  }}	 j%                          t'        |      d.kD  rZ|sX|d;v rTj)                  d<      r%	 |j+                          d}	 |j%                          n|d=k(  r| j                  d4d>|d6|?       |r@|r>j)                  d@      r	 |j-                  ||       n| j                  d4dD|d6|?       |s|r|r| j1                  dE       t3        |F      }t5        ||f      |dG<   t7        dH      |dI<   t9        |
|      |dJ<   	 t;        |K      |dL<   dN|_        |D ]q  }|jA                  dO      D cg c]  }|jC                          c}D ]?  }	 |jA                  dPd4      \  }}t        t9        ||            }|jE                  ||       A s dR|vr|jE                  dRdS       g } |	D cg c]  }|jC                          c}D ]  }!| jG                  t	        |!      d4          ! g }"|D cg c]  }|jC                          c}D ]B  }!|"jG                  t5        t	        |!                   | jG                  t	        |!      d4          D dTjI                  |"      |dU<   g }#|D cg c]  }|jC                          c}D ]B  }!|#jG                  t5        t	        |!                   | jG                  t	        |!      d4          D dTjI                  |#      |dV<   tK        |dWz   ||X      }$|jM                  |$       |D ]  }%	 tO        dYdZ      }$tQ        |%d[      5 }&|$jS                  |&jU                                d d d        tW        jX                  |$       |$jE                  d\d]tZ        j\                  j_                  |%      ^       |jM                  |$        |ja                         }'	 jc                  |te        |       |'      }(jg                          (r@|(D ](  })| j1                  d`|)da|(|)   d.   db|(|)   d4          * | ji                  dc|(d       | ji                  de|(d       y # t        j                  $ rH}|d3k(  r8| j                  d4d5|d6|d7t        |      t        j                         8       Y d }~d }~wt        $ r Y w xY w# t        j"                  $ rC}| j                  d4d9|d6|d7t        |      t        j                         8       Y d }~d }~ww xY w# t        j"                  $ rC}| j                  d4d:|d6|d7t        |      t        j                         8       Y d }~9d }~ww xY w# t        j"                  $ rC}| j                  d4d5|d6|d7t        |      t        j                         8       Y d }~Zd }~ww xY w# t        j"                  $ rC}| j                  d4d:|d6|d7t        |      t        j                         8       Y d }~d }~ww xY w# t        j.                  $ r | j                  d4dA|d6|dB?       Y yt        j"                  $ r | j                  d4dC|d6|?       Y w xY w# t<        $ r" t;               |dL<   | j1                  dM       Y tw xY wc c}w # t        $ r | j1                  dQ|z         Y dw xY wc c}w c c}w c c}w # 1 sw Y   xY w# t        $ r@}| j                  d4d_|%d7t        |      t        j                         8       Y d }~d }~ww xY w# t        $ rX}| j                  d4d`dTjI                  te        |             dat        |      t        j                         8       Y d }~d }~ww xY w)gNusernamestr)typepasswordT)r   no_loghost	localhost)r   defaultportint   ehlohostsenderrootfrom)r   r   aliasestolist
recipients)r   elementsr   r"   cc)r   r&   r   bccsubjectmsg)r   requiredr"   bodyattachpathheaderscharsetzutf-8subtypeplainhtml)r   r   choicessecuretry)alwaysneverstarttlsr6   timeout   message_id_domainansible)argument_specrequired_togetherr   Fr8   )r   r   local_hostnamer:   )r@   r:   r7      z(Unable to start an encrypted session to :z: )rcr*   	exceptionzUnable to Connect zHelo failed for host )r9   r6   STARTTLSr9   z"StartTLS is not offered on server )rC   r*   AUTHzAuthentication to z3 failed, please check your username and/or passwordz/No Suitable authentication method was found on z#No Authentication on the server at z1Username and Password was sent without encryption)_charsetFrom)	localtimeDateSubject)domainz
Message-IDzNThe Message-ID domain cannot be set on Python 2; the system's hostname is usedzMultipart message|=z%Skipping header '%s', unable to parsezX-MailerzAnsible mail modulez, ToCcz

)_subtyperG   applicationzoctet-streamrbzContent-disposition
attachment)filenamez9Failed to send community.general.mail: can't attach file zFailed to send mail to 'z':  z-Failed to send mail to at least one recipient)r*   resultzMail sent successfully )5r   dictparamsgetr   r   smtplibSMTP_SSLconnectsslSSLError	fail_jsonr   	traceback
format_exc	ExceptionSMTPSMTPExceptionehlor   has_extnr9   loginSMTPAuthenticationErrorwarnr   r   r	   r   r
   	TypeErrorpreamblesplitstrip
add_headerappendjoinr   r-   r   openset_payloadreadr   encode_base64osr.   basename	as_stringsendmailsetquit	exit_json)*moduler   r   r   r   r@   r   r%   copiesblindcopiesr)   r,   attach_filesr/   r0   r1   r5   r:   r<   codesecure_statesender_phrasesender_addrsmtpsmtpmessageer*   headerxhdrh_keyh_val	addr_listaddrto_listcc_listpartrU   fpcomposedrW   keys*                                             j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/mail.pymainr      s
    
u%
uT2
 5+6
 5"-	

 ud3
 UFVHE
 %&L>Z
 %<
 &5"=
 edUGD
 5!
 VfbA
 fubA
 eW5
 eWvw>OP
  UE;ab!
" eR0#
$ #yA%
( '
34+F0 }}  ,H}}  ,H==V$D==V$D]]&&z2N]]x(F""4(J]]t$F--##E*Kmm	*G==V$D==$$X.Lmm	*Gmm	*Gmm	*G]]x(Fmm	*G&9:DL!*6!2M;AW"++DQ_ipqD"++>SZ[D$(LLt$<!k# ||Dahi||>7S $T4 8D+
D		 4y1}*= =}}Z(cMMO#'LPIIK Z'$$\`bf/g$hH== r

8X. UY[_'`aX(GH

)Cm[9:CKt,CKGW-C	Nf&.?@L
 'CL K'-||C'89!AGGI9 	KCK"yya0u!&"89ue,		KK z#89I$/0q0 -4+,- G$./q/ -z)D/234+,- 		'"CIG$*+q+ -z)D/234+,- 		'"CID6MGgFDJJt ! 
Y		YM>:Dh% ,  +,""4(OO1<"''JZJZ[cJdOeJJt
Y }}Hf{C	NHE
 	IIK 	fCKKVC[QR^U[\_U`abUcde	fLU[\
1&A} << mX%$$04dIaL0JU^UiUiUk % m     AAD$PYZ[P\#]iri}i}i  	A  	AA
    DAtTS\]^S_#`lu  mA  mA  mC  	D  	DD ,, c$$&*D)A,0@KTK_K_Ka % c cc
 ,, P$$SWY]_hij_k/l  yB  yM  yM  yO$  P  PP 22 H  A{  BF  ,G   H(( r  Aeiko+p qr  f&LLdef :
  KCcIJK 1 0 ,, ,
  	Y&	!(6AJAUAUAW  Y Y	Y  fA))C	N3Yq\$CNWNbNbNd 	 	f 	ffs6  a A_. )Aa 8b0 .d	 e" f; h 2i<i,i4.i9'i>j j:Aj,k .a=a>a aa aa b-*8b((b-0d8dd	e8ee"f858f33f8;-h++hh'iii10i1j	j	k5kk	l=%Al88l=__main__)#
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESrw   r\   r_   rb   emailr   email.utilsr   r   r	   r
   email.mime.baser   email.mime.multipartr   email.mime.textr   email.headerr   ansible.module_utils.basicr   ansible.module_utils.sixr   +ansible.module_utils.common.text.convertersr   r   __name__rX       r   <module>r      sm    A @DLIV 
  
   E E $ . $  4 ( A{B| zF r   