Sending mail via sendmail from python

前端 未结 8 1623
时光说笑
时光说笑 2020-12-04 12:04

If I want to send mail not via SMTP, but rather via sendmail, is there a library for python that encapsulates this process?

Better yet, is there a good library that

相关标签:
8条回答
  • 2020-12-04 12:43

    It's quite common to just use the sendmail command from Python using os.popen

    Personally, for scripts i didn't write myself, I think just using the SMTP-protocol is better, since it wouldn't require installing say an sendmail clone to run on windows.

    https://docs.python.org/library/smtplib.html

    0 讨论(0)
  • 2020-12-04 12:44

    This is a simple python function that uses the unix sendmail to deliver a mail.

    def sendMail():
        sendmail_location = "/usr/sbin/sendmail" # sendmail location
        p = os.popen("%s -t" % sendmail_location, "w")
        p.write("From: %s\n" % "from@somewhere.com")
        p.write("To: %s\n" % "to@somewhereelse.com")
        p.write("Subject: thesubject\n")
        p.write("\n") # blank line separating headers from body
        p.write("body of the mail")
        status = p.close()
        if status != 0:
               print "Sendmail exit status", status
    
    0 讨论(0)
提交回复
热议问题