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\Mailers\SMTPMailer;
use Framework\Email\Message;

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

// The message is created
$message = new Message();
$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.';
    print_r($mailer->getLogs()); // Show logs for debugging
}

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\SMTPMailer;

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

$mailer = new SMTPMailer($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.

Add Logs

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

Also, you can disable log saving, to save memory, by setting addlogs to false.

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