Sending mail via sendmail from python

前端 未结 8 1640
时光说笑
时光说笑 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条回答
  •  猫巷女王i
    2020-12-04 12:40

    Python 3.5+ version:

    import subprocess
    from email.message import EmailMessage
    
    def sendEmail(from_addr, to_addrs, msg_subject, msg_body):
        msg = EmailMessage()
        msg.set_content(msg_body)
        msg['From'] = from_addr
        msg['To'] = to_addrs
        msg['Subject'] = msg_subject
    
        sendmail_location = "/usr/sbin/sendmail"
        subprocess.run([sendmail_location, "-t", "-oi"], input=msg.as_bytes())
    

提交回复
热议问题