880c67d1b26b0f58dc5d6edeca6d9187676ad428 — emerson 25 days ago 0151ec1
make it mailgun http api
2 files changed, 24 insertions(+), 24 deletions(-)

M dronebl/config.example.yaml
M dronebl/dronebl_check_tickets.py
M dronebl/config.example.yaml => dronebl/config.example.yaml +2 -0
@@ 5,6 5,8 @@ dbhost: "127.0.0.1"
 dronebl_key: something
 dronebl_account_name: something
 dronebl_account_password: something
+mg_domain_name: something
+mg_api_key: something
 email_host: something
 email_port: 587
 email_username: something

M dronebl/dronebl_check_tickets.py => dronebl/dronebl_check_tickets.py +22 -24
@@ 1,8 1,6 @@
-import email
 import logging
 import psycopg2
 import requests
-import smtplib
 import time
 import yaml
 from bs4 import BeautifulSoup


@@ 12,9 10,6 @@ config = yaml.full_load(open("config.yaml"))
 pgconn = psycopg2.connect(dbname=config["dbname"], user=config["dbuser"], password=config["dbpassword"], host=config["dbhost"])
 key = config["dronebl_key"]
 entries_to_remove = []
-smtp_connection = smtplib.SMTP(config["email_host"], config["email_port"])
-smtp_connection.starttls()
-smtp_connection.login(config["email_username"], config["email_password"])
 debug_log = logging.getLogger("dronebl_check_tickets")
 debug_log.setLevel(logging.DEBUG)
 debug_log_file_handler = logging.FileHandler("dronebl_check_tickets_debug.log")


@@ 26,28 21,32 @@ s = requests.Session()
 headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"}
 
 def send_active_email(ticket, details):
-	msg = email.message.EmailMessage()
-	msg["From"] = config["email_from"]
-	msg["Date"] = email.utils.localtime()
+	auth_api = ("api", config["mg_api_key"])
+	data = {
+		"from": config["email_from"],
+		"to": ticket["email"],
+		"subject": f"DroneBL removal request for {ticket['ip']} denied",
+		"text": config["email_active"].format(ticket["ip"], details),
+	}
 	if config.get("email_bcc"):
-		msg["Bcc"] = config["email_bcc"]
-	msg["Message-ID"] = email.utils.make_msgid(domain=config["email_domain"])
-	msg["To"] = ticket["email"]
-	msg["Subject"] = f"DroneBL removal request for {ticket['ip']} denied"
-	msg.set_content(config["email_active"].format(ticket["ip"], details))
-	smtp_connection.send_message(msg)
+		data["bcc"] = config["email_bcc"]
+
+	requests.post(f"https://api.mailgun.net/v3/config['mg_domain_name']/messages",
+			auth=auth_api, data=data)
 
 def send_inactive_email(ticket):
-	msg = email.message.EmailMessage()
-	msg["From"] = config["email_from"]
-	msg["Date"] = email.utils.localtime()
+	auth_api = ("api", config["mg_api_key"])
+	data = {
+		"from": config["email_from"],
+		"to": ticket["email"],
+		"subject": f"DroneBL entry for {ticket['ip']} removed",
+		"text": config["email_inactive"].format(ticket["ip"]),
+	}
 	if config.get("email_bcc"):
-		msg["Bcc"] = config["email_bcc"]
-	msg["Message-ID"] = email.utils.make_msgid(domain=config["email_domain"])
-	msg["To"] = ticket["email"]
-	msg["Subject"] = f"DroneBL entry for {ticket['ip']} removed"
-	msg.set_content(config["email_inactive"].format(ticket["ip"]))
-	smtp_connection.send_message(msg)
+		data["bcc"] = config["email_bcc"]
+
+	requests.post(f"https://api.mailgun.net/v3/config['mg_domain_name']/messages",
+			auth=auth_api, data=data)
 
 def remove_dronebl_entries(entries):
 	request = "\n".join(entries)


@@ 128,6 127,5 @@ with pgconn:
 				send_inactive_email(ticket)
 				time.sleep(5)
 
-smtp_connection.quit()
 if entries_to_remove:
 	remove_dronebl_entries(entries_to_remove)