Download the PHP package vecode/moritabox-smtp without Composer
On this page you can find all versions of the php package vecode/moritabox-smtp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package moritabox-smtp
moritabox-smtp
Overview
SMTP server built with ReactPHP, supports PLAIN, LOGIN and CRAM-MD5 authentication.
An easy way to run a local SMTP server for testing mail implementations.
Requirements
php
:>=8.1
react/socket
:^1.13
Installation
The easiest way to install it is to use Composer:
Basic usage
Create an SmtpServer
instance by passing an authentication callback; then subscribe to the ready
, mail
and/or error
events:
The ready
event is fired when the server is initialized and listening, the error
event signals ReactPHP's Socket
errors.
Finally, the mail
event is fired when a complete mail message has been received, with a Mail
instance which contains the from address, recipient addresses and all its contents in plain-text form so that you can store it or parse with a MIME message parser (for example by using the mailparse
extension).
Note that you can change the listening port (defaults to 8025
) from the SmtpServer
constructor and also you can specify the fully-qualified domain for the CRAM-MD5
challenge and also set a flag so that the port is bound only locally (that is, allow connections from 127.0.0.1
only).
SSL/TLS is supported only in implicit mode, that is, the connection starts out with a secure handshake. For it to work you will need to enable it and pass the certificate and private key location (if required):
As you can see, the second parameter is an SSL context options listing as defined here.
STARTTLS is not supported due to a ReactPHP limitation.
Authentication
Each of the supported authentication methods require you to provide an authentication callback that will receive the user name and must return the password for that user or false
for non-existing users. As you can see, the returned password must be in plain-text, a limitation on the SMTP protocol, so it is advised to encrypt the passwords if you store them (in a database for example).
If you do not provide an authentication callback, it will be disabled, but it is generally not recommended unless you know what you are doing. For example, relay servers don't require authentication but they must validate the recipient address and reject the message if it is not valid.
Validation
You can also validate the sender/recipient (depending on the server's purpose) by means of the setMailCallback
and setRecipientCallback
methods, both of them take a Closure
as argument that in turn receives an $email
parameter; just return true
to allow the address or false
to deny it.
This example uses a pretty simple approach, but you may use a regular expression or any other advanced mechanism to validate it should you like to.
Using PHPMailer to send mail
The most-common use case is testing email sent from other PHP projects, commonly implemented with PHPMailer or similar libraries.
In the following example you can see a basic setup; the most important bit is the authentication parameters (SMTPAuth
, Username
and Password
, the later two will be checked via the authentication callback described in the previous section):
If you have enabled implicit SSL you must also set the SMTPSecure
option as follows:
License
MIT licensed
Copyright © 2023 Vecode. All rights reserved.