Sending mail via sendmail from python

前端 未结 8 1622
时光说笑
时光说笑 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:19

    Header injection isn't a factor in how you send the mail, it's a factor in how you construct the mail. Check the email package, construct the mail with that, serialise it, and send it to /usr/sbin/sendmail using the subprocess module:

    import sys
    from email.mime.text import MIMEText
    from subprocess import Popen, PIPE
    
    
    msg = MIMEText("Here is the body of my message")
    msg["From"] = "me@example.com"
    msg["To"] = "you@example.com"
    msg["Subject"] = "This is the subject."
    p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
    # Both Python 2.X and 3.X
    p.communicate(msg.as_bytes() if sys.version_info >= (3,0) else msg.as_string()) 
    
    # Python 2.X
    p.communicate(msg.as_string())
    
    # Python 3.X
    p.communicate(msg.as_bytes())
    
    0 讨论(0)
  • 2020-12-04 12:19

    I was just searching around for the same thing and found a good example on the Python website: http://docs.python.org/2/library/email-examples.html

    From the site mentioned:

    # Import smtplib for the actual sending function
    import smtplib
    
    # Import the email modules we'll need
    from email.mime.text import MIMEText
    
    # Open a plain text file for reading.  For this example, assume that
    # the text file contains only ASCII characters.
    fp = open(textfile, 'rb')
    # Create a text/plain message
    msg = MIMEText(fp.read())
    fp.close()
    
    # me == the sender's email address
    # you == the recipient's email address
    msg['Subject'] = 'The contents of %s' % textfile
    msg['From'] = me
    msg['To'] = you
    
    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    s = smtplib.SMTP('localhost')
    s.sendmail(me, [you], msg.as_string())
    s.quit()
    

    Note that this requires that you have sendmail/mailx set up correctly to accept connections on "localhost". This works on my Mac, Ubuntu and Redhat servers by default, but you may want to double-check if you run into any issues.

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

    The easiest answer is the smtplib, you can find docs on it here.

    All you need to do is configure your local sendmail to accept connection from localhost, which it probably already does by default. Sure, you're still using SMTP for the transfer, but it's the local sendmail, which is basically the same as using the commandline tool.

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

    This question is very old, but it's worthwhile to note that there is a message construction and e-mail delivery system called Marrow Mailer (previously TurboMail) which has been available since before this message was asked.

    It's now being ported to support Python 3 and updated as part of the Marrow suite.

    0 讨论(0)
  • 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())
    
    0 讨论(0)
  • 2020-12-04 12:41

    Jim's answer did not work for me in Python 3.4. I had to add an additional universal_newlines=True argument to subrocess.Popen()

    from email.mime.text import MIMEText
    from subprocess import Popen, PIPE
    
    msg = MIMEText("Here is the body of my message")
    msg["From"] = "me@example.com"
    msg["To"] = "you@example.com"
    msg["Subject"] = "This is the subject."
    p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True)
    p.communicate(msg.as_string())
    

    Without the universal_newlines=True I get

    TypeError: 'str' does not support the buffer interface
    
    0 讨论(0)
提交回复
热议问题