从本地计算机发送匿名邮件 [英] Send anonymous mail from local machine

查看:164
本文介绍了从本地计算机发送匿名邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python通过外部SMTP服务器发送电子邮件。在下面的代码中,我尝试使用 smtp.gmail.com 将电子邮件从gmail ID发送到其他ID。我可以使用下面的代码生成输出。

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

import smtplib
from email.MIMEText import MIMEText
import socket


socket.setdefaulttimeout(None)
HOST = "smtp.gmail.com"
PORT = "587"
sender= "somemail@gmail.com"
password = "pass"
receiver= "receiver@somedomain.com"

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

但是我必须做在没有外部SMTP服务器帮助的情况下也是如此。使用Python怎么做?

请帮忙。

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 代码,它使用了出色的 smtpd模块

The best way to achieve this is understand the Fake SMTP code it uses the great 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()

要使用此功能,请将以上内容另存为fake_stmp.py和:

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

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.

如果那不工作尝试smtplib:

If that doesn't work try the smtplib:

import smtplib

SERVER = "localhost"

FROM = "sender@example.com"
TO = ["user@example.com"] # 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()

这篇关于从本地计算机发送匿名邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆