Programing

이메일 전송 테스트

lottogame 2020. 10. 18. 08:18
반응형

이메일 전송 테스트


이메일 전송 테스트에 대한 팁이 있습니까? Gmail 계정을 만드는 것 외에 특히 이메일을 수신하기 위해?

이메일을 보낼 때 폴더 안에 로컬로 저장하고 싶습니다.


개발 및 테스트를위한 매우 편리한 솔루션 인 이메일 전송을 위해 파일 백엔드를 사용할 수 있습니다 . 이메일은 전송되지 않지만 지정한 폴더에 저장됩니다!


Django 테스트 프레임 워크에는 이메일 서비스 테스트에 도움이되는 몇 가지 내장 도우미가 있습니다 .

문서의 예 (짧은 버전) :

from django.core import mail
from django.test import TestCase

class EmailTest(TestCase):
    def test_send_email(self):
        mail.send_mail('Subject here', 'Here is the message.',
            'from@example.com', ['to@example.com'],
            fail_silently=False)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Subject here')

단위 테스트에 참여하는 경우 가장 좋은 해결책은 django에서 제공하는 인 메모리 백엔드 를 사용하는 것 입니다.

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

py.test 고정 장치 로 사용하는 경우

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

각 테스트에서는 mail.outbox서버와 함께 재설정되므로 테스트간에 부작용이 없습니다.

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

MailHog 사용

MailCatcher에서 영감을 받아 설치가 더 쉽습니다.

Go로 구축-MailHog는 여러 플랫폼에 설치하지 않고 실행됩니다.


또한 Jim , MailHog Chaos Monkey 라는 구성 요소가있어 다양한 문제가 발생하는 이메일 전송을 테스트 할 수 있습니다.

Jim은 무엇을 할 수 있습니까?

  • 연결 거부
  • 속도 제한 연결
  • 인증 거부
  • 발신자 거부
  • 수신자 거부

여기에서 자세한 내용을 읽어보십시오 .


(이모 지로 이메일을 보낼 때 실패한 원래 mailcatcher와 달리 UTF-8로 인코딩되고 현재 릴리스에서 실제로 수정되지 않았으며 MailHog는 작동합니다.)


첨부 파일을 보낼 필요가없는 모든 프로젝트의 경우 django-mailer를 사용 합니다. 모든 아웃 바운드 이메일이 전송을 트리거 할 때까지 대기열에 포함되고 전송 된 후에도 기록됩니다. 이 모든 것이 관리자에 표시되므로 이메일로 보내는 코드가 인터 튜브로 발사하려는 내용을 빠르게 확인할 수 있습니다.


Django에는 또한 인 메모리 이메일 백엔드가 있습니다. In-memory backend 아래의 문서에 자세한 내용이 있습니다. 이것은 Django 1.6에 존재하며 이전 버전에 있는지 확실하지 않습니다.


테스트 목적으로 SMTPLib를 패치하면 메일을 보내지 않고 테스트하는 데 도움이 될 수 있습니다.


파일 백엔드를 사용하는 것은 잘 작동하지만 이메일을보기 위해 파일 시스템을 살펴 보는 것이 약간 번거 롭다는 것을 알았습니다. mailcatcher, https://github.com/sj26/mailcatcher 를 사용하여 이메일을 캡처하고 웹 UI에 표시 할 수 있습니다.

Django에서 mailcatcher를 사용하려면 settings.py에 다음과 같은 내용을 추가해야합니다.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = '127.0.0.1'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 1025
EMAIL_USE_TLS = False

smtpd.SMTPServerand 에서 상속하여 정말 간단한 SMTP 서버를 시작하는 것은 어떨까요 threading.Thread?

class TestingSMTPServer(smtpd.SMTPServer, threading.Thread):
    def __init__(self, port=25):
        smtpd.SMTPServer.__init__(
            self,
            ('localhost', port),
            ('localhost', port),
            decode_data=False
        )
        threading.Thread.__init__(self)

    def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
        self.received_peer = peer
        self.received_mailfrom = mailfrom
        self.received_rcpttos = rcpttos
        self.received_data = data

    def run(self):
        asyncore.loop()

process_message는 SMTP 서버가 메일 요청을받을 때마다 호출되며 원하는대로 할 수 있습니다.

테스트 코드에서 다음과 같이하십시오.

smtp_server = TestingSMTPServer()
smtp_server.start()
do_thing_that_would_send_a_mail()
smtp_server.close()
self.assertIn(b'hello', smtp_server.received_data)

그냥 기억 호출하여 asyncore 루프 (듣기에서 서버를 중지) 종료.close()asyncore.dispatchersmtp_server.close()


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


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


Tying a few of the pieces here together, here's a straightforward setup based on filebased.EmailBackend. This renders a list view linking to the individual log files, which have conveniently timestamped filenames. Clicking a link in the list displays that message in the browser (raw):

Settings

EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"
EMAIL_FILE_PATH = f"{MEDIA_ROOT}/email_out"

View

import os

from django.conf import settings
from django.shortcuts import render

def mailcheck(request):

    path = f"{settings.MEDIA_ROOT}/email_out"
    mail_list = os.listdir(path)

    return render(request, "mailcheck.html", context={"mail_list": mail_list})

Template

{% if mail_list %}
  <ul>
  {% for msg in mail_list %}
    <li>
      <a href="{{ MEDIA_URL }}email_out/{{msg}}">{{ msg }}</a>
    </li>
  {% endfor %}
  </ul>
{% else %}
  No messages found.
{% endif %}

urls

path("mailcheck/", view=mailcheck, name="mailcheck"),

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.


My solution is write content to a html file. This way can help you see how email look like. I leave it here htmlfilebased.EmailBackend.

Other tip: You can use django email template editor which can help you edit your email template with zero inline css.

참고URL : https://stackoverflow.com/questions/3728528/testing-email-sending

반응형