def prompt_email(name, residency): print("\nWhat would you like the subject (title) of your email to be?") subject = input( "Type here and press enter (if blank, a random one will be generated): " ) print( "\033[1;33;48m\nIF YOU ACCIDENTALLY INPUTTED A WRONG SUBJECT, press control and 'c' to exit the program to try " "again\nTo run" " the program again, type 'python3 send.py' and press 'Enter'") if len(subject) == 0: subject = messages.gen_subject() print( "\033[1;32;48m\nMailbot can write emails addressed personally to each lawmaker." ) print("\033[1;37;48m") print( "If you would like to WRITE YOUR OWN message, please save it in a .txt file.\nThe easiest way to do " "this is to just write your message in example.txt.\n") response = "" msg = "" while response.lower() != "y" and response.lower() != "n": response = input( "Would you like mailbot to write emails for you? (y/n): ") if response.lower() == "y": with open("template.txt", 'r', encoding='utf-8-sig') as fd: msg = fd.read() msg = msg.replace("[PERSON-NAME]", name) msg = msg.replace("[RESIDENCY]", residency) elif response.lower() == 'n': print( "\033[1;33;48m\nIf you accidentally inputted 'n', press control and 'c' to exit the program to try " "again\nTo run" " the program again, type 'python3 send.py' and press 'Enter'") while len(msg) <= 0: print("\033[1;37;48m\nIf not,") filename = input("What is the name of your txt file?: ") try: with open(filename, 'r', encoding='utf-8-sig') as fd: msg = fd.read() except: print("Incorrect file name. Please try again") if input("Enter 'n' to try again. Enter 'q' to quit" ) == "q": quit() return subject, msg
def send_email(self, recv, src_name, src_email, password): port = 465 # standard port for SMTP over SSL smtp_server = "smtp.gmail.com" num_sent = 0 num_tries = 0 while True: num_tries += 1 try: # create a secure SSL context context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(src_email, password) while recv: recipient = recv.pop() dst_name = recipient[0] location = recipient[1] dst_email = recipient[2] msg = EmailMessage() msg['Subject'] = messages.gen_subject() msg['From'] = src_email msg['To'] = dst_email body = messages.gen_body(src_name, dst_name, location) msg.set_content(body) print(msg.as_string()) server.send_message(msg) print("Sent email") num_sent += 1 break except smtplib.SMTPException as E: print(E) if num_tries >= 5: return -1 break print("Unexpected error... trying again in 10 seconds.") time.sleep(10) return num_sent
while True: try: # create a secure SSL context context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(src_email, password) while recv: recipient = recv.pop() dst_name = recipient[0] location = recipient[1] dst_email = recipient[2] msg = EmailMessage() msg['Subject'] = subject if subject else messages.gen_subject() msg['From'] = src_email msg['To'] = dst_email body = messages.attach_greeting(dst_name, message) if message else messages.gen_body(src_name, dst_name, location) msg.set_content(body) print(msg.as_string()) server.send_message(msg) send += 1 break except smtplib.SMTPException: print("Unexpected error... trying again in 10 seconds.") time.sleep(10) print(f'\nSuccessfully sent {send} emails!\n')
def main(): print( """Welcome to the activism email bot! This bot sends emails to 119 elected officials accross the U.S. calling for action from our elected officials. (template from nomoreracistcops.github.io) Credits for this app to go https://github.com/alandgton/activism-mail-bot.\n""" ) print( """When you press enter, a browser window will launch, asking you to give this app permission to send emails from your gmail account. (At the time of this writing, we have not gotten our app verified yet as it takes a couple days to do so and so you will need to click on the Advanced button to bypass it on the webpage)\n""") print( """We promise we are not doing anything malicious with your email creds and we are using them only for the purpose of sending emails to elected officials. You can check the source code in link above to check if you would like.\n""") input("Press Enter to continue to begin!") creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( resource_path('credentials.json'), SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('gmail', 'v1', credentials=creds) print("\nNow that we've finished authenticating lets get started:\n") src_name = input("Type your name (first, last) and press enter: ") print("\nWhat would you like the subject (title) of your email to be?") subject = input( "Type here and press enter (a random one will be generated if blank): " ) recv = recipients.gen_recipients() while recv: recipient = recv.pop() dst_name = recipient[0] location = recipient[1] dst_email = recipient[2] subject = subject if subject else messages.gen_subject() body = messages.gen_body(src_name, dst_name, location) message = create_message(dst_email, subject, body) send_message(service, "me", message) print_email(dst_email, subject, body) # print if we get through without time.sleep(0.1) print("Sweet thanks for sending the emails! you rock :)")
recv = recipients.gen_recipients("LA") while True: try: # create a secure SSL context context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(src_email, password) while recv: recipient = recv.pop() dst_name = recipient[0] location = recipient[1] dst_email = recipient[2] msg = EmailMessage() msg['Subject'] = messages.gen_subject() msg['From'] = src_email msg['To'] = dst_email body = messages.gen_body(src_name, dst_name, location) msg.set_content(body) print(msg.as_string()) server.send_message(msg) break except smtplib.SMTPException: print("Unexpected error... trying again in 10 seconds.") time.sleep(10)
def send(): port = 465 # standard port for SMTP over SSL smtp_server = "smtp.gmail.com" correct_login = False send = 0 name = prompt_name() residency = prompt_residency() recv = prompt_recipients() subject, message = prompt_email(name, residency) # while True: try: # create a secure SSL context context = ssl.create_default_context() # open smtplib, a client object to send emails. -> take in server and port to send from with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: while correct_login is False: try: src_email, password = prompt_login() server.login(src_email, password) correct_login = True except: print( "\033[1;31;48mIncorrect email or password. Please try again" ) print("\033[1;37;48m") if correct_login: # Take info from recipients file while recv: recipient = recv.pop() recv_name = recipient[0] location = recipient[1] dst_email = recipient[2] police_budget = recipient[3] total_budget = recipient[4] police_budget_int = int(police_budget.replace(",", "")) total_budget_int = int(total_budget.replace(",", "")) percent = round( 100 * (police_budget_int / total_budget_int), 2) msg = EmailMessage() if len(subject) > 0: msg['Subject'] = subject else: msg['Subject'] = messages.gen_subject() msg['From'] = src_email msg['To'] = dst_email message = message.replace("[POLICE-BUDGET]", police_budget) message = message.replace("[TOTAL-BUDGET]", total_budget) message = message.replace("[PERCENT]", str(percent)) message = message.replace("[CITY-NAME]", location) if residency.lower() != location.lower(): message = message.replace( " and I am from " + residency + ".", " and") message = message.replace("member of this community", "citizen") body = "Dear " + recv_name + " of " + location + ",\n\n" + message msg.set_content(body) print(msg.as_string()) server.send_message(msg) send += 1 # break except smtplib.SMTPException: print("Unexpected error... trying again in 10 seconds.") print("Are you running this program in terminal?") time.sleep(10) print_barrier() print(f'\nSuccessfully sent {send} emails!\n') print_barrier()