o
    /h                      @   s   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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 G d
d dZdS )    N)
get_logger)find_user_home_path)get_oauth2_infoget_oauth_string)resolve_addresses)validate_email_with_regexhandle_password)prepare_message)make_addr_alias_userc                   @   s  e Zd ZdZ												d-dd	Zd
d Zdd Zedd Zedd Z	e
jdfddZ										d.ddZ											d/ddZdd Zdd Zdd Zdd Zd d! Zed"d# Zed$d% Zd&d' Zd0d)d*Zd+d, ZdS )1SMTPzs :class:`yagmail.SMTP` is a magic wrapper around
    ``smtplib``'s SMTP connection, and allows messages to be sent.Nsmtp.gmail.comTr   Futf-8c                 K   s   t  | _|   || _|
d urt|
|}|d u r|d }|r%|d u r%d}n|d u r,t }t|\| _| _|r;t	| j d | _
|| _|d urIt|n|rMdnd| _|| _|| _|| _|| _|	| _|| _i | _g | _d| _|
| _|
d u rt|n|| _|| _d S )Nemail_address 465587r   )r   logset_loggingsoft_email_validationr   r   r   user	useraliasr   	is_closedhoststrportsmtp_starttlssslsmtp_skip_login
debuglevelencodingkwargscacheunsentnum_mail_sentoauth2_filecredentialsdkim)selfr   passwordr   r   r   smtp_sslsmtp_set_debuglevelr   r    r%   r   r'   r!   oauth2_info r-   B/var/www/html/myenv/lib/python3.10/site-packages/yagmail/sender.py__init__   s:   


zSMTP.__init__c                 C   s   | S Nr-   r(   r-   r-   r.   	__enter__D   s   zSMTP.__enter__c                 C   s   | j s|   dS )NF)r   close)r(   exc_typeexc_valexc_tbr-   r-   r.   __exit__G   s   zSMTP.__exit__c                 C   s   | j rtjS tjS r0   )r   smtplibSMTP_SSLr   r1   r-   r-   r.   
connectionL      zSMTP.connectionc                 C   s   | j d u r| jr
dS dS | j S )NFT)r   r   r1   r-   r-   r.   starttlsP   s   
zSMTP.starttlsc                 C   s   t ||| _dS )a  
        This function allows to change the logging backend, either output or file as backend
        It also allows to set the logging level (whether to display only critical/error/info/debug.
        for example::

            yag = yagmail.SMTP()
            yag.set_logging(yagmail.logging.DEBUG)  # to see everything

        and::

            yagmail.set_logging(yagmail.logging.DEBUG, 'somelocalfile.log')

        lastly, a log_level of :py:class:`None` will make sure there is no I/O.
        N)r   r   )r(   	log_levelfile_path_namer-   r-   r.   r   V   s   zSMTP.set_loggingc                 C   sn   t | j| j|||}| jr|d D ]}t| qt| j| j|||||| j||	|
| j}|d }| }||fS )N
recipients)	r   r   r   r   r   r
   r    r'   	as_string)r(   tosubjectcontentsattachmentsccbccheadersprettify_html
message_idgroup_messages	addresses
email_addrmsgr?   msg_stringsr-   r-   r.   prepare_sendg   s*   
zSMTP.prepare_sendc                 C   s@   |    | ||||||||	|
|
\}}|r||fS | ||S )z% Use this to send an email with gmail)loginrO   _attempt_send)r(   rA   rB   rC   rD   rE   rF   preview_onlyrG   rH   rI   rJ   r?   rN   r-   r-   r.   send   s    z	SMTP.sendc              
   C   s   d}|dk rIz| j | j||}| jd| |  jd7  _|W S  tjyD } z| j| |d7 }t	
|d  W Y d }~nd }~ww |dk s| j||f dS )Nr      zMessage sent to %s   F)smtpsendmailr   r   infor$   r8   SMTPServerDisconnectederrortimesleepr#   append)r(   r?   rN   attemptsresulter-   r-   r.   rQ      s    
zSMTP._attempt_sendc                 C   s4   t t| jD ]}| j|\}}| || qdS )z
        Emails that were not being able to send will be stored in :attr:`self.unsent`.
        Use this function to attempt to send these again
        N)rangelenr#   poprQ   )r(   ir?   rN   r-   r-   r.   send_unsent   s   zSMTP.send_unsentc              
   C   s4   d| _ z| j  W dS  tttjfy   Y dS w )z) Close the connection to the SMTP server TN)r   rV   quit	TypeErrorAttributeErrorr8   rY   r1   r-   r-   r.   r3      s   z
SMTP.closec                 C   s*   | j d ur| | j d S | | j d S r0   )r%   _login_oauth2r&   _loginr1   r-   r-   r.   rP      s   
z
SMTP.loginc                 C   s   | j | j| jfi | j| _| j| j | jr7| j  | jdu r(| j  n
| jjdi | j | j  d| _	| j
sL| | j|}| j| j| | jd| j| j| j dS )z
        Login to the SMTP server using password. `login` only needs to be manually run when the
        connection to the SMTP server was closed by the user.
        TFzConnected to SMTP @ %s:%s as %sNr-   )r:   r   r   r!   rV   set_debuglevelr   r<   ehlor   r   r	   r   rP   r   rX   )r(   r)   r-   r-   r.   rj      s   


zSMTP._loginc                 C   
   t | |S r0   r   )r   r)   r-   r-   r.   r	         
zSMTP.handle_passwordc                 C   rm   r0   )r   )r   r,   r-   r-   r.   r      rn   zSMTP.get_oauth_stringc                 C   s   d|v r	| d | j| j| jfi | j| _z	| j| j W n	 ty)   Y nw | 	| j
|}| j|d  | jdu rC| j  | jdd|  d S )Nr   google_client_idTAUTHzXOAUTH2 )rc   r:   r   r   r!   rV   rk   r   rh   r   r   rl   r<   docmd)r(   r,   auth_stringr-   r-   r.   ri      s   


zSMTP._login_oauth28Awesome features! You made my day! How can I contribute?c                 C   s   |  dd| dS )z6 Most important function. Please send me feedback :-) zkootenpv@gmail.comzYagmail feedbackN)rS   )r(   messager-   r-   r.   feedback   r;   zSMTP.feedbackc                 C   s0   z| j s|   W d S W d S  ty   Y d S w r0   )r   r3   rh   r1   r-   r-   r.   __del__  s   zSMTP.__del__)NNr   NNTr   Fr   NTN)
NNNNNNNTNT)NNNNNNFNTNT)rs   )__name__
__module____qualname____doc__r/   r2   r7   propertyr:   r<   loggingERRORr   rO   rS   rQ   re   r3   rP   rj   staticmethodr	   r   ri   ru   rv   r-   r-   r-   r.   r      sr    
.


(
!	


r   )r[   r|   r8   yagmail.logr   yagmail.utilsr   yagmail.oauth2r   r   yagmail.headersr   yagmail.validater   yagmail.passwordr	   yagmail.messager
   r   r   r-   r-   r-   r.   <module>   s   