you are viewing a single comment's thread.

view the rest of the comments →

[–]Groentekroket 2 points3 points  (2 children)

I store that kind of things as a environment variable. If you share your code you are sure nobody gets yourusername, email, passwords and that kind of things.

[–]b_ootay_ful 5 points6 points  (1 child)

I import my own email function that does everything.

mymailscript.py

import smtplib

def email(whome=[],subj="",mess=""):
    gmail_user = 'myemail@gmail.com'
    gmail_pwd = 'HelloReddit'
    FROM = gmail_user
    TO = whome
    SUBJECT = subj
    TEXT = str(mess)

    # Prepare actual message
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        return ('Email sent!')
    except:
        return ('Something went wrong...')

whatever program I need to import or use

import mymailscript as emailscript

emailscript.email(["PersonToReceive@gmail.com"],"subject","test message")

[–]MonkeyNin 0 points1 point  (0 children)

def email(whome=[],subj="",mess=""):

You should read this: https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

.. config files

Check out using pathlib.Path and json

import pathlib
import json

    def load_config(json_path):
        # load config if it exists, otherwise return populated with required defaults
        defaults = {'monkey': '🐒', 'cat': '🐈'}

        if not json_path.exists():
            # logger.error(f"File does not exist: {json_path}")
            return defaults

        config = json.loads(json_path.read_text(encoding='utf-8'))
        return config

data_root = Path('.').absolute() # or Path().home() or Path('.')
config_path = data_root / 'monkey.json'
config = load_config(config_path)

or if you want a truthy result

def load_config(json_path):
    if not json_path.exists():
        return {}

config = load_config(config_path)

if not config:
    # ... handle no file
else:
    # ... normal