The z4m_emailsending module extends the ZnetDK 4 Mobile starter application by adding the following features:
- Sends automatically an email to :
- New user added to the application to communicate their credentials.
- Existing user who requested a new password:
- To get confirmation of the password request when the user clicked on the Forgot password? link,
- To communicate their new password if it has been modified by the application administrator or if the user has clicked on the Forgot password? link.
- New views added to the application for:
- configuring email sending,
- testing connection to an SMTP server,
- sending a test email.
- displaying the history of sent emails with the ability to filter and purge content by period and sending status.
In addition, the module includes the EmailToSend
PHP class based on the PHPMailer
class and offering the following enhancements:
- Sending emails in PHP using the email sending configuration done from the application view provided by the module,
- Sending an email in PHP from a custom email template,
- Adding automatically sent emails to the history of sent emails.
Note
This module embeds and uses PHPMailer version 6.9.1 to send emails.
This module is published under the version 3 of GPL General Public Licence.
This module contains the z4m_email_sending_settings
and z4m_email_sending_history
views to declare within the menu.php
of the application in order to configure the Email sending and to get the history of the sent emails.
Email sending configuration and sent emails are stored in database within the
zdk_email_sending_server_settings
and zdk_email_sending_history
SQL tables
created automatically on module execution.
- ZnetDK 4 Mobile version 2.9 or higher,
- A MySQL database is configured to store the application data,
- PHP version 7.4 or higher,
- Authentication is enabled
(
CFG_AUTHENT_REQUIRED
isTRUE
in the App'sconfig.php
).
- Add a new subdirectory named
z4m_emailsending
within the./engine/modules/
subdirectory of your ZnetDK 4 Mobile starter App, - Copy module's code in the new
./engine/modules/z4m_emailsending/
subdirectory, or from your IDE, pull the code from this module's GitHub repository, - Edit the App's
menu.php
located in the./applications/default/app/
subfolder and include themenu.inc
script to add menu item definition for thez4m_email_sending_settings
andz4m_email_sending_history
views.
require ZNETDK_MOD_ROOT . '/z4m_emailsending/mod/menu.inc';
- Edit the App's
config.php
located in the./applications/default/app/
subfolder and declare theMOD_Z4M_EMAILSENDING_ENCRYPTION_KEY
PHP constant to set a custom encryption key used to encrypt the SMTP server credentials stored by the module when authentication to SMTP server is required:
define('MOD_Z4M_EMAILSENDING_ENCRYPTION_KEY', 'Enter here you secret key');
- Go to the Email sending config. menu and configure your email sending server.
Once the Email sending menu item is added to the application, you can restrict
its access via a user profile.
For example:
- Create a user profile named
Admin
from the Authorizations | Profiles menu, - Select for this new profile, the Email sending menu item,
- Finally for each allowed user, add them the
Admin
profile from the Authorizations | Users menu.
Once the z4m_emailsending is installed, you have to configure your email
server from the configuration view (see first section below).
For advanced configuration, extra PHP constants have to be added to the config.php
of your application (see second section below).
To send your first email, you have to configure the email server by following the procedure below:
- Open your App and go to configuration view by clicking the Email sending and Email sending config. menu items.
- Fill in the configuration form:
- EMAIL SENDER: enter the name and email address of the sender used by default to send emails.
The email sender must be configured to allow the module to automatically send emails when a new user is created, a user's password is changed by the administrator or when a user clicks the Forgot password? link. - EMAIL SENDING SERVER: indicate whether to send emails using a Local email server or a SMTP server.
If SMTP is choosen, common SMTP Server parameters must be filled in (Host, TCP/IP port, ...). - OPTIONS: check the sending options.
Email sending can be disabled by checking the Email sending enabled option.
To get email sending history, check the History of sent emails option.
To debug SMTP sending, check the SMTP debug enabled level 4 option (debug traces are written in theemails_sent.log
located in the ZnetDK 4 Mobile./engine/log
folder).
- EMAIL SENDER: enter the name and email address of the sender used by default to send emails.
- Click the Save button to store your configuration.
- If you configured a SMTP Server connection, click the Test connection to SMTP server button to check connection to the SMTP server.
- A test email can be sent by clicking the Send a testing email... button and entering a recipient email address.
The following advanced configuration parameters can be set in the
config.php
script of the application.
MOD_Z4M_EMAILSENDING_ENCRYPTION_KEY
: this PHP constant defines the encryption key (for example the string'My custom encryption key'
) required to store in database the SMTP Server credentials.CFG_FORGOT_PASSWORD_ENABLED
: constant set toTRUE
by default by the module.
Change this value toFALSE
if you want to disable the Forgot password? link.MOD_Z4M_EMAILSENDING_NOTIFY_USER_ON_CREATION
: constant set toTRUE
by default.
Change this value toFALSE
to avoid sending an email each time a user is created.MOD_Z4M_EMAILSENDING_NOTIFY_USER_ON_PASSWORD_CHANGE
: constant set toTRUE
by default.
Change this value toFALSE
to avoid sending an email each time a user's password is changed.MOD_Z4M_EMAILSENDING_SMTP_DEBUG_LEVEL
: constant set to4
(LOWLEVEL) by default.
Other accepted values are1
(CLIENT),2
(SERVER) and3
(CONNECTION).
To test emails locally and thus avoid sending test emails to a real mailbox, you can configure a local email server like Smtp4Dev.
The z4m_emailsending module includes several standard email templates in the
mod/email/
folder.
new-password_[LANG].php
: template used to send a temporary password when a user clicked the forgot password? link from the login page.new-user_[LANG].php
: template used to send new user's credentials.password-change_[LANG].php
: template used to send the new user's password when it has been changed from the User view.password-request_[LANG].php
: template used to get confirmation after a new password request done by clicking the forgot password? link.test-email_[LANG].php
: template used to test email sending when the
The z4m_emailsending module includes the EmailToSend
PHP class to send an email from the PHP code of your application,
for example for an appointment reminder.
The EmailToSend
PHP class extends the PHPMailer
class.
When the EmailToSend
class is instantiated, the following PHPMailer
properties are set by default:
- Exceptions:
PHPMailer
Exceptions are disabled.
Exceptions are thrown by theEmailToSend
object if settings can't be fetched in database and when an error occurred on email sending. CharSet
: set toutf-8
.- Language: set to the language configured for the ZnetDK 4 Mobile Starter App (see
CFG_DEFAULT_LANGUAGE
). - Server settings: set according to the Email sending configuration done via the module's view.
The concerned PHPMailer properties are:From
,FromName
,Mailer
,Host
,Port
,SMTPAuth
,SMTPSecure
,Username
,Password
,SMTPDebug
andDebugoutput
. imageBaseDir
: set to../default/app/public/images/
(public image folder of the application).
Important
To get history of email sent via the EmailToSend
PHP class, be sure to check the History of sent emails option from the Email sending configuration view.
Send a simple email as you would with PHPMailer except you don't need to specify the email sender and the email server connection settings.
Here is a simple example below:
use \z4m_emailsending\mod\EmailToSend;
$email = new EmailToSend();
$email->addAddress('[email protected]', 'John DOE');
$email->Subject = 'My email subject';
$email->msgHTML('<p>Hello,</p><p>This is my first email...</p>');
$email->send();
To send an email from your custom template, first add your template in the
./applications/default/app/email/
folder of your App. See below an example of
custom template named meeting-reminder.php
:
/* meeting-reminder.php: meeting reminder email template */
// Email subject
$this->templateSubject = 'Appointment reminder: [[meeting_datetime]]';
// Email message body
$this->templateBody = <<<'EOT'
<p>Hello [[customer_name]],</p>
<p>This message to remind you of the date and time of your next appointment: [[meeting_datetime]]</p>
<p>Regards,</p>
EOT;
Next, in the PHP code for sending your email, call the EmailToSend::useTemplate()
method
to specify the template name and the values to replace in the email template.
In the example below, the specified template is meeting-reminder
and the values
to replace in the email subject (second argument as indexed array) and in the email body (third argument as indexed array).
use \z4m_emailsending\mod\EmailToSend;
$email = new EmailToSend();
$email->useTemplate('meeting-reminder', [
'meeting_datetime' => '2024-05-30 08:30'
], [
'customer_name' => 'John DOE',
'meeting_datetime' => '2024-05-30 08:30'
]
);
$email->addAddress('[email protected]', 'John DOE');
$email->send();
This module is translated in French, English and Spanish languages.
To translate the view labels and the standard email templates in another language or change the standard
translations, please follow the procedure below.
- Copy in the clipboard the PHP constants declared within the
locale_en.php
script of the module, - Paste them from the clipboard within the
locale.php
script of your application, - Finally, translate each text associated with these PHP constants into your own language.
- Add a folder named
email
in the./applications/default/app/
directory of your application. - Copy PHP scripts whose file name ends with
_en.php
(for examplenew-password_en.php
) and located in the mod/email/ module's folder, to the./applications/default/app/email/
folder added in step 1, - Rename the copied PHP scripts to remove the language indicator suffix
_en
: for example renamenew-password_en.php
tonew-password.php
. - Edit the PHP scripts renamed in step 3 and translate the text to your own language.
- Now, the new email templates located in the
./applications/default/app/email/
folder are automatically used by the module to replace standard email templates.
Tip
The procedure to translate email templates can also be used to customize a standard template available in your native language.
The zdk_email_sending_server_settings
and zdk_email_sending_history
SQL tables
are created automatically by the module when one of the module views is displayed
for the first time.
If the MySQL user declared through the
CFG_SQL_APPL_USR
PHP constant does not have CREATE
privilege, the module can't create the
required SQL tables.
In this case, you can create the module's SQL tables by importing in MySQL or
phpMyAdmin the script z4m_emailsending.sql
provided by the module.
See CHANGELOG.md file.
Your contribution to the ZnetDK 4 Mobile project is welcome. Please refer to the CONTRIBUTING.md file.