def get_msg(me, you): # me == my email address # you == recipient's email address html_file_path = "D:/Test/email_content_saved.txt" text_file_path = "D:/Test/email_text.txt" # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = "100+ HIGH TF/CF/DA EXPIRED DOMAINS TO BUY ONLY $10 EACH" msg['From'] = me msg['To'] = you # Create the body of the message (a plain-text and an HTML version). text = "" html = FileHandler.read_all_from_file(html_file_path, 't') # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) return msg
def testEmailLogin(self): # Send the message via local SMTP server using Oauth2. from Email.SMTP import SMTP import httplib2 from Email.Utility.Oauth2 import CustomOAuth2Credentials me = "*****@*****.**" you = "*****@*****.**" msg = get_msg(me, you) http = httplib2.Http() credentials = OAuth2Credentials.from_json( FileHandler.read_all_from_file(credentials_local_path)) # scopes = credentials.retrieve_scopes(http) # for item in scopes: # print(item) if credentials.access_token_expired: # http = credentials.authorize(http) credentials.refresh(http) jsoned = credentials.to_json() FileHandler.remove_file_if_exist(credentials_local_path) FileHandler.append_line_to_file(credentials_local_path, str(jsoned)) auth_str = GenerateOAuth2String(me, access_token=credentials.access_token) s = SMTP(**gmail_provider) s.set_debuglevel(debuglevel=4) s.ehlo() s.starttls() s.authenticate_oauth2(auth_str) s.sendmail(me, you, msg.as_string()) s.quit()
def testRe(self): css_text = FileHandler.read_all_from_file("/Users/superCat/Desktop/PycharmProjectPortable/test/example.css") test_s = "if('undefined' === typeof wwhomepage) var wwhomepage = {}; wwhomepage.customPromoHeaders = {\" /web/20130415001342/http://www.bbc.co.uk\/news\/magazine-22094279\":" match = re.search(link_pattern, css_text) groups = match.group() if match is not None: for i in match.groups(0): print(i)
def testRe(self): css_text = FileHandler.read_all_from_file( "/Users/superCat/Desktop/PycharmProjectPortable/test/example.css") test_s = "if('undefined' === typeof wwhomepage) var wwhomepage = {}; wwhomepage.customPromoHeaders = {\" /web/20130415001342/http://www.bbc.co.uk\/news\/magazine-22094279\":" match = re.search(link_pattern, css_text) groups = match.group() if match is not None: for i in match.groups(0): print(i)
def testCss2Parse(self): css_text = FileHandler.read_all_from_file("/Users/superCat/Desktop/PycharmProjectPortable/test/example.css") groups = [] parse_str_sp = functools.partial(parse_str, groups, 1) temp = re.sub(link_pattern, parse_str_sp, css_text) # for item in groups: # print(item) print("captured total: ", len(groups)) for item in groups: if isinstance(item, LinkAttrs): print("res:", item.path, "link:", item.link)
def testCss2Parse(self): css_text = FileHandler.read_all_from_file( "/Users/superCat/Desktop/PycharmProjectPortable/test/example.css") groups = [] parse_str_sp = functools.partial(parse_str, groups, 1) temp = re.sub(link_pattern, parse_str_sp, css_text) # for item in groups: # print(item) print("captured total: ", len(groups)) for item in groups: if isinstance(item, LinkAttrs): print("res:", item.path, "link:", item.link)
def testCssParse(self): css_text = FileHandler.read_all_from_file("/Users/superCat/Desktop/PycharmProjectPortable/test/example.css") section = css_text.split("}") groups = [] parse_str_sp = functools.partial(parse_str, groups, 1) result = "" for sec in section: sec += "}" temp = re.sub(css_link_pattern, parse_str_sp, sec) result += temp for item in groups: print(item) print(result)
def testMsgGen(self): email_template_path = "D:/Test/email_content_template.txt" email_content_save_path = "D:/Test/email_content_saved.txt" email_lines_before_table_path = "D:/Test/email_text_before_table.txt" email_lines_after_table_path = "D:/Test/email_text_after_table.txt" data_file_path = "D:/Test/data_sample.csv" # th for head cell, td for data cell email_template = FileHandler.read_all_from_file(email_template_path) cell_item_template = '<{0:s} style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: ' \ 'border-box;padding: 8px;text-align: left;line-height: 1.42857143;vertical-align: ' \ 'bottom;border-top: 1px solid #ddd;border-bottom: 2px solid #ddd;border: 1px solid ' \ '#ddd!important;border-bottom-width: 2px;background-color: #fff!important;">' \ '{1:s}</{0:s}>' row_item_template = '<tr style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing:' \ ' border-box;page-break-inside: avoid;">{0:s}</tr>' line_format = '<p style="-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: ' \ 'border-box;orphans: 3;widows: 3;margin: 0 0 10px;">{0:s}</p><br>' before_table_lines = FileHandler.read_lines_from_file( email_lines_before_table_path, remove_blank_line=False) after_table_lines = FileHandler.read_lines_from_file( email_lines_after_table_path, remove_blank_line=False) before_table_str = "".join( [line_format.format(x, ) for x in before_table_lines]) after_table_str = "".join( [line_format.format(x, ) for x in after_table_lines]) table_cells_str = "" with open(data_file_path, mode='r', newline='') as csv_file: reader = csv.reader(csv_file, delimiter=',') header = next(reader) header_row_str = row_item_template.format("".join( [cell_item_template.format( "th", x, ) for x in header])) for row in reader: table_cells_str += row_item_template.format("".join( [cell_item_template.format( "td", x, ) for x in row])) email_content = email_template.format(before_table_str, 50, header_row_str, table_cells_str, after_table_str) FileHandler.remove_file_if_exist(email_content_save_path) FileHandler.append_line_to_file(email_content_save_path, email_content) return email_content
def testCssParse(self): css_text = FileHandler.read_all_from_file( "/Users/superCat/Desktop/PycharmProjectPortable/test/example.css") section = css_text.split("}") groups = [] parse_str_sp = functools.partial(parse_str, groups, 1) result = "" for sec in section: sec += "}" temp = re.sub(css_link_pattern, parse_str_sp, sec) result += temp for item in groups: print(item) print(result)