Update main.py
This commit is contained in:
parent
eaab87febe
commit
99f92c077b
64
main.py
64
main.py
@ -8,22 +8,27 @@ from email import encoders
|
||||
import os
|
||||
|
||||
|
||||
def perform_query(query, db_config):
|
||||
def connect_db(sql_query):
|
||||
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.execute(query)
|
||||
cursor.execute(sql_query)
|
||||
|
||||
results = cursor.fetchall()
|
||||
column_headers = [i[0] for i in cursor.description]
|
||||
return results, column_headers
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error: {e}")
|
||||
finally:
|
||||
if connection.is_connected():
|
||||
cursor.close()
|
||||
connection.close()
|
||||
print(f"Error connecting to DB: {e}")
|
||||
return None
|
||||
|
||||
|
||||
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}")
|
||||
|
||||
|
||||
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:
|
||||
msg = MIMEMultipart()
|
||||
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'))
|
||||
|
||||
with open(file_path, 'rb') as attachment:
|
||||
with open(filename, 'rb') as attachment:
|
||||
part = MIMEBase('application', 'octet-stream')
|
||||
part.set_payload(attachment.read())
|
||||
encoders.encode_base64(part)
|
||||
part.add_header(
|
||||
'Content-Disposition',
|
||||
f'attachment; filename={os.path.basename(file_path)}'
|
||||
f'attachment; filename={os.path.basename(filename)}'
|
||||
)
|
||||
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.login(smtp_user, smtp_password)
|
||||
server.send_message(msg)
|
||||
server.quit()
|
||||
print("Email sent!")
|
||||
|
||||
except Exception as e:
|
||||
@ -68,24 +74,30 @@ def send_email(subject, body, recipient_email, sender_email, sender_password, fi
|
||||
|
||||
|
||||
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"
|
||||
write_to_excel(results, headers, excel_file_name)
|
||||
|
||||
email_subject = "Your subject"
|
||||
email_body = "Please find attached bla bla"
|
||||
recipient = "mark@42tele.com"
|
||||
sender = "zobbi@sender.com"
|
||||
|
||||
send_email(email_subject, email_body, recipient, sender, excel_file_name)
|
||||
subject = "Mexedia pricelist"
|
||||
body = "Please find attached your pricelist"
|
||||
recipient_email = "mark@42tele.com"
|
||||
sender_email = "mark@42tele.com"
|
||||
smtp_server = "smtp.eu.mailgun.org"
|
||||
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
|
||||
)
|
||||
Loading…
Reference in New Issue
Block a user