PHP Mail
Most Web-Server offer an email function. But offen the desired sender Email is not configured on the Web Server. K8 Web Kit supports the following 2 email systems:
- email by the Web server
- email by PHPMailer and an external email server
Use cases
- gbSendContact()
- gbSystemEmail() with the action activate, activateReminder, resetPwd or inviteUser (gbsendActivation() is replaced by it)
- k8MfaStart(): the login code (action mfaCode)
- gbSendPwd()
Basic Configuration
config/_init.php
The default domain_emailmode is set to "1: email by PHPmailer". Please fill out your email configuration in the _init.php.
If the domain_emailmode is set to "0: email by server": please look to your Server Email Configuration and check the configuration.
The default domain_testemail can be used for several user as test. Please regard that "Send Password" is not working well in this case.
domain_email allows to configure:
- activate, Activation email
- sendPwd, Send password email
For both:
- subject
- content
can be declared. The content supports HTML-emails.
The template can be used for further necessary emails.
PHPMailer
Configuration
config/_init.php
The default domain_emailmode is set to "1: PHPMailer". Please continue with the configuration for your email server. For example for a google email server it is important to configure an App-Password for third party access (description of App Password configuration).
More information to:
<projectpath>/_init.json
This is the same definitiion written in JSON format.
Send email
JavaScript
domain_emailrights
- 0: no emails
- 1: to logged in user only (default)
- 2: logged in to every one
- 3: role: 32
- 4: own email configuration (future)
Parameters:
- to
- cc
- bcc
- subject
- message: this is the whole email body
- content: uses the template
PHP
If you want to use PHPMailer for own emails even by your customer, you can declare PHPMailer like this and transfer the customer mail server access data.
Email with attachment
JavaScript
Additional parameters of sendEmail:
- template: the action of an email template in k8emailtemplates. It delivers the subject and the message, if you don't send them.
- placeholder: replaces {{subject}}, {{activitydate}} ... in the subject and in the message.
- attachments: a list of name and content.
The browser may only send calendar files:
- the name ends with .ics
- the content starts with BEGIN:VCALENDAR
- max. 3 files, each max. 100 KB
Otherwise the email is not sent: "attachment not allowed (only .ics)". An unknown template returns "email template not found". Who may send emails is defined by domain_emailrights.
Example: the menu "Send ics" of the datadefinition addressactivities, the template is activity_ics.
PHP
Both email classes, eMailbyPHPMailer and eMailbyServer, can attach files.
- bprepareAttachment($file,$temporary): attaches a file of the server. With $temporary=1 it is deleted after sending.
- bprepareStringAttachment($name,$content,$type): attaches a content without a file, e.g. a calendar file you have just created.
Call the methods before bsend(). If something fails, getError() returns the reason.
Create an ICS file
Structure
An ICS file is a text file with the lines KEY:value.
- BEGIN:VCALENDAR ... END:VCALENDAR: the whole calendar file
- VERSION, PRODID: version of the format and the program, which created the file
- METHOD:PUBLISH: the recipient can add the appointment to the calendar. To let the recipient accept or decline, use METHOD:REQUEST and an ORGANIZER.
- BEGIN:VEVENT ... END:VEVENT: one appointment
- UID: unique ID of the appointment (record ID and host)
- ATTENDEE: the participant, with mailto:
- LOCATION, SUMMARY (title), DESCRIPTION (text), CLASS
- DTSTART, DTEND: begin and end as YYYYMMDDTHHMMSS (local time)
- DTSTAMP: time of creation
Rules:
- the lines are separated by CRLF
- a long line is folded: the next line begins with a blank
- in texts the characters , ; \ and a line break are written as \, \; \\ and \n
JavaScript
The function builds the text of the ICS file from a record. It is used by "Send ics" in addressactivities_head_end.js.
- dat: the record (activityID, subject, description, email, activitydate)
- minutes: duration of the appointment. The end is the begin plus the minutes.
- fmt: formats a date as YYYYMMDDTHHMMSS
- esc: masks the special characters of a text
- fold: folds lines longer than 60 characters
- The HTML of the description is converted to text, a paragraph and a <br> become a line break.
- The lines are joined with CRLF, the last line ends with CRLF too.
The result is attached to the email as termin.ics, see "Email with attachment".
Save or invite
Currently "Send ics" sends a publication (METHOD:PUBLISH). An invitation (METHOD:REQUEST) needs only a few more lines, but the recipient gets Accept / Tentative / Decline buttons directly in the email.
- METHOD:REQUEST instead of PUBLISH
- ORGANIZER (required): the sender with a valid email address. The answers of the recipients go to this address.
- SEQUENCE: counts the changes. Send the same UID with a higher SEQUENCE to update the appointment.
- STATUS:CONFIRMED: the appointment is fixed. To cancel it send the same UID with METHOD:CANCEL.
The content type of the attachment must contain the method, otherwise Outlook and Gmail do not show the buttons:
text/calendar; method=REQUEST; charset=utf-8
It is the third parameter of bprepareStringAttachment(), see "Email with attachment".
| Save (PUBLISH) | Invite (REQUEST) | |
|---|---|---|
| The recipient sees | the attachment termin.ics, a double click adds it to the calendar | the appointment with the buttons Accept / Tentative / Decline in the email |
| Answer to you | none | the recipient sends the answer to the ORGANIZER, so you need an address, which receives answers |
| Entry in the calendar | only if the recipient opens the file | automatic when the recipient accepts |
| Change or cancel | not possible, the recipient keeps a dead copy | same UID with higher SEQUENCE updates, METHOD:CANCEL cancels |
| Required | nothing | ORGANIZER with a valid email address |
| Typical use | newsletter, event notes, deadlines | meetings with accept and decline |
Email templates
Email templates allow you to conveniently create and format templates.
Action
The name for the email, should be the action, which it's used for. The system emails use the actions (see System emails):
- activate
- resetPwd
- inviteUser
- activateReminder
- mfaCode
Language
You can define different languages for one action.
Bcc
if necessary send the email to blind copy.
Content
The Content can be formated with this editor (tinymce). Placeholders can be used like this {{placeholder}}. By sending the email, they are exchanged for the value of the data record. The content is inserted in the global email template.
Message
The message is equivalent to the content. This message is send as email body. It should have also a signature.
Images
Images can be uploaded by the image icon, "Upload". The image can be resized by the image handler. It's saved in the upload folder and the complete url is linked in the image tag.
The ediors above use absolut path with (datadefinition.masterdata.seofriendly: true).
System emails: activate, activateReminder, resetPwd, inviteUser, mfaCode
The function gbSystemEmail() (masterdata/BasicFunctions.php) sends the emails of the user management. Each email contains a link with a one-time token (table k8logintoken). The token can be used once and expires.
| Action | Sent when | Link | Valid (config/_init.php) | Placeholders |
|---|---|---|---|---|
| activate | after the registration (process_action sendActivation), the user must activate the account | {{activation_link}}, page activate | expiration_activation, hours (default 144) | {{activation_link}}, {{expires}}, {{domain_name}} and the fields of the user |
| resetPwd | password forgotten (process_action passwordreset_email), the user sets a new password | {{reset_link}}, page passwordreset | expiration_pwdreset, minutes (default 1440) | {{reset_link}}, {{expires}}, {{domain_name}} and the fields of the user |
| inviteUser | administrator or superuser invites a user (menu "Invite user" in the user list, process_action userinvite_email), the user sets the password | {{reset_link}}, page passwordreset | expiration_pwdinvite, minutes (default 10080 = 7 days) | {{username}}, {{reset_link}}, {{expires}}, {{domain_name}} and the fields of the user |
| activateReminder | reminder for a not activated user, sent by the cron job masterdata/k8activationreminder.php after activation_reminder_hours (see User management) | {{activation_link}}, page activate | activation_delete_hours, hours (default 72), then the user is deleted | {{activation_link}}, {{expires}}, {{domain_name}} and the fields of the user |
| mfaCode | login with e-mail code (domain_mfa), after the correct password; sent by k8MfaStart(), not by gbSystemEmail() |
no link, 6-digit code | domain_mfa['minutes'] (default 10) | {{code}}, {{minutes}}, {{username}}, {{domain_name}} |
Placeholders
- {{expires}}: date and time until the link is valid, format dd.mm.yyyy hh:mm (time of the database server).
- {{domain_name}}: the name of the website (domain_name in config/_init.php).
- The fields of the user are the columns of the table k8login, e.g. {{username}}, {{email}}, {{firstname}}, {{lastname}}.
Which text is used
- The template with this action in the table k8emailtemplates. The language is searched in this order: the language of the user, the system language (domain_langnative), any language. The language is the value (en, de, ...) of the group chosen in languageID. Subject and message come from the template.
- No template: the default text in
$GLOBALS['domain_email'][action](config/_init.php, keys subject and content). For the activation the texts of the registration form (email_subject, email_content) are used first.
The subject is converted to plain text before sending (tags removed, entities like resolved).
sendPwd (new password in the email) is switched off, because it stored the password as plain text. The action is still in $GLOBALS['domain_email'], use resetPwd instead.
Load all templates
Your datadefintion :
The file: {{datadefID}}_head_include.php is included. This includes the email templates into your web site.
the include for php:
The variable data_emailtemplates is declared in JavaScript. It's an array with all defined templates.
Use the template
JavaScript excerp of callback cbmenuleft of reclamationapprovals:
Process
- statustext: prepare the status text
- dat_emailtemplates: filter the template reclamation_approval from array
- prepare the data for the email, dat_email:
- to
- bcc
- subject
- message
- command PHP to send the email
- set emailsend in the recclamationapprovals
- set recclamationapprovals
Email user configuration
The datadefinition k8emailconfig allows you to define additional email connectors.
Some email provider use a Two-factor authentication (2FA). The version of PHP Mailer used by K8 Web Kit don't support 2FA. Other programs don't do that either. For this reason many email provider, like google, microsoft or yahoo allow to configure an app password. So you simply enter the app password here.
This email configuration is joined to the company object. It can be joined to your objects. It can be called by:
- ProcessMethod.php
- process_action=sendUserEmail
- configID=<your ID>
- ... like above
- process_action=sendUserEmail