Sendmail Errno[61] Connection Refused

前端 未结 7 1115
不思量自难忘°
不思量自难忘° 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!

    0 讨论(0)
  • 2021-01-31 08:32

    For whatever reason, I had difficulty passing server and port to the constructor, but not the connect function. This ended up working for me:

        s = smtplib.SMTP(timeout=30) #seconds
        s.connect(EMAIL_HOST, EMAIL_PORT)
        m = MIMEText('see subject')
        m['subject'] = 'sweet subject'
        m['from'] = EMAIL_FROM
        m['to'] = to_list  # comma-separated list of emails
        s.sendmail(m['from'], m['to'].split(','), m.as_string())
        s.quit()
    
    0 讨论(0)
  • 2021-01-31 08:33

    If you are root on your system then you may want to install opensmtpd. First this way you don't need to run the server manually (this service is enabled by default so after smtpd installation either start it manually or reboot your machine). Second, you don't need to change the line server = smtplib.SMTP(SERVER). To conclude, use yum install opensmtpd or the equivalent apt-get command.

    0 讨论(0)
  • 2021-01-31 08:34

    If you don't want to run a separate server, and if you're only using Unix, you can use this technique, copied from http://www.yak.net/fqa/84.html, and originally from the Python FAQ:

    On Unix, it's very simple, using sendmail. The location of the sendmail program varies between systems; sometimes it is /usr/lib/sendmail, sometime /usr/sbin/sendmail. The sendmail manual page will help you out. Here's some sample code:

    SENDMAIL = "/usr/sbin/sendmail" # sendmail location
    import os
    p = os.popen("%s -t" % SENDMAIL, "w")
    p.write("To: cary@ratatosk.org\n")
    p.write("Subject: test\n")
    p.write("\n") # blank line separating headers from body
    p.write("Some text\n")
    p.write("some more text\n")
    sts = p.close()
    if sts != 0:
        print "Sendmail exit status", sts
    
    0 讨论(0)
  • 2021-01-31 08:37

    Start a simple SMTP server with Python like so:

    python -m smtpd -n -c DebuggingServer localhost:1025
    
    0 讨论(0)
  • 2021-01-31 08:40

    If you start a local server as follows:

    python -m smtpd -n -c DebuggingServer localhost:1025

    Make sure to modify the mail-sending code to use the non-standard port number:

    server = smtplib.SMTP(SERVER, 1025)
    server.sendmail(FROM, TO, message)
    server.quit()
    
    0 讨论(0)
提交回复
热议问题