Your plugin can add a new way to read the ticket emails, like the API of an email service. The admin selects it as the Type in Settings > Tickets > IMAP settings. RISE creates the tickets from the emails your plugin reads.
Add the type to the list
app_hooks()->add_filter("app_filter_imap_types", function ($types) {
$types["my_plugin_api"] = "My Mail API";
return $types;
});
Show your fields
Print your fields inside a div with data-imap-type. RISE shows the div only when your type is selected.
app_hooks()->add_action("app_hook_imap_settings_extension", function ($options) {
echo '';
echo '';
echo '';
echo '<input type="text" id="my_plugin_api_key" name="my_plugin_api_key" value="" class="form-control">';
echo '';
echo '';
});
- After the admin clicks Save & authorize, RISE opens the data-authorize-url page.
- RISE reads the emails only when the setting imap_authorized is "1". Save it when your authorization is done. RISE sets it to "0" every time the IMAP settings are saved.
- Show "******" for saved secrets, and keep the saved value when this comes back.
Save your fields
app_hooks()->add_action("app_hook_imap_settings_save_data", function ($options) {
//$options["imap_type"] is the selected type
$Settings_model = model("App\Models\Settings_model");
$Settings_model->save_setting("my_plugin_api_key", clean_data((string) service("request")->getPost("my_plugin_api_key")));
});
Read the emails
The cron job calls this action when email piping is on, authorized and your type is selected. Read the unread emails and give each one to RISE.
app_hooks()->add_action("app_hook_run_imap", function ($options) {
if ($options["imap_type"] !== "my_plugin_api") {
return;
}
$Ticket = new \App\Libraries\Ticket();
//for each new email:
$Ticket->create_ticket_from_imap(array(
"email_address" => "customer@example.com",
"email_subject" => "Subject",
"email_sender_name" => "Customer name",
"email_content" => "The html message
",
"email_reply_content" => "The new text of a reply
",
"email_attachments" => $files_data,
"reference_id" => "thread id",
"email_headers" => $raw_headers
));
});
| Key | What it is |
| email_content | The message of a new ticket. |
| email_reply_content | The message without the quoted old text. It's used when the email is a reply of a ticket. |
| email_attachments | Files saved by move_temp_file($file_name, get_setting("timeline_file_path"), "imap_ticket", NULL, "", $file_content). |
| reference_id | The thread id of the email. A reply with the same id is added to the same ticket. A ticket id in the subject (#10) works too. |
| email_headers | Optional. The raw headers of the email. RISE reads the Authentication-Results header for the sender check (DKIM/DMARC). Or pass "email_authentication" as "passed", "failed" or "unknown" if your service gives you the result. RISE also reads Message-ID, In-Reply-To and References: the ticket emails reply in the client's mail thread, and a reply with these ids is added to the same ticket. |
| email_message_id | Optional. The Message-ID of the email, like |
| email_parent_message_ids | Optional. Array of the Message-IDs in In-Reply-To and References, when you don't pass email_headers. |
- $options["time_limit_at"] is the time (unix) when the cron job should stop. Stop there and continue in the next run.
- Mark the emails as read, so they are not added again.
- An email whose Message-ID was already added to a ticket is skipped. So it's safe to try an email again.
When an email fails
One bad email should not stop the others. Use Email_import_tracker to try a failed email again in the next runs. After 3 tries, create a notice ticket and mark the email as read.
$tracker = new \App\Libraries\Email_import_tracker("my_plugin_api");
//for each unread email:
if ($tracker->is_given_up($email_id)) {
$Ticket = new \App\Libraries\Ticket();
$Ticket->create_import_failed_ticket(array(
"email_subject" => $subject,
"email_from" => $from,
"email_to" => $to,
"email_cc" => $cc,
"email_date" => $date
));
$tracker->done($email_id);
//mark the email as read
continue;
}
$tracker->start($email_id);
try {
//download the email and save its files
\App\Libraries\Email_import_tracker::refresh_db_connection();
$Ticket = new \App\Libraries\Ticket();
$Ticket->create_ticket_from_imap($data);
$tracker->done($email_id);
//mark the email as read
} catch (\Throwable $e) {
$tracker->failed($email_id, $e, "My Mail API");
}
- start() saves the try before the import, so it counts even when PHP stops on a memory or time limit.
- refresh_db_connection() opens the database connection again if MySQL closed it while a big email was downloaded.
- The notice ticket has no client and sends nothing to the sender. The team gets the normal new ticket notification.
- Call keep_only($unread_email_ids) at the end to forget emails that someone read in the mailbox.
Find a record by an email address
Email addresses and message ids can look like SQL to get_one_where() (for example update@example.com), so it rejects them. Use get_one_where_bound() for these values. It works like get_one_where(), and the values are escaped by the query builder.
$user = model("App\Models\Users_model")->get_one_where_bound(array("email" => $email, "deleted" => 0));