Update main.py

This commit is contained in:
Mark 2025-01-22 22:44:10 +01:00
parent eaab87febe
commit 99f92c077b

64
main.py
View File

@ -8,22 +8,27 @@ from email import encoders
import os import os
def perform_query(query, db_config): def connect_db(sql_query):
try: try:
connection = mysql.connector.connect(**db_config) connection = mysql.connector.connect(
host='192.168.145.196',
user='sql_to_xlsx',
password='2Uce5Uc5',
database='smsgw_control',
auth_plugin='mysql_native_password',
ssl_disabled=True,
use_pure=True
)
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(query) cursor.execute(sql_query)
results = cursor.fetchall() results = cursor.fetchall()
column_headers = [i[0] for i in cursor.description] column_headers = [i[0] for i in cursor.description]
return results, column_headers return results, column_headers
except mysql.connector.Error as e: except mysql.connector.Error as e:
print(f"Error: {e}") print(f"Error connecting to DB: {e}")
finally: return None
if connection.is_connected():
cursor.close()
connection.close()
def write_to_excel(data, headers, file_name): def write_to_excel(data, headers, file_name):
@ -38,7 +43,7 @@ def write_to_excel(data, headers, file_name):
print(f"Excel file saved as {file_name}") print(f"Excel file saved as {file_name}")
def send_email(subject, body, recipient_email, sender_email, sender_password, file_path): def send_email(subject, body, recipient_email, sender_email, filename, smtp_server, smtp_port, smtp_user, smtp_password):
try: try:
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = sender_email msg['From'] = sender_email
@ -47,20 +52,21 @@ def send_email(subject, body, recipient_email, sender_email, sender_password, fi
msg.attach(MIMEText(body, 'plain')) msg.attach(MIMEText(body, 'plain'))
with open(file_path, 'rb') as attachment: with open(filename, 'rb') as attachment:
part = MIMEBase('application', 'octet-stream') part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read()) part.set_payload(attachment.read())
encoders.encode_base64(part) encoders.encode_base64(part)
part.add_header( part.add_header(
'Content-Disposition', 'Content-Disposition',
f'attachment; filename={os.path.basename(file_path)}' f'attachment; filename={os.path.basename(filename)}'
) )
msg.attach(part) msg.attach(part)
with smtplib.SMTP('smtp.eu.mailgun.org', 587) as server: with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() server.starttls()
server.login(smtp_user, smtp_password) server.login(smtp_user, smtp_password)
server.send_message(msg) server.send_message(msg)
server.quit()
print("Email sent!") print("Email sent!")
except Exception as e: except Exception as e:
@ -68,24 +74,30 @@ def send_email(subject, body, recipient_email, sender_email, sender_password, fi
if __name__ == "__main__": if __name__ == "__main__":
db_config = {
'host': '192.168.145.196',
'user': 'sql_to_xlsx',
'password': '2Uce5Uc5',
'database': 'smsgw_control'
}
sql_query = "SELECT mcc AS MCC, mnc AS MNC, route, price FROM smsgw_control.routingprices WHERE route = 'M1' AND clientid = '13283';" sql_query = """SELECT
countryname_eng as Countryname, ifnull(network, 'Default') as Network, routingprices.mcc as MCC, routingprices.mnc as MNC, route, price
FROM
smsgw_control.routingprices
LEFT JOIN smsgw_control.E212_MCC ON routingprices.mcc = smsgw_control.E212_MCC.MCC
LEFT JOIN smsgw_control.E212_MCCMNC on routingprices.mcc = smsgw_control.E212_MCCMNC.MCC and routingprices.mnc = smsgw_control.E212_MCCMNC.MNC
WHERE
route = 'M1' AND clientid = '13283';"""
results, headers = perform_query(sql_query, db_config) results, headers = connect_db(sql_query)
excel_file_name = "pricelist.xlsx" excel_file_name = "pricelist.xlsx"
write_to_excel(results, headers, excel_file_name) write_to_excel(results, headers, excel_file_name)
email_subject = "Your subject" subject = "Mexedia pricelist"
email_body = "Please find attached bla bla" body = "Please find attached your pricelist"
recipient = "mark@42tele.com" recipient_email = "mark@42tele.com"
sender = "zobbi@sender.com" sender_email = "mark@42tele.com"
smtp_server = "smtp.eu.mailgun.org"
send_email(email_subject, email_body, recipient, sender, excel_file_name) smtp_port = 587
smtp_user = 'stps@monitoring.fortytwo.mt'
smtp_password = 'f18c48a7076c868bef91eb8f0ca59d33-9776af14-d2648a16'
send_email(
subject, body, recipient_email, sender_email, excel_file_name, smtp_server, smtp_port, smtp_user, smtp_password
)