Sendmail Errno[61] Connection Refused

前端 未结 7 1133
不思量自难忘°
不思量自难忘° 2021-01-31 08:28

I\'ve been trying to get my application to mail some outputted text to an email. For simplification I have isolated the script :

import smtplib
import sys
import         


        
7条回答
  •  庸人自扰
    2021-01-31 08:31

    I wanted to create something so that you could just copy paste it and have it work but this is the closest I got:

    from email.message import EmailMessage
    import smtplib
    import os
    
    def send_email(message,destination):
        # important, you need to send it to a server that knows how to send e-mails for you
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        # don't know how to do it without cleartexting the password and not relying on some json file that you dont git control...
        server.login('valid.username@gmail.com', 'password_for_gmail')
        msg = EmailMessage()
        msg.set_content(message)
    
        msg['Subject'] = 'TEST'
        msg['From'] = 'valid.username@gmail.com'
        msg['To'] = destination
        server.send_message(msg)
    
    if __name__ == '__main__':
        send_email('msg','destination@email')
    

    I feel the tutorial is misleading because it assumes without telling you very well that you already have a running server that sends e-mails for you...its odd. The only issue with my script is that I dont know how to make it work without having the cleartext password just written there but alas...at least it sends it? Just make a fake e-mail address or something...


    made this question long time ago, so I don't remember what this means exactly put will put it here just in case:

    It works only if you enable access for less secure apps: myaccount.google.com/lesssecureapps . I think you should put that in answer.

    I probably went around it by using a fake email only for that or somehting like that or an email from my org can't remember. Good luck!

提交回复
热议问题