Emergency Google SMTP authentication changes

master
bleeson 2025-05-05 15:16:50 -07:00
parent ec5a81fbdc
commit 3b7716e0cd
3 changed files with 53 additions and 6 deletions

1
credentials.json Normal file
View File

@ -0,0 +1 @@
{"installed":{"client_id":"249203512502-8ut4vkh77ns4rl40ia485t460niii2b8.apps.googleusercontent.com","project_id":"python-access-2025","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}

View File

@ -6,7 +6,7 @@ Problems:
Source does not send accurate unit price, shipping charge, tax, and discounts. To Source does not send accurate unit price, shipping charge, tax, and discounts. To
bring something accurate in we use the old Shopify integration. This brings up bring something accurate in we use the old Shopify integration. This brings up
another issue that the old integration brings in orders at creation, if these fields another issue that the old integration brings in orders at creation, if these fields
get modifed (like from a refund) we are bringing in the wrong amount. get modifed (like from a refund) we are brining in the wrong amount.
Questions: Questions:
How does Source handle multiple lots on a line? How does Source handle multiple lots on a line?
@ -28,7 +28,7 @@ To catch issues we need to look at:
-missing sales orders, that came into the shipping table, but failed to import in -missing sales orders, that came into the shipping table, but failed to import in
-missing deliveries, that failed during import, e.g. not enough stock -missing deliveries, that failed during import, e.g. not enough stock
To find these use the sql files in "sql reports" To find these use the sql files in "daily sql reports"
---------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------
Reimporting: Reimporting:

View File

@ -11,6 +11,16 @@ from email.mime.text import MIMEText
import shutil import shutil
import os import os
import base64
import google.auth
import pickle
# Gmail API utils
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://mail.google.com/']
THIS_DIRECTORY = pathlib.Path(__file__).parent THIS_DIRECTORY = pathlib.Path(__file__).parent
INCOMING_DIRECTORY = THIS_DIRECTORY / "incoming_orders" INCOMING_DIRECTORY = THIS_DIRECTORY / "incoming_orders"
@ -49,19 +59,55 @@ def main():
if file_count: if file_count:
file_alert(file_count) file_alert(file_count)
def gmail_authenticate():
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 availablle, 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('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)
return build('gmail', 'v1', credentials=creds)
def gmail_send_message(service, payload):
create_message = {"raw": payload}
# pylint: disable=E1101
send_message = (
service.users()
.messages()
.send(userId="me", body=create_message)
.execute()
)
return send_message
def file_alert(file_list): def file_alert(file_list):
file_string = ', '.join(file_list) file_string = ', '.join(file_list)
msg = MIMEMultipart() msg = MIMEMultipart()
msg['Subject'] = 'Source ecommerce: Files Left Over' msg['Subject'] = 'Source ecommerce: Files Left Over'
msg['Precedence'] = 'bulk' msg['Precedence'] = 'bulk'
msg['From'] = 'x3report@stashtea.com' msg['From'] = 'x3report@stashtea.com'
msg['To'] = 'bleeson@stashtea.com' msg['To'] = 'bleeson@stashtea.com,icarrera@yamamotoyama.com'
#msg['CC'] = 'bleeson@stashtea.com' #msg['CC'] = 'bleeson@stashtea.com'
emailtext = f'files: {file_string}' emailtext = f'files: {file_string}'
msg.attach(MIMEText(emailtext, 'plain')) msg.attach(MIMEText(emailtext, 'plain'))
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp: service = gmail_authenticate()
smtp.login(user='x3reportmk2@yamamotoyama.com', password=r'n</W<7fr"VD~\2&[pZc5') encoded_message = base64.urlsafe_b64encode(msg.as_bytes()).decode()
smtp.send_message(msg) gmail_send_message(service, encoded_message)
# with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
# smtp.login(user='x3reportmk2@yamamotoyama.com', password=r'n</W<7fr"VD~\2&[pZc5')
# smtp.send_message(msg)
if __name__ == "__main__": if __name__ == "__main__":