Python 从本地机器发送匿名邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24270715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:21:06  来源:igfitidea点击:

Send anonymous mail from local machine

pythonemailsmtpanonymous

提问by Nidhin Joseph

I was using Python for sending an email using an external SMTP server. In the code below, I tried using smtp.gmail.comto send an email from a gmail id to some other id. I was able to produce the output with the code below.

我使用 Python 使用外部 SMTP 服务器发送电子邮件。在下面的代码中,我尝试使用smtp.gmail.com从 gmail id 向其他 id 发送电子邮件。我能够使用下面的代码生成输出。

import smtplib
from email.MIMEText import MIMEText
import socket


socket.setdefaulttimeout(None)
HOST = "smtp.gmail.com"
PORT = "587"
sender= "[email protected]"
password = "pass"
receiver= "[email protected]"

msg = MIMEText("Hello World")

msg['Subject'] = 'Subject - Hello World'
msg['From'] = sender
msg['To'] = receiver

server = smtplib.SMTP()
server.connect(HOST, PORT)
server.starttls()
server.login(sender,password)
server.sendmail(sender,receiver, msg.as_string())
server.close()

But I have to do the same without the help of an external SMTP server. How can do the same with Python?
Please help.

但是我必须在没有外部 SMTP 服务器帮助的情况下做同样的事情。如何用 Python 做同样的事情?
请帮忙。

回答by Mansueli

The best way to achieve this is understand the Fake SMTPcode it uses the great smtpd module.

实现这一目标的最佳方法是了解它使用的伪造 SMTP代码smtpd module

#!/usr/bin/env python
"""A noddy fake smtp server."""

import smtpd
import asyncore

class FakeSMTPServer(smtpd.SMTPServer):
    """A Fake smtp server"""

    def __init__(*args, **kwargs):
        print "Running fake smtp server on port 25"
        smtpd.SMTPServer.__init__(*args, **kwargs)

    def process_message(*args, **kwargs):
        pass

if __name__ == "__main__":
    smtp_server = FakeSMTPServer(('localhost', 25), None)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        smtp_server.close()

To use this, save the above as fake_stmp.py and:

要使用它,请将上述内容另存为 fake_stmp.py 并:

chmod +x fake_smtp.py
sudo ./fake_smtp.py

If you really want to go into more details, then I suggest that you understand the source code of that module.

如果您真的想深入了解,那么我建议您了解该模块的源代码。

If that doesn't work try the smtplib:

如果这不起作用,请尝试 smtplib:

import smtplib

SERVER = "localhost"

FROM = "[email protected]"
TO = ["[email protected]"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

回答by mti2935

Most likely, you may already have an SMTP server running on the host that you are working on. If you do ls -l /usr/sbin/sendmaildoes it show that an executable file (or symlink to another file) exists at this location? If so, then you may be able to use this to send outgoing mail. Try /usr/sbin/sendmail [email protected] < /path/to/file.txtto send the message contained in /path/to/file.txt to [email protected] (/path/to/file.txt should be an RFC-compliant email message). If that works, then you can use /usr/sbin/sendmail to send mail from your python script - either by opening a handle to /usr/sbin/sendmail and writing the message to it, or simply by executing the above command from your python script by way of a system call.

最有可能的是,您可能已经在正在使用的主机上运行了 SMTP 服务器。如果这样做ls -l /usr/sbin/sendmail,它是否表明此位置存在可执行文件(或指向另一个文件的符号链接)?如果是这样,那么您可以使用它来发送外发邮件。尝试/usr/sbin/sendmail [email protected] < /path/to/file.txt将包含在 /path/to/file.txt 中的消息发送到 [email protected](/path/to/file.txt 应该是符合 RFC 标准的电子邮件)。如果可行,那么您可以使用 /usr/sbin/sendmail 从您的 python 脚本发送邮件 - 通过打开 /usr/sbin/sendmail 的句柄并将消息写入其中,或者只需从您的python脚本通过系统调用的方式。