
    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
mZ dZ	 d dlZdZd dlmZmZ d d	lmZ d d
lmZ d dlmZ 	 	 ddZd Zedk(  r e        yy# e$ r  e	j                          ZdZY Kw xY w)    )absolute_importdivisionprint_functiona	  
module: sendgrid
short_description: Sends an email with the SendGrid API
description:
  - Sends an email with a SendGrid account through their API, not through the SMTP service.
notes:
  - This module is non-idempotent because it sends an email through the external API. It is idempotent only in the case that
    the module fails.
  - Like the other notification modules, this one requires an external dependency to work. In this case, you will need an
    active SendGrid account.
  - In order to use O(api_key), O(cc), O(bcc), O(attachments), O(from_name), O(html_body), and O(headers) you must C(pip install
    sendgrid).
requirements:
  - sendgrid Python library 1.6.22 or lower (Sendgrid API V2 supported)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  username:
    type: str
    description:
      - Username for logging into the SendGrid account.
      - It is only required if O(api_key) is not supplied.
  password:
    type: str
    description:
      - Password that corresponds to the username.
      - It is only required if O(api_key) is not supplied.
  from_address:
    type: str
    description:
      - The address in the "from" field for the email.
    required: true
  to_addresses:
    type: list
    elements: str
    description:
      - A list with one or more recipient email addresses.
    required: true
  subject:
    type: str
    description:
      - The desired subject for the email.
    required: true
  api_key:
    type: str
    description:
      - Sendgrid API key to use instead of username/password.
  cc:
    type: list
    elements: str
    description:
      - A list of email addresses to cc.
  bcc:
    type: list
    elements: str
    description:
      - A list of email addresses to bcc.
  attachments:
    type: list
    elements: path
    description:
      - A list of relative or explicit paths of files you want to attach (7MB limit as per SendGrid docs).
  from_name:
    type: str
    description:
      - The name you want to appear in the from field, for example V(John Doe).
  html_body:
    description:
      - Whether the body is html content that should be rendered.
    type: bool
    default: false
  headers:
    type: dict
    description:
      - A dict to pass on as headers.
  body:
    type: str
    description:
      - The e-mail body content.
    required: true
author: "Matt Makai (@makaimc)"
a3  
- name: Send an email to a single recipient that the deployment was successful
  community.general.sendgrid:
    username: "{{ sendgrid_username }}"
    password: "{{ sendgrid_password }}"
    from_address: "ansible@mycompany.com"
    to_addresses:
      - "ops@mycompany.com"
    subject: "Deployment success."
    body: "The most recent Ansible deployment was successful."
  delegate_to: localhost

- name: Send an email to more than one recipient that the build failed
  community.general.sendgrid:
    username: "{{ sendgrid_username }}"
    password: "{{ sendgrid_password }}"
    from_address: "build@mycompany.com"
    to_addresses:
      - "ops@mycompany.com"
      - "devteam@mycompany.com"
    subject: "Build failure!."
    body: "Unable to pull source repository from Git server."
  delegate_to: localhost
N)LooseVersionTF)AnsibleModulemissing_required_lib)	urlencode)to_bytes)	fetch_urlc                    t         sPd}d}|||||d}t        |      }d}|D ]  }t        |d      }|d|z  z  } ||z  }|dd	d
}t        | |||d      S t	        t
        j                  j                        t	        d      kD  r| j                  d       |rt        j                  |      }nt        j                  ||      }t        j                         }|j                  |       |D ]  }|j                  |        |r|D ]  }|j                  |        |	r|	D ]  }|j                  |        |r|j                  |       |
r8|
D ]3  }t         j"                  j%                  |      }|j'                  ||       5 |r|j)                  |d|d       n|j)                  |       |r|j+                  |       n|j-                  |       |j/                  |      S )Nz+https://api.sendgrid.com/api/mail.send.jsonAnsible)api_userapi_keyfromsubjecttext surrogate_or_strict)errorsz&to[]=%sz!application/x-www-form-urlencodedzapplication/json)z
User-AgentzContent-typeAcceptPOST)dataheadersmethodz1.6.22zLPlease install sendgrid==1.6.22 or lower since module uses Sendgrid V2 APIs.msgz <.)HAS_SENDGRIDr	   r
   r   r   sendgridversion__version__	fail_jsonSendGridClientMailset_subjectadd_toadd_ccadd_bccset_headersospathbasenameadd_attachmentset_fromset_htmlset_textsend)moduleusernamepasswordfrom_addressto_addressesr   bodyr   ccbccattachments	html_body	from_namer   SENDGRID_URIAGENTr   encoded_datato_addresses_api	recipientsgmessagerecipfnames                            n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sendgrid.pypost_sendgrid_apirH      s    D$$$H % 	7I 3HII
Y 66	7 	((!&#F/1 L'Z`aa ((445X8NN!op((1B((8<B--/G$! 	"ENN5!	"  &u%& '&' (  0ww''*&&tQ/0 )\BC\*T"T"www    c                  r   t        t        t        d      t        dd      t        dd      t        ddd      t        ddd      t        dd	      t        d      t        d      t        ddd      t        d      t        d      t        ddd
      t        ddd            dddgddggddgg      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }	| j                  d   }
| j                  d   }| j                  d   }| j                  d   }|||||||g}t        d |D              r*t        s$d}| j                  t        d|      t                t        | ||||	|
|||||||!      \  }}t        s!|d"   d#k7  r6| j                  d$|d%   z  &       n|d#k7  r| j                  d$|d'   z  &       | j                  |
d(       y ))NF)requiredT)rK   no_logliststr)rK   typeelementsdict)rK   rO   bool)rK   defaultrO   r+   )r3   r4   r   r9   r8   r   r5   r<   r6   r   r7   r;   r:   r   r4   r3   )argument_specsupports_check_modemutually_exclusiverequired_togetherr9   r8   r   r<   r5   r6   r   r7   r;   r:   c              3   $   K   | ]  }|d u 
 y w)N ).0lib_args     rG   	<genexpr>zmain.<locals>.<genexpr>   s     
@77$
@s   zgwhen using any of the following arguments: api_key, bcc, cc, headers, from_name, html_body, attachmentsr   )reason)r   	exception)r:   r9   r8   r   r;   r   status   z-unable to send email through SendGrid API: %sr   r   rC   )r   changed)
r   rQ   paramsanyr   r"   r   SENDGRID_IMP_ERRrH   	exit_json)r2   r3   r4   r   r9   r8   r   r<   r5   r6   r   r7   r;   r:   sendgrid_lib_argsr]   responseinfos                     rG   mainri      sZ   5)56%5e&5AU%@%f5t,E*t&5I$'t$E5vFe&6J
 !
#
#
 '
34+F0 }}Z(H}}Z(HmmI&G
--
C	t	BmmI&Gk*I==0L==0LmmI&G== Dk*I--.K #r7Iy+V

@.?
@@P1*VL#3 	 	5 'vx'3\7D^i+.2wR[elnNHd >S !PSWX]S^!^_s?!PSWXaSb!bc
%0rI   __main__)NNNNFNN)
__future__r   r   r   rO   __metaclass__DOCUMENTATIONEXAMPLESr*   	tracebackBansible_collections.community.general.plugins.module_utils.versionr   rd   r   r   ImportError
format_excansible.module_utils.basicr   r   +ansible.module_utils.six.moves.urllib.parser	   +ansible.module_utils.common.text.convertersr
   ansible.module_utils.urlsr   rH   ri   __name__rY   rI   rG   <module>rx      s    A @Vp8 
  [ L
 K A @ / SW?C= F:1z zF U  +y++-Ls   A A43A4