ó
TR¹Nc           @   st   d  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 e f d „  ƒ  YZ
 d S(   s   SMTP email backend class.iÿÿÿÿN(   t   settings(   t   BaseEmailBackend(   t   DNS_NAMEt   EmailBackendc           B   sM   e  Z d  Z d d d d d e d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(   s=   
    A wrapper that manages the SMTP network connection.
    c         K   s¡   t  t |  ƒ j d | ƒ | p% t j |  _ | p7 t j |  _ | pI t j |  _	 | p[ t j
 |  _ | d  k r| t j |  _ n	 | |  _ d  |  _ t j ƒ  |  _ d  S(   Nt   fail_silently(   t   superR   t   __init__R    t
   EMAIL_HOSTt   hostt
   EMAIL_PORTt   portt   EMAIL_HOST_USERt   usernamet   EMAIL_HOST_PASSWORDt   passwordt   Nonet   EMAIL_USE_TLSt   use_tlst
   connectiont	   threadingt   RLockt   _lock(   t   selfR   R
   R   R   R   R   t   kwargs(    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyR      s    		c         C   sº   |  j  r t Sy t j |  j |  j d t j ƒ  ƒ|  _  |  j rj |  j  j	 ƒ  |  j  j
 ƒ  |  j  j	 ƒ  n  |  j r˜ |  j r˜ |  j  j |  j |  j ƒ n  t SWn |  j s¶ ‚  q¶ n Xd S(   s‘   
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        t   local_hostnameN(   R   t   Falset   smtplibt   SMTPR   R
   R   t   get_fqdnR   t   ehlot   starttlsR   R   t   logint   TrueR   (   R   (    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyt   open   s    			c         C   s`   zO y |  j  j ƒ  Wn7 t j k
 r7 |  j  j ƒ  n |  j rG d S‚  n XWd d |  _  Xd S(   s*   Closes the connection to the email server.N(   R   t   quitt   sockett   sslerrort   closeR   R   (   R   (    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyR%   5   s    	c         C   s•   | s
 d S|  j  j ƒ  zi |  j ƒ  } |  j s3 d Sd } x0 | D]( } |  j | ƒ } | r@ | d 7} q@ q@ W| r |  j ƒ  n  Wd |  j  j ƒ  X| S(   so   
        Sends one or more EmailMessage objects and returns the number of email
        messages sent.
        Ni    i   (   R   t   acquireR!   R   t   _sendR%   t   release(   R   t   email_messagest   new_conn_createdt   num_sentt   messaget   sent(    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyt   send_messagesE   s     	c         C   s]   | j  ƒ  s t Sy/ |  j j | j | j  ƒ  | j ƒ  j ƒ  ƒ Wn |  j sT ‚  n  t SXt S(   s-   A helper method that does the actual sending.(	   t
   recipientsR   R   t   sendmailt
   from_emailR,   t	   as_stringR   R    (   R   t   email_message(    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyR'   ^   s    		N(
   t   __name__t
   __module__t   __doc__R   R   R   R!   R%   R.   R'   (    (    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyR      s   			(   R6   R   R#   R   t   django.confR    t   django.core.mail.backends.baseR   t   django.core.mail.utilsR   R   (    (    (    sK   /home/panlixing/Python_Projects/gaeseries/django/core/mail/backends/smtp.pyt   <module>   s   