Aplus Framework Docs

Email

Aplus Framework Email Library

Aplus Framework Email Library.

Installation

The installation of this library can be done with Composer:

composer require aplus/email

Sending Emails

The process of sending messages by email follows the example code below.

use Framework\Email\Mailer;

// Set the mailer that will send the messages
$mailer = new Mailer('johndoe', 'p$$word');

// The message is created
$message = $mailer->createMessage();
$message->setFrom('[email protected]')
        ->addTo('[email protected]')
        ->setPlainMessage('Hello, Mary! How are you?');

// Try to send the message
$sent = $mailer->send($message); // false or true

if ($sent) {
    echo 'Message sent.';
} else {
    echo 'The message was not sent: ';
    echo $mailer->getLastResponse();
}

Plain Message

It is possible to set the plain text version of the message:

$message->setPlainMessage('Hello, John Doe!');

HTML Message

It is also possible to set the message as HTML:

$message->setHtmlMessage('Hello, <b>John Doe</b>!');

Or both versions:

$message->setPlainMessage('Hello, John Doe!')
        ->setHtmlMessage('Hello, <b>John Doe</b>!');

Embed Images

When sending HTML messages it may be necessary to place images in the body of the message.

This is done through an inline attachment with the cid in the src attribute of the image:

$message->setHtmlMessage('Hello, <b>John Doe</b>!<br>
See how beautiful the sky was today:
<img src="cid:sky">');
$message->setInlineAttachment(__DIR__ . '/blue-sky.png', 'sky')

Attachments

The other attachments can be added with the addAttachment method:

$message->addAttachment(__DIR__ . '/storage/invoice-1001.pdf');

Headers

Message header fields can be set directly using the setHeader method:

$message->setHeader('Subject', 'How are you?')
        ->setHeader('From', '[email protected]')
        ->setHeader('To', '[email protected]');
use Framework\Email\Header;

$message->setHeader(Header::SUBJECT, 'How are you?')
        ->setHeader(Header::FROM, '[email protected]')
        ->setHeader(Header::TO, '[email protected]');

Or through setters of the most used headers:

$message->setSubject('How are you?')
        ->setFrom('[email protected]')
        ->addTo('[email protected]');

X-Priority

The X-Priority can be set as below:

use Framework\Email\XPriority

$message->setXPriority(XPriority::HIGH);

Mailer Connection

The default configs for connecting to the mail server are as follows:

use Framework\Email\Mailer;

$config = [
    'host' => 'localhost',
    'port' => 587,
    'tls' => true,
    'options' => [
        'ssl' => [
            'allow_self_signed' => false,
            'verify_peer' => true,
            'verify_peer_name' => true,
        ],
    ],
    'username' => null,
    'password' => null,
    'charset' => 'utf-8',
    'crlf' => "\r\n",
    'connection_timeout' => 10,
    'response_timeout' => 5,
    'hostname' => gethostname(),
    'keep_alive' => false,
    'save_logs' => false,
];

$mailer = new Mailer($config);

The username and password must be set.

The port is normally 25, 465 or 587. Check with your postmaster.

Keep Alive

If you are going to send more than one message on the same connection, set keepalive to true.
This will use the same connection for all submissions.

Logs

If you need to debug communication with the SMTP server, enable the option to save logs in the configuration by setting save_logs to true. Then it will be possible to obtain the logs with the Mailer::getLogs method.

It is possible to clear the logs after each submission using the Mailer::resetLogs method.

Conclusion

Aplus Email Library is an easy-to-use tool for, beginners and experienced, PHP developers.
It is perfect for sending emails via SMTP in a very practical way.
The more you use it, the more you will learn.

Did you find something wrong?
Be sure to let us know about it with an issue.
Thank you!

Search results