From 99a055b02d7f96f078d8d84cfbce5dba5b05e79f Mon Sep 17 00:00:00 2001 From: sallywoolweaver Date: Fri, 14 Apr 2023 19:05:47 -0500 Subject: [PATCH 1/2] fixed tmobile verizon issue --- GmailAPI/Gmail_API.py | 72 ++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/GmailAPI/Gmail_API.py b/GmailAPI/Gmail_API.py index b633020..ee135d4 100644 --- a/GmailAPI/Gmail_API.py +++ b/GmailAPI/Gmail_API.py @@ -10,9 +10,17 @@ from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody import re -#from dotenv import load_dotenv +#twilio set up as backup +from twilio.rest import Client +from dotenv import load_dotenv + +load_dotenv() +twilio_account_sid = os.environ.get("TWILIO_ACCOUNT_SID") +twilio_auth_token = os.environ.get("TWILIO_AUTH_TOKEN") +twilio_phone_number= os.environ.get("TWILIO_PHONE_NUMBER") + + -#load_dotenv() #TODO: #Loop only checks newest email every 30 seconds. If 2 are sent within that window, one is ignored. Fix this. @@ -74,29 +82,45 @@ def get_contents(pmsg): return file_contents return "No text contents found" - -def SendEmail(to, cc_mail, body, attachmentspath=None): - creds = Credentials( - #login information for where email is being sent from - username = "lhsrobobarista@outlook.com", - password = "Coffee1!" - ) - account = Account( - primary_smtp_address=username, - credentials=creds, - autodiscover=True, - access_type=DELEGATE +def send_sms_twilio(to, body): + client = Client(twilio_account_sid, twilio_auth_token) + message = client.messages.create( + body=body + '\nPlease do not reply to this number', + from_=twilio_phone_number, + to=to ) + print("Message sent to", to) - m = Message( - account=account, - cc_recipients=cc_mail, - subject=None, - body=HTMLBody(body), - to_recipients=[Mailbox(email_address=to)] - ) - print(body) - m.send() + +def SendEmail(to, cc_mail, body, attachmentspath=None): + # Check if the email address is for a T-Mobile or Verizon number + if "@tmomail.net" in to or "@vtext.com" in to: + # Extract the phone number from the email address + phone_number = re.sub(r"@.*", "", to) + # Send the SMS using Twilio + send_sms_twilio(phone_number, body) + else: + creds = Credentials( + # login information for where the email is being sent from + username="lhsrobobarista@outlook.com", + password="Coffee1!" + ) + account = Account( + primary_smtp_address=username, + credentials=creds, + autodiscover=True, + access_type=DELEGATE + ) + + m = Message( + account=account, + cc_recipients=cc_mail, + subject=None, + body=HTMLBody(body), + to_recipients=[Mailbox(email_address=to)] + ) + print(body) + m.send() @@ -122,6 +146,7 @@ def checkMail(): msg = email.message_from_bytes(response[1]) # decode the email subject print(msg['From']) + return(get_contents(msg), msg['From']) rem = messages @@ -129,7 +154,6 @@ def checkMail(): time.sleep(5) - # close the connection and logout imap.close() imap.logout() From 42f4e10ab3b445d57c3b03fd6344c9a3ff0a4d36 Mon Sep 17 00:00:00 2001 From: sallywoolweaver Date: Sun, 16 Apr 2023 21:51:26 -0500 Subject: [PATCH 2/2] fixed tmobile and verizon