A simple SMTP server (in Python)

2019-01-21 19:50发布

Could you please suggest a simple SMTP server with the very basic APIs (by very basic I mean, to read, write, delete email), that could be run on a linux box? I just need to convert the crux of the email into XML format and FTP it to another machine.

标签: python smtp
8条回答
看我几分像从前
2楼-- · 2019-01-21 20:29

If you want to quickly test Django's send_mail with hasen's answer above:

# Skip lines 3 and 4 if not using virtualenv.
# At command prompt

mkdir django1
cd django1
virtualenv venv
source venv/bin/activate
pip install django==1.11
django-admin startproject django1 .

# run the Django shell

python manage.py shell

# paste into shell following:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)
# This should write an email like the following:

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Subject here
From: from@example.com
To: to@example.com
Date: Wed, 02 May 2018 02:12:09 -0000
Message-ID: <20180502021209.32641.51865@i1022>

Here is the message.

Not necessary to have valid values in send_mail function. Above values will work just fine with hasen's example.

查看更多
干净又极端
3楼-- · 2019-01-21 20:30

Take a look at this SMTP sink server:

from datetime import datetime
import asyncore
from smtpd import SMTPServer

class EmlServer(SMTPServer):
    no = 0
    def process_message(self, peer, mailfrom, rcpttos, data):
        filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),
                self.no)
        f = open(filename, 'w')
        f.write(data)
        f.close
        print '%s saved.' % filename
        self.no += 1


def run():
    foo = EmlServer(('localhost', 25), None)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    run()

It uses smtpd.SMTPServer to dump emails to files.

查看更多
我命由我不由天
4楼-- · 2019-01-21 20:33

To get Hasen's script working in Python 3 I had to tweak it slightly:

from datetime import datetime
import asyncore
from smtpd import SMTPServer

class EmlServer(SMTPServer):
    no = 0
    def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
        filename = '%s-%d.eml' % (datetime.now().strftime('%Y%m%d%H%M%S'),
            self.no)
        print(filename)
        f = open(filename, 'wb')
        f.write(data)
        f.close
        print('%s saved.' % filename)
        self.no += 1

def run():
    EmlServer(('localhost', 25), None)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    run()
查看更多
姐就是有狂的资本
5楼-- · 2019-01-21 20:35

There are really 2 things required to send an email:

  • An SMTP Server - This can either be the Python SMTP Server or you can use GMail or your ISP's server. Chances are you don't need to run your own.
  • An SMTP Library - Something that will send an email request to the SMTP server. Python ships with a library called smtplib that can do that for you. There is tons of information on how to use it here: http://docs.python.org/library/smtplib.html

For reading, there are two options depending on what server you are reading the email from.

查看更多
Anthone
6楼-- · 2019-01-21 20:42

A more modern approach is to use the aiosmtpd library (documentation available here).

You can find a good example here: https://aiosmtpd.readthedocs.io/en/latest/aiosmtpd/docs/controller.html.

查看更多
三岁会撩人
7楼-- · 2019-01-21 20:46

These are nice examples for a start.

smtpd – Sample SMTP Servers

http://pymotw.com/2/smtpd/index.html

smtplib – Simple Mail Transfer Protocol client

http://pymotw.com/2/smtplib/index.html

查看更多
登录 后发表回答