�
���h� � �� � d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl m
Z
d dlmZm
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZ d d lmZmZ d d
lmZ e� � n# e$ r Y nw xY wdgZd%dedefd�Zdedz dedz fd�Z dedz dee fd�Z!d&dedededededz dedz dee dz dedz de
fd�Z"d ed!e
defd"�Z#d#� Z$e%d$k r e$� � dS dS )'� N)�datetime)�EmailMessage)�List�Dict�Any)�Credentials)�build)�InstalledAppFlow)�Request)�Template�StrictUndefined)�load_dotenvz*https://www.googleapis.com/auth/gmail.send�credentials.json�
token.json�credentials_path�
token_pathc �t � d }t j � |� � rt j |t
� � }|r|j s�|r0|j r)|j r"|� t � � � � nbt j � | � � st d| � d�� � �t j
| t
� � }|� d�� � }t |d� � 5 }|� |� � � � � d d d � � n# 1 swxY w Y t% dd|�� � S ) NzMissing zJ. Create OAuth client (Desktop) in Google Cloud Console and place it here.r )�port�w�gmail�v1)�credentials)�os�path�existsr �from_authorized_user_file�SCOPES�valid�expired�
refresh_token�refreshr �
SystemExitr
�from_client_secrets_file�run_local_server�open�write�to_jsonr )r r �creds�flow�tokens �KC:\Users\crens\Downloads\gmail_csv_sender\gmail_csv_sender\send_from_csv.py�get_servicer, sw � ��E� �w�~�~�j�!�!� J��5�j�&�I�I��� )��� )�� 2�U�]� 2�u�':� 2��M�M�'�)�)�$�$�$�$��7�>�>�"2�3�3�
J� � "I�,<� "I� "I� "I� J� J� J�#�<�=M�v�V�V�D��)�)�q�)�1�1�E�
�*�c�
"�
"� )�e��K�K��
�
���(�(�(� )� )� )� )� )� )� )� )� )� )� )���� )� )� )� )���$�E�2�2�2�2s �)(D�D!�$D!�template_string�
template_filec � � | r|rt d� � �|rTt |dd�� � 5 }t |� � � t �� � cd d d � � S # 1 swxY w Y d S | rt | t �� � S d S )Nz/Provide either --body or --body_file, not both.�r�utf-8)�encoding)� undefined)r"