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
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 :)")
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) break except smtplib.SMTPException: print("Unexpected error... trying again in 10 seconds.") time.sleep(10)
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)