Last active
January 28, 2021 06:16
-
-
Save jayveeangeles/680ff19c82c0b9ba965b970b126c69d5 to your computer and use it in GitHub Desktop.
Python3 script to send IP using Gmail SMTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import socket | |
import argparse | |
import json | |
import logging | |
import os, sys | |
import smtplib | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
def get_ip_address(remote_server='8.8.8.8', port=80) -> str: | |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | |
s.connect((remote_server, port)) | |
return s.getsockname()[0] | |
def parse_cfg_file(config: str) -> dict: | |
cfg_dict = {} | |
cfg_stripped = config.split('\n') | |
for item in cfg_stripped: | |
if item: | |
key, val = item.split('=') | |
if key and val: | |
cfg_dict[key] = val | |
return cfg_dict | |
def create_email_message(from_address, to_address, subject, body) -> MIMEText: | |
# msg = MIMEText(body) | |
msg = MIMEMultipart('alternative') | |
msg['From'] = from_address | |
msg['To'] = from_address | |
msg['Subject'] = subject | |
html = ( | |
f"<html>" | |
f"<head></head>" | |
f"<body>" | |
f"<p>Hi!<br>" | |
f"{body}" | |
f"</p>" | |
f"<p>Regards,<br>{from_address}</p>" | |
f"</body>" | |
f"</html>" | |
) | |
html_body = MIMEText(html, 'html') | |
msg.attach(html_body) | |
return msg | |
def send_email(msg: MIMEText, config: dict) -> None: | |
if int(config['MAIL_PORT']) == 25: | |
with smtplib.SMTP(config['MAIL_SMTP'], port=int(config['MAIL_PORT'])) as s: | |
s.ehlo() | |
s.sendmail(config['MAIL_USER'], [config['MAIL_USER']] + \ | |
config['MAIL_LIST'].split(','), msg.as_string()) | |
else: | |
with smtplib.SMTP_SSL(config['MAIL_SMTP'], port=int(config['MAIL_PORT'])) as s: | |
s.ehlo() | |
s.login(config['MAIL_USER'], config['MAIL_PASS']) | |
s.sendmail(config['MAIL_USER'], [config['MAIL_USER']] + \ | |
config['MAIL_LIST'].split(','), msg.as_string()) | |
def main() -> None: | |
logging.info('getting IP address of main NIC') | |
email_address = get_ip_address() | |
logging.info('getting config') | |
cfg_dict = parse_cfg_file(cfg_string) | |
logging.info('creating email message') | |
email_msg = create_email_message( | |
from_address=cfg_dict['MAIL_USER'], | |
to_address=cfg_dict['MAIL_LIST'], | |
subject=f"IP Address from {socket.gethostname()}", | |
body=f"Please connect to {email_address}" | |
) | |
try: | |
send_email(email_msg, cfg_dict) | |
except Exception as e: | |
logging.error(f"encountered error while trying to send email: {e}") | |
sys.exit(-1) | |
else: | |
logging.info('email sent!') | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-e", "--env", default='./.env.config', | |
help="config file for arguments") | |
args = parser.parse_args() | |
with open(args.env) as f: | |
cfg_string = f.read() | |
format = "%(asctime)s (%(lineno)4d) [%(levelname)-.3s]: %(message)s" | |
logging.basicConfig(format=format, \ | |
level=logging.DEBUG) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment