Gr8/mt5emaildets.py
2025-09-10 15:02:31 +03:00

114 lines
3.2 KiB
Python

import MetaTrader5 as mt5
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import json
import os
# ----------------------------
# Load settings
# ----------------------------
def load_config(filepath="config.json"):
if not os.path.exists(filepath):
raise FileNotFoundError(f"Config file not found: {filepath}")
with open(filepath, "r") as f:
return json.load(f)
# ----------------------------
# Send email
# ----------------------------
def send_email(subject, body, smtp_config):
msg = MIMEMultipart()
msg['From'] = smtp_config["sender_email"]
msg['To'] = smtp_config["recipient_email"]
msg['Subject'] = subject
msg.attach(MIMEText(body, "plain"))
try:
with smtplib.SMTP(smtp_config["smtp_server"], smtp_config["smtp_port"]) as server:
server.starttls()
server.login(smtp_config["sender_email"], smtp_config["sender_password"])
server.send_message(msg)
print(f"[EMAIL] Sent to {smtp_config['recipient_email']}")
except Exception as e:
print(f"[EMAIL ERROR] {e}")
# ----------------------------
# Get account details
# ----------------------------
def get_account_details():
info = mt5.account_info()
if info is None:
raise RuntimeError(f"Unable to fetch account info. Error: {mt5.last_error()}")
# Build a clean text message
details = (
f"MT5 ACCOUNT DETAILS\n"
f"===================\n"
f"Login: {info.login}\n"
f"Name: {info.name}\n"
f"Server: {info.server}\n"
f"Balance: {info.balance}\n"
f"Equity: {info.equity}\n"
f"Leverage: {info.leverage}\n"
f"Currency: {info.currency}\n"
f"Trade Mode: {info.trade_mode}\n"
)
return details
# ----------------------------
# Main function
# ----------------------------
def main():
config = load_config("config.json")
smtp_config = {
"smtp_server": "smtp.gmail.com",
"smtp_port": 587,
"sender_email": "ifairvaluegap@gmail.com", # your Gmail
"sender_password": "pyxv pjnd jftb klnz", # Gmail App Password
"recipient_email": "griffinskimutai@gmail.com" # where to send account details
}
# Initialize MT5
if mt5.initialize(
path=config["mt5Pathway"],
login=int(config["username"]),
password=config["password"],
server=config["server"]
):
print("[MT5] Connection successful")
try:
account_text = get_account_details()
send_email(
subject="MT5 Account Details",
body=account_text,
smtp_config=smtp_config
)
except Exception as e:
send_email(
subject="MT5 Account Info Error",
body=str(e),
smtp_config=smtp_config
)
else:
error_msg = f"[MT5] Connection failed: {mt5.last_error()}"
print(error_msg)
send_email(
subject="MT5 Connection Failed",
body=error_msg,
smtp_config=smtp_config
)
mt5.shutdown()
# ----------------------------
# Run script
# ----------------------------
if __name__ == "__main__":
main()