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

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

问题描述

我使用 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天全站免登陆