Testing email sending

2019-03-08 06:41发布

Any tips on testing email sending? Other than maybe creating a gmail account, especially for receiving those emails?

I would like to, maybe, store the emails locally, within a folder as they are sent.

13条回答
再贱就再见
2楼-- · 2019-03-08 07:25

https://websocket.email provides a simple way to test email sending with minimal setup (you don't even need an account).

查看更多
疯言疯语
3楼-- · 2019-03-08 07:28

You can use a file backend for sending emails which is a very handy solution for development and testing; emails are not sent but stored in a folder you can specify!

查看更多
唯我独甜
4楼-- · 2019-03-08 07:28

Use Maildump.

https://github.com/ThiefMaster/maildump

MailDump is a python-based clone of the awesome MailCatcher tool. Its purpose is to provide developers a way to let applications send emails without actual emails being sent to anyone. Additionally lazy developers might prefer this over a real SMTP server simply for the sake of it being much easier and faster to set up.

However it requires Python 2.

查看更多
Animai°情兽
5楼-- · 2019-03-08 07:30

Patching SMTPLib for testing purposes can help test sending mails without sending them.

查看更多
成全新的幸福
6楼-- · 2019-03-08 07:31

If you have a TomCat server available, or other servlet engine, then a nice approach is "Post Hoc" which is a small server that looks to the application exactly like a SMTP server, but it includes a user interface that allows you to view and inspect the email messages that were sent. It is open source and freely available.

Find it at: Post Hoc GitHub Site

See the blog post: PostHoc: Testing Apps that Send Email

查看更多
祖国的老花朵
7楼-- · 2019-03-08 07:35

If you are into unit-testing the best solution is to use the In-memory backend provided by django.

EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

Take the case of use it as a py.test fixture

@pytest.fixture(autouse=True)
def email_backend_setup(self, settings):
    settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'  

In each test, the mail.outbox is reset with the server, so there are no side effects between tests.

from django.core import mail

def test_send(self):
    mail.send_mail('subject', 'body.', 'from@example.com', ['to@example.com'])
    assert len(mail.outbox) == 1

def test_send_again(self):
    mail.send_mail('subject', 'body.', 'from@example.com', ['to@example.com'])
    assert len(mail.outbox) == 1
查看更多
登录 后发表回答