-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathairmail.php
151 lines (133 loc) · 4.85 KB
/
airmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
require_once 'airmail.civix.php';
use CRM_Airmail_Utils as E;
/**
* Implements hook_civicrm_alterMailParams().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterMailParams
*/
function airmail_civicrm_alterMailParams(&$params, $context) {
$backend = E::getBackend();
if (!$backend || !in_array('CRM_Airmail_Backend', class_implements($backend))) {
return;
}
$backend->alterMailParams($params, $context);
// Create meta data for transactional email
if ($context != 'civimail' && $context != 'flexmailer') {
$mail = new CRM_Mailing_DAO_Mailing();
$mail->name = 'Airmail Transactional Emails';
if ($mail->find(TRUE)) {
if (!empty($params['contact_id'])) {
$contactId = $params['contact_id'];
}
elseif (!empty($params['contactId'])) {
// Contribution/Event confirmation
$contactId = $params['contactId'];
}
else {
// As last option from emall address
$contactId = airmail_targetContactId($params['toEmail']);
}
if (!$contactId) {
// Not particularly useful, but devs can insert a backtrace here if they want to debug the cause.
// Example: for context = singleEmail, we end up here. We should probably fix core.
Civi::log()->warning('ContactId not known to attach header for this transactional email by Airmail extension possible duplicates email hence skipping: ' . CRM_Utils_Array::value('toEmail', $params));
return;
}
if ($contactId) {
$eventQueue = CRM_Mailing_Event_BAO_Queue::create([
'job_id' => CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $mail->id, 'id', 'mailing_id'),
'contact_id' => $contactId,
'email_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Email', $contactId, 'id', 'contact_id'),
'mailing_id' => $mail->id,
]);
// Add m to differentiate from bulk mailing
$emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
$verpSeparator = CRM_Core_Config::singleton()->verpSeparator;
$params['returnPath'] = implode($verpSeparator, ['m', $eventQueue->job_id, $eventQueue->id, $eventQueue->hash]) . "@$emailDomain";
// add custom headers
$params['headers']['X-My-Header'] = "This is the mail system at host " . "@$emailDomain" . "I am sorry to have to inform you that your message could not be delivered to one or more recipients. It is attached below. ";
// add a tracking img if enabled.
if ($mail->open_tracking && !empty($params['html'])) {
$params['html'] .= "\n" . '<img src="' . CRM_Utils_System::externUrl('extern/open', "q=$eventQueue->id")
. '" width="1" height="1" alt="" border="0">';
}
}
}
else {
Civi::log()->debug('Airmail: the mailing for transactional emails was not found. Bounces will not be tracked. Disable/enable the Airmail extension to re-create the mailing.');
}
}
}
/**
* Returns the contact_id for a specific email address.
* Returns NULL if no contact was found, or if more than one
* contact was matched.
*
* @return contact Id | NULL
*/
function airmail_targetContactId($email) {
// @todo Does this exclude deleted contacts?
$result = civicrm_api3('email', 'get', [
'email' => trim($email),
'sequential' => 1,
]);
if ($result['count'] == 1) {
return $result['values'][0]['contact_id'];
}
return NULL;
}
/**
* hook_civicrm_navigationMenu
*
* add "Airmail Configuration" to the Mailings menu
*/
function airmail_civicrm_navigationMenu(&$menu) {
$adder = new CRM_Airmail_NavAdd($menu);
$attributes = array(
'label' => E::ts('Airmail Configuration'),
'name' => 'Airmail Configuration',
'url' => 'civicrm/airmail/settings',
'permission' => 'access CiviMail,administer CiviCRM',
'operator' => 'AND',
'separator' => 1,
'active' => 1,
);
$adder->addItem($attributes, array('Mailings'));
$menu = $adder->getMenu();
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function airmail_civicrm_config(&$config) {
_airmail_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function airmail_civicrm_install() {
_airmail_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function airmail_civicrm_enable() {
_airmail_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
*/
function airmail_civicrm_managed(&$entities) {
CRM_Airmail::createTransactionalMailing();
}