Skip to content

marko/mail-smtp

SMTP mail driver --- delivers emails over SMTP with TLS, authentication, and MIME encoding. Handles TLS encryption, LOGIN/PLAIN authentication, multipart MIME messages (HTML + text + attachments), and inline images. Configuration comes from config/mail.php under the smtp key.

Implements MailerInterface from marko/mail.

Terminal window
composer require marko/mail-smtp

This automatically installs marko/mail.

Set the mail driver to smtp in your config:

config/mail.php
return [
'driver' => 'smtp',
'from' => [
'address' => $_ENV['MAIL_FROM_ADDRESS'] ?? 'noreply@example.com',
'name' => $_ENV['MAIL_FROM_NAME'] ?? 'My App',
],
'smtp' => [
'host' => $_ENV['MAIL_HOST'] ?? 'localhost',
'port' => (int) ($_ENV['MAIL_PORT'] ?? 587),
'encryption' => $_ENV['MAIL_ENCRYPTION'] ?? 'tls',
'username' => $_ENV['MAIL_USERNAME'] ?? null,
'password' => $_ENV['MAIL_PASSWORD'] ?? null,
'auth_mode' => 'login', // 'login' or 'plain'
'timeout' => 30,
],
];
OptionDefaultDescription
hostlocalhostSMTP server hostname
port587SMTP server port
encryptiontlsEncryption method --- tls, ssl, or null for none
usernamenullSMTP authentication username
passwordnullSMTP authentication password
auth_modeloginAuthentication mode --- login or plain
timeout30Connection timeout in seconds

Bind the mailer interface in your module.php:

module.php
use Marko\Mail\Contracts\MailerInterface;
use Marko\Mail\Smtp\SmtpMailer;
return [
'bindings' => [
MailerInterface::class => SmtpMailer::class,
],
];

Then inject MailerInterface and send emails:

use Marko\Mail\Contracts\MailerInterface;
use Marko\Mail\Message;
class OrderNotifier
{
public function __construct(
private MailerInterface $mailer,
) {}
public function notifyShipment(
string $email,
string $trackingNumber,
): void {
$message = Message::create()
->to($email)
->from('orders@example.com', 'Store')
->subject('Your order has shipped')
->html("<p>Tracking: $trackingNumber</p>");
$this->mailer->send($message);
}
}

Extend SmtpMailer via Preference to customize message building:

use Marko\Core\Attributes\Preference;
use Marko\Mail\Smtp\SmtpMailer;
#[Preference(replaces: SmtpMailer::class)]
class CustomSmtpMailer extends SmtpMailer
{
// Custom SMTP behavior
}

Implements MailerInterface. See marko/mail for the full interface contract.

MethodDescription
send(Message $message): boolBuild and send a Message over SMTP --- handles headers, MIME encoding, and attachments
sendRaw(string $to, string $raw): boolSend a pre-built raw message string to the given recipient

Low-level SMTP protocol transport --- manages the socket connection, TLS negotiation, authentication, and envelope commands.

MethodDescription
connect(string $host, int $port, ?string $encryption = null): voidOpen a socket connection to the SMTP server
ehlo(string $hostname): arraySend the EHLO greeting and return server capabilities
startTls(): voidUpgrade the connection to TLS encryption
authenticate(string $username, string $password, string $mode = 'LOGIN'): voidAuthenticate with the server using LOGIN or PLAIN
mailFrom(string $address): voidSet the envelope sender address
rcptTo(string $address): voidAdd an envelope recipient address
data(string $content): voidSend the message content
quit(): voidClose the SMTP session and disconnect

Reads SMTP settings from the smtp key in config/mail.php via MailConfig.

MethodDescription
host(): stringSMTP server hostname (default: localhost)
port(): intSMTP server port (default: 587)
encryption(): ?stringEncryption method (default: tls)
username(): ?stringAuthentication username
password(): ?stringAuthentication password
timeout(): intConnection timeout in seconds (default: 30)
authMode(): ?stringAuthentication mode (default: login)