Skip to content

MMailer

Overview

MMailer is a PHP class that allows sending emails in HTML format with the ability to add attachments.

The class first tries to send the email using PHP's mail() function, and in case of failure, it automatically uses SMTP protocol to use a sending server that must have been specified beforehand.

If mail sending has already been configured on the server and PHP's mail() function is available, nothing else needs to be done except calling the MMailer::send() method. Otherwise, you'll need to configure an SMTP server beforehand.
Conversely, even if an SMTP server is configured, it will be ignored if PHP's mail() function works, unless you have called the MMailer::setForceSmtpMode(true) function.

Basic Configuration

Usage Example

use mlib\net\mail\MMailer;
use mlib\net\mail\MMailerException;

try {
    // Simple email sending
    MMailer::send(
        'sender@example.com',
        'recipient@example.com',
        'Email Subject',
        '<h1>HTML Content</h1><p>This is a test email.</p>'
);
} catch (MMailerException $e) {
    echo "Email sending error: " . $e->getMessage();
}

The example above can only work if mail sending is configured on the server.

Configuration for SMTP usage

use mlib\net\mail\MMailer;

// Configure SMTP server
MMailer::setSmtp(
    'smtp.example.com', // SMTP server address
    587,                // Port (default: 25)
    'tls',              // Secure mode (null, 'tls' or 'ssl')
    'your@email.com',   // SMTP username (optional)
    'your_password',    // SMTP password (optional)
    'LOGIN'             // Authentication type (optional: 'PLAIN', 'LOGIN', 'CRAM-MD5', 'XOAUTH')
);

This call must be made before calling MMailer::send(), but it can be done only once for the entire script, unless you want to use a different SMTP server for a specific send.
Note that this is only possible if mail sending is not configured globally on the server.

Advanced Features

Adding additional parameters

use mlib\net\mail\MMailer;

$params = [
    'Cc' => 'copy@example.com',
    'Bcc' => 'hidden@example.com',
    'Reply-To' => 'reply@example.com',
    'Return-Path' => 'return@example.com'
];

MMailer::send(
    'sender@example.com',
    'recipient@example.com',
    'Subject with copy',
    '<p>This message has copy recipients.</p>',
    $params
);

Adding attachments

use mlib\net\mail\MMailer;

// Single attachment
MMailer::send(
    'sender@example.com',
    'recipient@example.com',
    'Email with attachment',
    '<p>Please find the attached document.</p>',
    null,
    '/path/to/document.pdf'
);

// Multiple attachments
MMailer::send(
    'sender@example.com',
    'recipient@example.com',
    'Email with multiple attachments',
    '<p>Please find the attached documents.</p>',
    null,
    [
        '/path/to/document1.pdf',
        '/path/to/image.jpg'
    ]
);

// Attachments with custom names
MMailer::send(
    'sender@example.com',
    'recipient@example.com',
    'Email with custom attachments',
    '<p>Please find the attached documents.</p>',
    null,
    [
        'doc123.pdf' => '/path/to/document1.pdf',
        'photo.jpg' => '/path/to/image.jpg'
    ]
);

Simulation mode

Simulation mode allows testing an application supposed to send emails without actually sending them. Useful in development for testing without risking sending real emails.

use mlib\net\mail\MMailer;

// Enable simulation mode
MMailer::setSimulationMode(true);

// The following call will always return true without actually sending the email
MMailer::send(
    'sender@example.com',
    'recipient@example.com',
    'Email in simulation mode',
    '<p>This message will not be sent.</p>'
);