Download the PHP package webfiori/mailer without Composer
On this page you can find all versions of the php package webfiori/mailer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mailer
WebFiori Mailer
A basic library for sending HTML based emails using PHP.
Supported PHP Versions
Build Status |
---|
In This Page:
- Usage
- Basic Usage
- Connecting to SMTP Server
- Creating Email Message
- Setting Email Subject
- Adding a Recipient
- Writing Some Text
- Sending The Message
- All Togather
- Attachments
- Before Send Callback
- After Send Callback
- Accessing SMTP Log
- Storing Email
- Setup Testing
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
.
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.
Setting Email Subject
To set the subject of the message, the method Email::setSubject()
can be used as follows:
Adding a Recipient
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()
.
Sending The Message
The final step is to send the message. This can be performed using the method Email::send()
.
All Togather
When we put all the steps as one, we would have the following:
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
.
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.
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.
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
.
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:
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.
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.
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.