<s> import re <EOL> import urllib2 <EOL> import smtplib <EOL> import dns . resolver <EOL> import dns . reversename <EOL> import socket <EOL> from core . utils import Utils <EOL> from core . mydns import Dns <EOL> from email import encoders <EOL> from email . MIMEMultipart import MIMEMultipart <EOL> from email . MIMEText import MIMEText <EOL> from email . MIMEBase import MIMEBase <EOL> from email import encoders <EOL> MX_RECORD_CACHE = { } <EOL> def validate_mx ( server , domain ) : <EOL> try : <EOL> if server in MX_RECORD_CACHE : <EOL> return MX_RECORD_CACHE [ server ] <EOL> smtp = smtplib . SMTP ( timeout = <NUM_LIT:10> ) <EOL> smtp . connect ( server ) <EOL> status , _ = smtp . helo ( ) <EOL> if status != <NUM_LIT> : <EOL> smtp . quit ( ) <EOL> print "<STR_LIT>" % ( server , status , _ ) <EOL> smtp . mail ( '<STR_LIT>' ) <EOL> status , _ = smtp . rcpt ( "<STR_LIT>" + domain ) <EOL> if status == <NUM_LIT> : <EOL> smtp . quit ( ) <EOL> MX_RECORD_CACHE [ server ] = True <EOL> return True <EOL> print "<STR_LIT>" % ( server , status , _ ) <EOL> smtp . quit ( ) <EOL> except smtplib . SMTPServerDisconnected as e : <EOL> print "<STR_LIT>" % ( server , e ) <EOL> except smtplib . SMTPConnectError as e : <EOL> print "<STR_LIT>" % ( server , e ) <EOL> except socket . timeout as e : <EOL> print "<STR_LIT>" % ( server , e ) <EOL> MX_RECORD_CACHE [ server ] = False <EOL> return False <EOL> def get_mx_records ( domain ) : <EOL> hosts = Dns . mx ( domain ) <EOL> for rdata in records : <EOL> if ( validate_mx ( ( str ( rdata . exchange ) ) [ : - <NUM_LIT:1> ] , domain ) ) : <EOL> hosts . append ( ( str ( rdata . exchange ) ) [ : - <NUM_LIT:1> ] ) <EOL> return hosts <EOL> def get_mx_record ( domain ) : <EOL> hosts = Dns . mx ( domain ) <EOL> if ( hosts ) : <EOL> return hosts [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def validate_email_address ( email_to , email_from , debug = False ) : <EOL> domain = email_to . split ( '<STR_LIT:@>' ) [ <NUM_LIT:1> ] <EOL> remote_server = get_mx_record ( domain ) <EOL> if ( remote_server is None ) : <EOL> print "<STR_LIT>" % ( email_to ) <EOL> return False <EOL> try : <EOL> smtp = smtplib . SMTP ( ) <EOL> smtp . connect ( remote_server ) <EOL> if debug : <EOL> smtp . set_debuglevel ( True ) <EOL> except smtplib . SMTPConnectError , e : <EOL> print e <EOL> return False <EOL> try : <EOL> smtp . ehlo_or_helo_if_needed ( ) <EOL> except Exception , e : <EOL> print e <EOL> return False <EOL> v_code , v_message = smtp . verify ( email_to ) <EOL> if v_code and v_code != <NUM_LIT> : <EOL> f_code , f_message = smtp . mail ( email_from ) <EOL> if f_code and f_code == <NUM_LIT> : <EOL> r_code , r_message = smtp . rcpt ( email_to ) <EOL> if r_code and r_code == <NUM_LIT> : <EOL> return True , r_message <EOL> if r_code and r_code == <NUM_LIT> : <EOL> return False , r_message <EOL> else : <EOL> return False <EOL> else : <EOL> return True , v_message <EOL> smtp . quit ( ) <EOL> return False <EOL> def send_email_direct ( email_to , email_from , display_name , subject , body , attach_fname , attach_filepath , debug = False ) : <EOL> domain = email_to . split ( '<STR_LIT:@>' ) [ <NUM_LIT:1> ] <EOL> remote_server = get_mx_record ( domain ) <EOL> if ( remote_server is None ) : <EOL> print "<STR_LIT>" % ( email_to ) <EOL> return <EOL> if ( not display_name ) : <EOL> display_name = email_from <EOL> server = smtplib . SMTP ( remote_server , <NUM_LIT> ) <EOL> msg = MIMEMultipart ( ) <EOL> msg [ '<STR_LIT>' ] = display_name <EOL> msg [ '<STR_LIT>' ] = email_to <EOL> msg [ '<STR_LIT>' ] = subject <EOL> msg . attach ( MIMEText ( body , '<STR_LIT>' ) ) <EOL> if ( attach_fname ) : <EOL> attachment = open ( attach_filepath , "<STR_LIT:rb>" ) <EOL> part = MIMEBase ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> part . set_payload ( ( attachment ) . read ( ) ) <EOL> encoders . encode_base64 ( part ) <EOL> part . add_header ( '<STR_LIT>' , "<STR_LIT>" % attach_fname ) <EOL> msg . attach ( part ) <EOL> smtp_sendmail_return = "<STR_LIT>" <EOL> if debug : <EOL> server . set_debuglevel ( True ) <EOL> try : <EOL> smtp_sendmail_return = server . sendmail ( email_from , email_to , msg . as_string ( ) ) <EOL> except Exception , e : <EOL> exception = '<STR_LIT>' + str ( e ) + '<STR_LIT:\n>' + str ( smtp_sendmail_return ) <EOL> finally : <EOL> server . quit ( ) <EOL> def send_email_account ( remote_server , remote_port , username , password , email_to , email_from , display_name , subject , body , attach_fname , attach_filepath , debug = False ) : <EOL> if ( remote_server == "<STR_LIT>" ) : <EOL> send_email_gmail ( username , password , email_to , email_from , subject , body , debug ) <EOL> else : <EOL> if ( not display_name ) : <EOL> display_name = email_from <EOL> server = smtplib . SMTP ( remote_server , remote_port ) <EOL> msg = MIMEMultipart ( ) <EOL> msg [ '<STR_LIT>' ] = display_name <EOL> msg [ '<STR_LIT>' ] = email_to <EOL> msg [ '<STR_LIT>' ] = subject <EOL> msg . attach ( MIMEText ( body , '<STR_LIT>' ) ) <EOL> if ( attach_fname ) : <EOL> attachment = open ( attach_filepath , "<STR_LIT:rb>" ) <EOL> part = MIMEBase ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> part . set_payload ( ( attachment ) . read ( ) ) <EOL> encoders . encode_base64 ( part ) <EOL> part . add_header ( '<STR_LIT>' , "<STR_LIT>" % attach_fname ) <EOL> msg . attach ( part ) <EOL> smtp_sendmail_return = "<STR_LIT>" <EOL> if debug : <EOL> server . set_debuglevel ( True ) <EOL> try : <EOL> server . login ( username , password ) <EOL> smtp_sendmail_return = server . sendmail ( email_from , email_to , msg . as_string ( ) ) <EOL> except Exception , e : <EOL> exception = '<STR_LIT>' + str ( e ) + '<STR_LIT:\n>' + str ( smtp_sendmail_return ) <EOL> finally : <EOL> server . quit ( ) <EOL> def send_email_gmail ( username , password , email_to , email_from , subject , body , debug = False ) : <EOL> server = smtplib . SMTP ( "<STR_LIT>" , <NUM_LIT> ) <EOL> message = "<STR_LIT>" % ( email_from , email_to , subject , body ) <EOL> smtp_sendmail_return = "<STR_LIT>" <EOL> if debug : <EOL> server . set_debuglevel ( True ) <EOL> try : <EOL> server . ehlo ( ) <EOL> server . starttls ( ) <EOL> server . login ( username , password ) <EOL> smtp_sendmail_return = server . sendmail ( email_from , email_to , message ) <EOL> except Exception , e : <EOL> exception = '<STR_LIT>' + str ( e ) + '<STR_LIT:\n>' + str ( smtp_sendmail_return ) <EOL> finally : <EOL> server . quit ( ) <EOL> class EmailTemplate ( ) : <EOL> def __init__ ( self , TYPE , SUBJECT , BODY ) : <EOL> self . TYPE = TYPE <EOL> self . SUBJECT = SUBJECT <EOL> self . BODY = BODY <EOL> def getTYPE ( self ) : <EOL> return self . TYPE <EOL> def getSUBJECT ( self ) : <EOL> return self . SUBJECT <EOL> def getBODY ( self ) : <EOL> return self . BODY <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . TYPE , self . SUBJECT , self . BODY ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print "<STR_LIT>" </s>
