o
    xb	                     @   s   d dl 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 d dl	m
Z
 d dlmZ d dlmZ d dlmZ dd	gZd
d Zdd Zdd ZdS )    N)MIMEText)Header)Request)InstalledAppFlow)errors)buildz.https://www.googleapis.com/auth/gmail.readonlyz*https://www.googleapis.com/auth/gmail.sendc                 C   s8   t |d}||d< t|  d| dd|d< ||d< |S )Nhtmltoz <>zutf-8fromsubject)r   r   )Zsender_nameZsender_mailr	   r   Zmessage_textmessage r   0C:\Users\insignal\homepage\insignal\pages\lib.pycreate_message   s
   
r   c              
   C   s   z%t dd}|tjd tjd  |tjd | |  |  W d S  tj	y? } zt
d|  W Y d }~d S d }~ww )Nzsmtp.gmail.comi  Z
EMAIL_USERZEMAIL_PASSWDzAn error occurred: %s)smtplibSMTP_SSLloginosenvironsendmail	as_stringquitr   	HttpErrorprint)to_emailZmime_messageZsmtperrorr   r   r   send_message   s   r   c                  C   s   t j } td}| }|djdd | s |d |d|  |d|  | }|d d	kr<d
S |d|  |	  |
  |
  dS )Nzmail_send_cnt.dbzISELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}'Zsend_history)Z
table_namez*create table send_history (send_date date)z0delete from send_history where send_date < '{0}'zAselect count(send_date) from send_history where send_date = '{0}'r   
   Fz'insert into send_history values ('{0}')T)datetimedatetodaysqlite3connectcursorexecuteformatZfetchoneZcommitclose)Zcurrent_dateZsend_mail_dbZ	db_cursorrecordsr   r   r   allow_mail_send&   s$   



r)   )base64r   r   r   r"   email.mime.textr   Zemail.headerr   google_auth_httplib2r   google_auth_oauthlib.flowr   googleapiclientr   googleapiclient.discoveryr   SCOPESr   r   r)   r   r   r   r   <module>   s"    	