Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29748

Send the contents from unmodified print statement by e-mail in python

$
0
0

I have a script that runs main() and at the end I want to send the contents it has by e-mail. I don't want to write new files nor anything. Just have the original script be unmodified and at the end just send the contents of what it printed. Ideal code:

main()send_mail()

I tried this:

def main():  print('HELLOWORLD')def send_email(subject='subject', message='', destination='me@gmail.com', password_path=None):    from socket import gethostname    from email.message import EmailMessage    import smtplib    import json    import sys    server = smtplib.SMTP('smtp.gmail.com', 587)    smtplib.stdout = sys.stdout # <<<<<-------- why doesn't it work?    server.starttls()    with open(password_path) as f:        config = json.load(f)        server.login('me@gmail.com', config['password'])        # craft message        msg = EmailMessage()        #msg.set_content(message)        msg['Subject'] = subject        msg['From'] = 'me@gmail.com'        msg['To'] = destination        # send msg        server.send_message(msg)if __name__ == '__main__':    main()    send_mail()

but it doesn't work.

I don't want to write other files or change the original python print statements. How to do this?


I checked all of the following links but none helped:


Viewing all articles
Browse latest Browse all 29748

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>