문제

SMTP가 아닌 sendmail을 통해 메일을 보내려는 경우 이 프로세스를 캡슐화하는 Python용 라이브러리가 있습니까?

더 나은 점은 전체 'sendmail - vs. smtp' 선택을 추상화하는 좋은 라이브러리가 있습니까?

나는 이 스크립트를 다수의 Unix 호스트에서 실행할 예정인데, 그 중 일부만 localhost:25를 수신하고 있습니다.이들 중 일부는 임베디드 시스템의 일부이므로 SMTP를 허용하도록 설정할 수 없습니다.

모범 사례의 일환으로 저는 라이브러리가 헤더 주입 취약점 자체를 처리하도록 하고 싶습니다. popen('/usr/bin/sendmail', 'w') 내가 원하는 것보다 금속에 조금 더 가깝습니다.

대답이 '가서 라이브러리를 작성하세요'라면 그렇게 하세요 ;-)

도움이 되었습니까?

해결책

헤더 삽입은 메일을 보내는 방법에 영향을 미치는 요소가 아니라 메일을 구성하는 방법에 영향을 미치는 요소입니다.을 체크 해봐 이메일 패키지를 만들고, 이를 사용하여 메일을 구성하고, 직렬화하여 다음으로 보냅니다. /usr/sbin/sendmail 사용하여 하위 프로세스 기준 치수:

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)
p.communicate(msg.as_string())

다른 팁

이것은 메일을 전달하기 위해 유닉스 sendmail을 사용하는 간단한 파이썬 함수입니다.

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

Jim의 답변은 Python 3.4에서 작동하지 않았습니다.추가로 추가해야 했어요 universal_newlines=True 주장 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())

없이 universal_newlines=True 나는 얻다

TypeError: 'str' does not support the buffer interface

os.popen을 사용하여 Python에서 sendmail 명령을 사용하는 것이 매우 일반적입니다.

개인적으로, 내가 직접 작성하지 않은 스크립트의 경우 Windows에서 실행하기 위해 sendmail 복제본을 설치할 필요가 없기 때문에 SMTP 프로토콜을 사용하는 것이 더 낫다고 생각합니다.

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

이 질문은 매우 오래된 질문이지만 이라는 메시지 구성 및 이메일 전달 시스템이 있다는 점에 유의할 가치가 있습니다. 골수 우편물 (이전의 TurboMail)은 이 메시지가 요청되기 전부터 사용 가능했습니다.

이제 Python 3을 지원하도록 이식되었으며 다음의 일부로 업데이트되었습니다. 골수 모음곡.

나는 똑같은 것을 검색하고 있었고 Python 웹 사이트에서 좋은 예를 찾았습니다. http://docs.python.org/2/library/email-examples.html

언급된 사이트에서:

# 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()

이를 위해서는 "localhost"에서의 연결을 허용하도록 sendmail/mailx를 올바르게 설정해야 합니다.이는 기본적으로 Mac, Ubuntu 및 Redhat 서버에서 작동하지만 문제가 발생하면 다시 확인하는 것이 좋습니다.

가장 쉬운 대답은 smtplib입니다. 여기서 문서를 찾을 수 있습니다. 여기.

당신이 해야 할 일은 localhost로부터의 연결을 수락하도록 로컬 sendmail을 구성하는 것뿐입니다. 이는 아마도 기본적으로 이미 수행하고 있을 것입니다.물론, 전송을 위해 여전히 SMTP를 사용하고 있지만 이는 기본적으로 명령줄 도구를 사용하는 것과 동일한 로컬 sendmail입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top