- class MailpitUser(username: str, password: str)¶
Mailpit user for authentication
Helper class to define a user for Mailpit authentication.
This is just a named tuple for username and password.
Example
>>> from testcontainers.mailpit import MailpitUser >>> users = [ ... MailpitUser("jane", "secret"), ... MailpitUser("ron", "pass2"), ... ] >>> for user in users: ... print(user.username, user.password) ... jane secret ron pass2 >>> username, password = users[0] >>> print(username, password) jane secret
- class MailpitContainer(image: str = 'axllent/mailpit', *, smtp_port: int = 1025, ui_port: int = 8025, users: list[MailpitUser] | None = None, require_tls: bool = False, **kwargs: Any)¶
Test container for Mailpit. The example below spins up a Mailpit server
Default configuration supports SMTP with STARTTLS and allows login with any user/password.
Options:
require_tls = True
forces the use of SSLusers = [MailpitUser("jane", "secret"), MailpitUser("ron", "pass2")]
only allows login withjane:secret
orron:pass2
Simple example:
>>> import smtplib >>> from testcontainers.mailpit import MailpitContainer >>> with MailpitContainer() as mailpit_container: ... host_ip = mailpit_container.get_container_host_ip() ... host_port = mailpit_container.get_exposed_smtp_port() ... server = smtplib.SMTP( ... mailpit_container.get_container_host_ip(), ... mailpit_container.get_exposed_smtp_port(), ... ) ... code, _ = server.login("any", "auth") ... assert code == 235 # authentication successful ... # use server.sendmail(...) to send emails
Example with auth and forced TLS:
>>> import smtplib >>> from testcontainers.mailpit import MailpitContainer, MailpitUser >>> users = [MailpitUser("jane", "secret"), MailpitUser("ron", "pass2")] >>> with MailpitContainer(users=users, require_tls=True) as mailpit_container: ... host_ip = mailpit_container.get_container_host_ip() ... host_port = mailpit_container.get_exposed_smtp_port() ... server = smtplib.SMTP_SSL( ... mailpit_container.get_container_host_ip(), ... mailpit_container.get_exposed_smtp_port(), ... ) ... code, _ = server.login("jane", "secret") ... assert code == 235 # authentication successful ... # use server.sendmail(...) to send emails