Skip to content
/ mail Public

Sockets-based library for sending HTML email messages.

License

Notifications You must be signed in to change notification settings

WebFiori/mail

Repository files navigation

WebFiori Mailer

A basic library for sending HTML based emails using PHP.

Supported PHP Versions

Build Status

In This Page:

Usage

Basic Usage

This section describes most basic use case of the library. It shows how to connect to SMTP server, writing a message and sending it to specific address.

Connecting to SMTP Server

Connection information are represented using an instance of the class webfiori\email\SMTPAccount.

<?php
require '../vendor/autoload.php';

use webfiori\email\SMTPAccount;
use webfiori\email\AccountOption;

//First, create new SMTP account that holds SMTP connection information.
$smtp = new SMTPAccount([
    AccountOption::PORT => 465,

    //Replace server address with your mail server address
    AccountOption::SERVER_ADDRESS => 'mail.example.com',

    //Replace server username with your mail server username
    AccountOption::USERNAME => '[email protected]',

    AccountOption::PASSWORD => 'KnvcbxFYCz77',

    AccountOption::SENDER_NAME => 'Ibrahim',

    //Replace sender address with your mail server sender address
    AccountOption::SENDER_ADDRESS => '[email protected]',

    AccountOption::NAME => 'no-reply'
]);

Creating Email Message

After having SMTP connection information, an instance of the class webfiori\email\Email can be created. The consructor of the class will accept one parameter which is the connection that will be used to connect to SMTP server.

//Second, create your actual email. using the account that was just created to
//send messages.
$email = new Email($smtp);

Setting Email Subject

To set the subject of the message, the method Email::setSubject() can be used as follows:

//Set subject
$email->setSubject('Hello World From PHP πŸ˜€');

Adding a Recipient

//Specify who will receive the message
$email->addTo('[email protected]');

Writing Some Text

The email messages which are created using the library are HTML based. They utilize the library webfiori\ui to build the virtual DOM.

An HTML elemtnt can be inserted to the body of the message by using the method Email::insert().

//Build your HTML Message
$div = $email->insert('div');
$div->addChild('p')->text('Hello World Message');
$div->addChild('p', [
    'style' => [
        'font-weight' => 'bold',
        'color' => 'red'
    ]
])->text('This is just a test message.');

Sending The Message

The final step is to send the message. This can be performed using the method Email::send().

//Finally, send.
$email->send();

All Togather

When we put all the steps as one, we would have the following:

require '../vendor/autoload.php';

use webfiori\email\AccountOption;
use webfiori\email\SMTPAccount;
use webfiori\email\Email;

$smtp = new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]);

$email = new Email($smtp);

$email->setSubject('Hello World From PHP πŸ˜€');

$email->addTo('[email protected]');

$div = $email->insert('div');
$div->addChild('p')->text('Hello World Message');
$div->addChild('p', [
    'style' => [
        'font-weight' => 'bold',
        'color' => 'red'
    ]
])->text('This is just a test message.');

$email->send();

Attachments

Attachements can be added to any email using the method Email::addAttachment(). The method accepts a single parameter. The parameter can be a string which represents the absolute path of the file to be attached or an object of type webfiori\file\File.

use webfiori\email\AccountOption;
use webfiori\email\SMTPAccount;
use webfiori\email\Email;
use webfiori\file\File;

$smtp = new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]);

$email = new EmailMessage($smtp);
 
$email->addAttachment('Attach00.txt');
$email->addAttachment(new File('another.txt'));

Before Send Callback

Suppose that a developer would like to perform a task everytime the method Email::send() is called, and that event must be called before connecting to SMTP server. In such case, the developer can use the method Email::addBeforeSend(). The method accepts two parameters, first one is a function callback and second one is an optional array of parameters to be passed to the callback.

$smtp = new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]);

$email = new Email($smtp);
$email->setSubject('Hello World From PHP πŸ˜€');
$email->addTo('[email protected]');

$email->addBeforeSend(function (Email $e) {
  $e->insert('p')->text('This text is added before sending');
});

$div = $email->insert('div');
$div->addChild('p')->text('Hello World Message');

$email->send();

After Send Callback

Suppose that a developer would like to perform a task everytime the method Email::send() is called, and that event must be called after sending the email. In such case, the developer can use the method Email::addAfterSend(). The method accepts two parameters, first one is a function callback and second one is an optional array of parameters to be passed to the callback.

$smtp = new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]);

$email = new Email($smtp);
$email->setSubject('Hello World From PHP πŸ˜€');
$email->addTo('[email protected]');

$email->addAfterSend(function (Email $e) {
 // Do any action like storing the log.
});

$div = $email->insert('div');
$div->addChild('p')->text('Hello World Message');

$email->send();

Accessing SMTP Log

One of the features of the library is the logging of SMTP commands that was sent to server. This is useful in case the developer would like to trace the cause of send failure. To access the log events, the method Email::getLog() can be used. The method will return an array that holds sub-assiciative arrays. each associative array will have 3 indices, command, response-code and response-message.

foreach ($email->getLog() as $logEvent) {
  echo ' Command: '.$logEvent['command'];
  echo ' Code: '.$logEvent['response-code'];
  echo ' Message: '.$logEvent['response-message'];
}

Storing Email

Since the emails which are constructed using the library are HTML based, they can be stored as HTML web pages. This feature is useful in case the developer would like to test a preview of final constructed email.

To store an email as HTML web page, the method Email::storeEmail() can be used as follows:

$m = new Email(new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]));
$m->setSubject('Test Ability to Store Email');
$m->addTo('[email protected]');
$m->insert('p')->text('Dear,')->setStyle([
    'font-weight' => 'bold',
    'font-size' => '15pt'
]);
$m->insert('p')->text('This email is just to inform you that you can store emails as web pages.');
$m->insert('p')->text('Regards,')->setStyle([
    'color' => 'green',
    'font-weight' => 'bold'
]);
$m->storeEmail('/path/to/email/file');

The call to the method Email::storeEmail() will do the following:

  • Render the final email.
  • Create a folder which has same subject as the email inside provided folder.
  • Create HTML file which has the date and time as its name inside the folder.

The final output of the given code will be HTML web page that is similar to following image.

image

Setup Testing

When testing the email, we usually intersted on seeing the final look of the email in addition to knowing who are the recepints of the email. The library provides the developer with two options for testing email messages:

  • Storing them as HTML web pages
  • Sending them to specific addresses.

The two testing modes are controlled by the method Email::setMode(). The method is used to set the mode at which the email will use when the method Email::send is called.

Storing as Web Pages

In this case, the mode of sending the message should be set to SendMode::TEST_STORE. Additionally, the location at which the message will be stored at must be provided.

$m = new Email(new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]));

//Here, set the mode to testing and storing.
$m->setMode(SendMode::TEST_STORE, [
    'store-path' => '/path/to/store/message'
]);


$m->setSubject('Test Ability to Store Email');
$m->addTo('[email protected]');
$m->insert('p')->text('Dear,')->setStyle([
    'font-weight' => 'bold',
    'font-size' => '15pt'
]);
$m->insert('p')->text('This email is just to inform you that you can store emails as web pages.');
$m->insert('p')->text('Regards,')->setStyle([
    'color' => 'green',
    'font-weight' => 'bold'
]);
$m->send();

Storing as Web Pages

In this case, the mode of sending the message should be set to SendMode::TEST_SEND. Additionally, the addresses of the users who will receive the email must be provided.

$m = new Email(new SMTPAccount([
    AccountOption::PORT => 465,
    AccountOption::SERVER_ADDRESS => 'mail.example.com',
    AccountOption::USERNAME => '[email protected]',
    AccountOption::PASSWORD => 'KnvcbxFYCz77',
    AccountOption::SENDER_NAME => 'Ibrahim',
    AccountOption::SENDER_ADDRESS => '[email protected]',
    AccountOption::NAME => 'no-reply'
]));

//Here, set the mode to testing and storing.
$m->setMode(SendMode::TEST_SEND, [
    'send-addresses' => [
        '[email protected]',
        '[email protected]',
    ]
]);


$m->setSubject('Test Ability to Store Email');
$m->addTo('[email protected]');
$m->insert('p')->text('Dear,')->setStyle([
    'font-weight' => 'bold',
    'font-size' => '15pt'
]);
$m->insert('p')->text('This email is just to inform you that you can store emails as web pages.');
$m->insert('p')->text('Regards,')->setStyle([
    'color' => 'green',
    'font-weight' => 'bold'
]);
$m->send();