| 1 |
|
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* Implements the hooks for Mail API |
| 7 |
|
* |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
|
$mail_api_server_info=""; |
| 11 |
|
|
| 12 |
|
|
| 13 |
|
/** |
| 14 |
|
* Invoke Mail API hook in a module based on users mail domain. |
| 15 |
|
*/ |
| 16 |
|
function mail_api_invoke() { |
| 17 |
|
global $_mail_api_module; |
| 18 |
|
|
| 19 |
|
if ($_mail_api_module=="") return FALSE; |
| 20 |
|
|
| 21 |
|
$args = func_get_args(); |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
$mail_api_server_info = mail_api_server_configuration($domain); |
| 25 |
|
$mail_api_server_info['protocol_module']='maildir_api'; |
| 26 |
|
|
| 27 |
|
array_unshift($args, $mail_api_server_info['protocol_module']); |
| 28 |
|
$args[0]=$_mail_api_module; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
if (!function_exists($args[0] .'_'. $args[1])) { |
| 32 |
|
drupal_set_message('Unknown function '. $args[0] .'_'. $args[1] .' was called.', 'error'); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
$rs = call_user_func_array('module_invoke', $args); |
| 36 |
|
|
| 37 |
|
return $rs; |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
/** |
| 42 |
|
* connect to a server of choice |
| 43 |
|
*/ |
| 44 |
|
function mail_api_connect($domain, $username, $password, $folder="") { |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
global $_mail_api_module, $mail_api_server_info; |
| 48 |
|
|
| 49 |
|
if (!$domain) { |
| 50 |
|
if (MAIL_API_DEBUG) drupal_set_message('domain not passed'); |
| 51 |
|
return FALSE; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
$mail_api_server_info = mail_api_server_configuration($domain); |
| 56 |
|
|
| 57 |
|
if (MAIL_API_DEBUG) drupal_set_message("mail_api_connect $username/$password"); |
| 58 |
|
|
| 59 |
|
$_mail_api_module = strtolower(trim($mail_api_server_info->protocol)) .'_api'; |
| 60 |
|
|
| 61 |
|
$rs = module_invoke($_mail_api_module, 'connect', $mail_api_server_info->hostname, $username . $mail_api_server_info->login_suffix, $password, $mail_api_server_info->port, $folder, $mail_api_server_info->options); |
| 62 |
|
|
| 63 |
|
return $rs; |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
returns the name of the module that's used to access mail storage |
| 69 |
|
*/ |
| 70 |
|
function mail_api_module() { |
| 71 |
|
global $_mail_api_module; |
| 72 |
|
|
| 73 |
|
return $_mail_api_module; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
/** |
| 77 |
|
returns status information on a mailbox |
| 78 |
|
option list |
| 79 |
|
* SA_MESSAGES - set status->messages to the number of messages in the mailbox |
| 80 |
|
* SA_RECENT - set status->recent to the number of recent messages in the mailbox |
| 81 |
|
* SA_UNSEEN - set status->unseen to the number of unseen (new) messages in the mailbox |
| 82 |
|
* SA_UIDNEXT - set status->uidnext to the next uid to be used in the mailbox |
| 83 |
|
* SA_UIDVALIDITY - set status->uidvalidity to a constant that changes when uids for the mailbox may no longer be valid |
| 84 |
|
* SA_ALL - set all of the above |
| 85 |
|
*/ |
| 86 |
|
function mail_api_status($mailbox="INBOX", $options=SA_ALL) { |
| 87 |
|
if (MAIL_API_DEBUG) drupal_set_message("mail_api_status $mailbox $options"); |
| 88 |
|
|
| 89 |
|
$rs = mail_api_invoke('status', $mailbox, $options); |
| 90 |
|
|
| 91 |
|
return $rs; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
/** |
| 96 |
|
* Get list of mail servers. |
| 97 |
|
*/ |
| 98 |
|
function mail_api_get_servers() { |
| 99 |
|
$result = db_query("SELECT * FROM {mail_api_servers} WHERE suspended=0"); |
| 100 |
|
|
| 101 |
|
$servers = array(); |
| 102 |
|
|
| 103 |
|
while ($server = db_fetch_array($result)) { |
| 104 |
|
$servers[] = $server; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
return $servers; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
/** |
| 113 |
|
* Get the domain name from the user's email address. |
| 114 |
|
*/ |
| 115 |
|
function mail_api_get_domain() { |
| 116 |
|
global $user; |
| 117 |
|
|
| 118 |
|
if ($_SESSION['mail_api']) { |
| 119 |
|
$domain = end(explode('@', $user->name)); |
| 120 |
|
|
| 121 |
|
return $domain; |
| 122 |
|
} |
| 123 |
|
return FALSE; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
/** |
| 128 |
|
* Implementation of hook_init(). |
| 129 |
|
*/ |
| 130 |
|
function mail_api_init() { |
| 131 |
|
global $user; |
| 132 |
|
/* |
| 133 |
|
if($domain = mail_api_get_domain()) { |
| 134 |
|
// if we find the user's mail server domain name |
| 135 |
|
// get the server's configuration information |
| 136 |
|
$server_info = mail_api_server_configuration($domain); |
| 137 |
|
if ($auth = mail_api_invoke('mail_api_authenticate', $user->name, mail_api_get_password(), $domain, $server_info)) { |
| 138 |
|
// if successfully authenticated |
| 139 |
|
// register a shutdown function to log out of the mail server |
| 140 |
|
// at the end of this page load. |
| 141 |
|
register_shutdown_function('mail_api_invoke', 'mail_api_logout'); |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
*/ |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
/** |
| 150 |
|
* Implementation of hook_user(). |
| 151 |
|
*/ |
| 152 |
|
function mail_api_user($op, &$edit, &$account, $category = NULL) { |
| 153 |
|
switch ($op) { |
| 154 |
|
case 'login': |
| 155 |
|
break; |
| 156 |
|
case 'logout': |
| 157 |
|
// if logging out |
| 158 |
|
// clear all mail server session information |
| 159 |
|
unset($_SESSION['mail_api']); |
| 160 |
|
break; |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
|
| 164 |
|
/** |
| 165 |
|
* Get configured server configuration by domain name from the database |
| 166 |
|
*/ |
| 167 |
|
function mail_api_server_configuration($domain) { |
| 168 |
|
$result = db_fetch_object(db_query("SELECT * FROM {mail_api_servers} WHERE domain = '%s' AND suspended=0", $domain)); |
| 169 |
|
|
| 170 |
|
return $result; |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
/** |
| 176 |
|
takes a domain and returns the module used to connect to this domain |
| 177 |
|
*/ |
| 178 |
|
function mail_api_get_module_by_domain($domain="") { |
| 179 |
|
$connection = db_fetch_object(db_query("SELECT * FROM {mail_api_servers} WHERE domain='%s'", $domain)); |
| 180 |
|
$module = strtolower($connection->protocol); |
| 181 |
|
return $module; |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
/** |
| 187 |
|
* Gets the number of messages in the current mailbox |
| 188 |
|
* |
| 189 |
|
* @param unknown_type $request |
| 190 |
|
* @return unknown |
| 191 |
|
*/ |
| 192 |
|
function mail_api_num_msg() { |
| 193 |
|
$rs = mail_api_invoke('num_msg', $request); |
| 194 |
|
return $rs; |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
|
// FIXME here |
| 199 |
|
|
| 200 |
|
/** |
| 201 |
|
* Returns headers for all messages in a mailbox |
| 202 |
|
* |
| 203 |
|
* @return unknown |
| 204 |
|
*/ |
| 205 |
|
function mail_api_headers() { |
| 206 |
|
$rs = mail_api_invoke('headers', $request); |
| 207 |
|
return $rs; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
/** |
| 211 |
|
* Returns a message header |
| 212 |
|
* if ARRAY is passed, returns an array |
| 213 |
|
* |
| 214 |
|
* @return unknown |
| 215 |
|
*/ |
| 216 |
|
function mail_api_header($msgno) { |
| 217 |
|
|
| 218 |
|
$rs = mail_api_invoke('header', $msgno, FT_UID); |
| 219 |
|
return $rs; |
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
/** |
| 226 |
|
* Read the message body |
| 227 |
|
* FT_UID - The msg_number is a UID |
| 228 |
|
* FT_PEEK - Do not set the \Seen flag if not already set |
| 229 |
|
* FT_INTERNAL - The return string is in internal format, will not canonicalize to CRLF. |
| 230 |
|
* |
| 231 |
|
*/ |
| 232 |
|
function mail_api_body($msg_number, $options=FT_UID, $seen=0) { |
| 233 |
|
$rs = mail_api_invoke('body', $msg_number, $options & $seen); |
| 234 |
|
return $rs; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
|
| 238 |
|
/** |
| 239 |
|
* Read an overview of the information in the headers of the given message |
| 240 |
|
* |
| 241 |
|
* @return unknown |
| 242 |
|
*/ |
| 243 |
|
function mail_api_overview($sequence="", $options="") { |
| 244 |
|
//drupal_set_message('mail_api_overview'); |
| 245 |
|
$rs = mail_api_invoke('overview', $sequence, $options); |
| 246 |
|
return $rs; |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
/** |
| 250 |
|
* Check current mailbox |
| 251 |
|
* |
| 252 |
|
* @return unknown |
| 253 |
|
*/ |
| 254 |
|
function mail_api_check() { |
| 255 |
|
$rs = mail_api_invoke('check'); |
| 256 |
|
return $rs; |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
/** |
| 261 |
|
* Attempt to authenticate the user against the appropriate mail server. |
| 262 |
|
*/ |
| 263 |
|
function mail_api_authenticate($form_values = array('name' => '', 'pass' => '')) { |
| 264 |
|
global $user; |
| 265 |
|
|
| 266 |
|
if (!valid_email_address($form_values['name']) || empty($form_values['pass'])) { |
| 267 |
|
return FALSE; |
| 268 |
|
} |
| 269 |
|
|
| 270 |
|
$domain = end(explode('@', $form_values['name'])); |
| 271 |
|
|
| 272 |
|
$server_info = mail_api_server_configuration($domain); |
| 273 |
|
if (module_invoke($server_info['protocol_module'], 'mail_api_authenticate', $form_values['name'], $form_values['pass'], $domain, $server_info)) { |
| 274 |
|
user_external_login_register($form_values['name'], 'mail_api'); |
| 275 |
|
user_authenticate_finalize($form_values); |
| 276 |
|
$_SESSION['mail_api'] = array( |
| 277 |
|
'protocol_module' => $server_info['protocol_module'], |
| 278 |
|
); |
| 279 |
|
mail_api_set_password($form_values['pass']); |
| 280 |
|
return $user; |
| 281 |
|
} |
| 282 |
|
return FALSE; |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
/** |
| 286 |
|
* Get the user's password from the $_SESSION variable. |
| 287 |
|
*/ |
| 288 |
|
function mail_api_get_password() { |
| 289 |
|
if ($_SESSION['mail_api']['enc'] && module_exists('aes')) { |
| 290 |
|
// if the AES module is installed, and the 'enc' session variable |
| 291 |
|
// is TRUE, use aes_decrypt to decrypt the module. |
| 292 |
|
return aes_decrypt($_SESSION['mail_api']['pass']); |
| 293 |
|
} |
| 294 |
|
else { |
| 295 |
|
// otherwise just return the password stored in the session. |
| 296 |
|
return $_SESSION['mail_api']['pass']; |
| 297 |
|
} |
| 298 |
|
} |
| 299 |
|
|
| 300 |
|
/** |
| 301 |
|
* Set the user's password in the $_SESSION variable. |
| 302 |
|
*/ |
| 303 |
|
function mail_api_set_password($password) { |
| 304 |
|
if (module_exists('aes') && $_SESSION['mail_api']['pass'] = aes_encrypt($password)) { |
| 305 |
|
// if the AES module is installed, and setting the password for the |
| 306 |
|
// session in encrypted form works: |
| 307 |
|
$_SESSION['mail_api']['enc'] = TRUE; |
| 308 |
|
} |
| 309 |
|
else { |
| 310 |
|
// otherwise set the password in the session variable in plain text |
| 311 |
|
$_SESSION['mail_api']['pass'] = $password; |
| 312 |
|
$_SESSION['mail_api']['enc'] = FALSE; |
| 313 |
|
} |
| 314 |
|
} |
| 315 |
|
|
| 316 |
|
/** |
| 317 |
|
* Return array of encryption options for SMTP |
| 318 |
|
*/ |
| 319 |
|
function mail_api_encryption_options() { |
| 320 |
|
return array(t('None'), t('SSL'), t('TLS')); |
| 321 |
|
} |
| 322 |
|
|
| 323 |
|
function mail_api_protocol($action, $name) { |
| 324 |
|
//die($action.' - '.$name); |
| 325 |
|
|
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
/** |
| 329 |
|
* Return list of supported protocols. |
| 330 |
|
*/ |
| 331 |
|
function mail_api_protocols() { |
| 332 |
|
|
| 333 |
|
|
| 334 |
|
// Invoke a hook in all enabled modules that implement it. |
| 335 |
|
$protocols = module_invoke_all('mail_api_protocols'); |
| 336 |
|
|
| 337 |
|
return $protocols; |
| 338 |
|
} |
| 339 |
|
|
| 340 |
|
/** |
| 341 |
|
* Return the quota for the mail account of the user. |
| 342 |
|
*/ |
| 343 |
|
function mail_api_account_quota() { |
| 344 |
|
return mail_api_invoke('mail_api_account_quota'); |
| 345 |
|
} |
| 346 |
|
|
| 347 |
|
|
| 348 |
|
|
| 349 |
|
/** |
| 350 |
|
* Get folders from cache. |
| 351 |
|
* pass * to fetch all mailboxes |
| 352 |
|
* @return: Return an array of folder information. |
| 353 |
|
*/ |
| 354 |
|
function mail_api_mailboxes($pattern="*") { |
| 355 |
|
if (MAIL_API_DEBUG) drupal_set_message("mail_api_mailboxes"); |
| 356 |
|
|
| 357 |
|
$mailboxes = mail_api_invoke('mailboxes', $pattern); |
| 358 |
|
|
| 359 |
|
if (!is_array($mailboxes)) return FALSE; |
| 360 |
|
|
| 361 |
|
foreach ($mailboxes as $id => $object) { |
| 362 |
|
$name = trim($object->name); |
| 363 |
|
|
| 364 |
|
if ($name=='') continue; |
| 365 |
|
|
| 366 |
|
$rs[]=mail_api_format_mailbox($name); |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
return $rs; |
| 370 |
|
} |
| 371 |
|
|
| 372 |
|
/** |
| 373 |
|
* Create new folder in user's mail server account |
| 374 |
|
*/ |
| 375 |
|
function mail_api_create_folder($folder_path) { |
| 376 |
|
// Call hook_mail_api_create_folder() on the appropriate mail server module. |
| 377 |
|
$success = mail_api_invoke('mail_api_create_folder', $folder_path); |
| 378 |
|
|
| 379 |
|
return $success; |
| 380 |
|
} |
| 381 |
|
|
| 382 |
|
/** |
| 383 |
|
* Move/rename folder in user's mail server account |
| 384 |
|
*/ |
| 385 |
|
function mail_api_move_folder($folder_path, $new_folder_path) { |
| 386 |
|
// Call hook_mail_api_move_folder() on the appropriate mail server module. |
| 387 |
|
$success = mail_api_invoke('mail_api_move_folder', $folder_path, $new_folder_path); |
| 388 |
|
|
| 389 |
|
// Rebuild the folder cache |
| 390 |
|
/*. |
| 391 |
|
if ($success) { |
| 392 |
|
mail_api_rebuild_folder_cache(); |
| 393 |
|
} |
| 394 |
|
*/ |
| 395 |
|
|
| 396 |
|
return $success; |
| 397 |
|
} |
| 398 |
|
|
| 399 |
|
/** |
| 400 |
|
* Delete folder in user's mail server account |
| 401 |
|
*/ |
| 402 |
|
function mail_api_delete_folder($folder_path) { |
| 403 |
|
// Call hook_mail_api_delete_folder() on the appropriate mail server module. |
| 404 |
|
$success = mail_api_invoke('mail_api_delete_folder', $folder_path); |
| 405 |
|
|
| 406 |
|
// Rebuild the folder cache |
| 407 |
|
/*. |
| 408 |
|
if ($success) { |
| 409 |
|
mail_api_rebuild_folder_cache(); |
| 410 |
|
} |
| 411 |
|
*/ |
| 412 |
|
|
| 413 |
|
return $success; |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
/** |
| 417 |
|
* Get all tags from mail server and store them in the cache. |
| 418 |
|
*/ |
| 419 |
|
function mail_api_rebuild_tag_cache() { |
| 420 |
|
global $user; |
| 421 |
|
|
| 422 |
|
// get user's tags from the mail server |
| 423 |
|
if ($tags = mail_api_invoke('mail_api_tags')) { |
| 424 |
|
// store them in the database |
| 425 |
|
db_query('DELETE FROM {mail_api_tag_cache} WHERE uid = %d', $user->uid); |
| 426 |
|
foreach ($tags as $tag) { |
| 427 |
|
db_query('INSERT INTO {mail_api_tag_cache} (uid, name) VALUES (%d, "%s")', $user->uid, $tag['name']); |
| 428 |
|
} |
| 429 |
|
} |
| 430 |
|
} |
| 431 |
|
|
| 432 |
|
/** |
| 433 |
|
* Get tags from cache. |
| 434 |
|
* |
| 435 |
|
* @return: Returns an array of tags. |
| 436 |
|
*/ |
| 437 |
|
function mail_api_tags() { |
| 438 |
|
global $user; |
| 439 |
|
$result = db_query('SELECT * FROM {mail_api_tag_cache} WHERE uid = %d', $user->uid); |
| 440 |
|
|
| 441 |
|
while ($tag = db_fetch_array($result)) { |
| 442 |
|
$tags[] = $tag; |
| 443 |
|
} |
| 444 |
|
|
| 445 |
|
return $tags; |
| 446 |
|
} |
| 447 |
|
|
| 448 |
|
/** |
| 449 |
|
* Rename a tag. |
| 450 |
|
*/ |
| 451 |
|
function mail_api_rename_tag($tag, $new_tag) { |
| 452 |
|
// Call hook_mail_api_rename_tag() on the appropriate mail server module. |
| 453 |
|
$success = mail_api_invoke('mail_api_rename_tag', $tag, $new_tag); |
| 454 |
|
|
| 455 |
|
// Rebuild the tag cache. |
| 456 |
|
if ($success) { |
| 457 |
|
mail_api_rebuild_tag_cache(); |
| 458 |
|
} |
| 459 |
|
|
| 460 |
|
return $success; |
| 461 |
|
} |
| 462 |
|
|
| 463 |
|
/** |
| 464 |
|
* Delete a tag. |
| 465 |
|
*/ |
| 466 |
|
function mail_api_delete_tag($tag) { |
| 467 |
|
// Call hook_mail_api_delete_tag() on the appropriate mail server module. |
| 468 |
|
$success = mail_api_invoke('mail_api_delete_tag', $tag); |
| 469 |
|
|
| 470 |
|
// Rebuild the tag cache. |
| 471 |
|
if ($success) { |
| 472 |
|
mail_api_rebuild_tag_cache(); |
| 473 |
|
} |
| 474 |
|
|
| 475 |
|
return $success; |
| 476 |
|
} |
| 477 |
|
|
| 478 |
|
/** |
| 479 |
|
* Get a page of results from a search. |
| 480 |
|
*/ |
| 481 |
|
function mail_api_pager_search($keywords = '', $folders = array(), $flags = array(), $header_fields = array(), $tags = array(), $sections = array(), $sort = array(), $limit = 25, $element = 0) { |
| 482 |
|
global $pager_page_array, $pager_total, $pager_total_items; |
| 483 |
|
$page = isset($_GET['page']) ? $_GET['page'] : ''; |
| 484 |
|
|
| 485 |
|
// Call hook_mail_api_pager_search(). |
| 486 |
|
$search_results = mail_api_invoke('mail_api_pager_search', $keywords, $folders, $flags, $header_fields, $tags, $sections, $sort, $limit); |
| 487 |
|
|
| 488 |
|
$pager_page_array = explode(',', $page); |
| 489 |
|
|
| 490 |
|
$pager_total_items[$element] = $search_results['count']; |
| 491 |
|
$pager_total[$element] = ceil($pager_total_items[$element] / $limit); |
| 492 |
|
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1)); |
| 493 |
|
|
| 494 |
|
return $search_results; |
| 495 |
|
} |
| 496 |
|
|
| 497 |
|
/** |
| 498 |
|
* Send an email. |
| 499 |
|
*/ |
| 500 |
|
function mail_api_send($message) { |
| 501 |
|
global $user; |
| 502 |
|
|
| 503 |
|
// Get domain name of user's account. |
| 504 |
|
$domain = mail_api_get_domain(); |
| 505 |
|
// Get server configuration for that domain. |
| 506 |
|
$server = mail_api_server_configuration($domain); |
| 507 |
|
|
| 508 |
|
if ($server->outbox) { |
| 509 |
|
// if the mail server supports sending messages, do this |
| 510 |
|
return mail_api_invoke('mail_api_send', $message); |
| 511 |
|
} |
| 512 |
|
elseif (module_exists('smtp')) { |
| 513 |
|
// this is based off the drupal_mail_wrapper() function from the SMTP Auth module |
| 514 |
|
// we could not use that function as is, however, since it uses site-wide settings |
| 515 |
|
// and we are using mail server specific and user specific settings |
| 516 |
|
|
| 517 |
|
$mail = new phpmailer(); //Create a new phpmailer object. |
| 518 |
|
$username = $server->smtp_primary_username; |
| 519 |
|
$password = $server->smtp_primary_password; |
| 520 |
|
|
| 521 |
|
$from_name = $user->name; |
| 522 |
|
$from = $user->name; |
| 523 |
|
|
| 524 |
|
$auth = $server->smtp_primary_auth ? TRUE : FALSE; |
| 525 |
|
|
| 526 |
|
if (!$username) { |
| 527 |
|
// if there's not a smtp username set for this mail server |
| 528 |
|
// use the user's username and password |
| 529 |
|
$mail->Username = $user->name; |
| 530 |
|
$mail->Password = mail_api_get_password(); |
| 531 |
|
} |
| 532 |
|
else { |
| 533 |
|
// else use the configured smtp username/passwords for this server. |
| 534 |
|
$mail->Username = $username; |
| 535 |
|
$mail->Password = $password; |
| 536 |
|
} |
| 537 |
|
|
| 538 |
|
//Take care of the email headers. |
| 539 |
|
foreach ($message['headers'] as $name => $value) { |
| 540 |
|
//watchdog('error', 'Key: ' . $name . ' Value: ' . $value); |
| 541 |
|
if (strtolower($name) == 'content-type' && strpos(strtolower($value), 'text/html') !== FALSE) { |
| 542 |
|
$mail->IsHTML(TRUE); |
| 543 |
|
} |
| 544 |
|
else if (strtolower($name) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) { |
| 545 |
|
//$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone |
| 546 |
|
$mail->AddCustomHeader($name .': '. $value); |
| 547 |
|
$mail->message_type = 'pre'; |
| 548 |
|
} |
| 549 |
|
else if (strtolower($name) == 'reply-to') { |
| 550 |
|
$mail->AddReplyTo = $value; |
| 551 |
|
} |
| 552 |
|
else if (strtolower($name) == 'return-path') { |
| 553 |
|
if (trim($value) != '') { |
| 554 |
|
$mail->Sender = $value; |
| 555 |
|
} |
| 556 |
|
} |
| 557 |
|
else if (strtolower($name) == 'content-transfer-encoding') { |
| 558 |
|
$mail->Encoding = $value; |
| 559 |
|
} |
| 560 |
|
else if (strtolower($name) == 'mime-version') { |
| 561 |
|
// just ommit MIME-Version it since it will be set by PHP-Mailer |
| 562 |
|
} |
| 563 |
|
else if (strtolower($name) == 'bcc') { |
| 564 |
|
$bccrecipients = split(',', $value); |
| 565 |
|
foreach ($bccrecipients as $bccrecipient) { |
| 566 |
|
if ( strpos($bccrecipient, '<') !== FALSE ) { |
| 567 |
|
$bccparts = explode(' <', $bccrecipient); |
| 568 |
|
$bccname = $bccparts[0]; |
| 569 |
|
$bccaddr = rtrim($bccparts[1], '>'); |
| 570 |
|
} |
| 571 |
|
else { |
| 572 |
|
$bccname = ''; |
| 573 |
|
$bccaddr = $bccrecipient; |
| 574 |
|
} |
| 575 |
|
$mail->AddBCC($bccaddr, $bccname); |
| 576 |
|
} |
| 577 |
|
} |
| 578 |
|
else { //Else the header key is not special, just add it. |
| 579 |
|
$mail->AddCustomHeader($name .': '. $value); //Add header line. |
| 580 |
|
} |
| 581 |
|
} |
| 582 |
|
|
| 583 |
|
$enc_options = mail_api_encryption_options(); |
| 584 |
|
switch (strtolower($enc_options[$server->smtp_primary_encryption])) { |
| 585 |
|
case 'ssl': |
| 586 |
|
$mail->Protocol = 'ssl://'; |
| 587 |
|
break; |
| 588 |
|
case 'tls': |
| 589 |
|
$mail->Protocol = 'tls://'; |
| 590 |
|
break; |
| 591 |
|
case 'none': |
| 592 |
|
$mail->Protocol = ''; |
| 593 |
|
} |
| 594 |
|
|
| 595 |
|
$mail->Host = $server->smtp_primary_hostname; |
| 596 |
|
$mail->Port = $server->smtp_primary_port; |
| 597 |
|
$mail->Mailer = 'smtp'; |
| 598 |
|
$mail->SMTPAuth = $auth; |
| 599 |
|
|
| 600 |
|
$mail->CharSet = 'utf-8'; |
| 601 |
|
|
| 602 |
|
$mail->From = $from; |
| 603 |
|
$mail->FromName = $from_name; |
| 604 |
|
|
| 605 |
|
$torecipients = split(',', $message['to']); |
| 606 |
|
foreach ($torecipients as $torecipient) { |
| 607 |
|
if (strpos($torecipient, '<') !== FALSE) { |
| 608 |
|
$toparts = explode(' <', $torecipient); |
| 609 |
|
$toname = $toparts[0]; |
| 610 |
|
$toaddr = rtrim($toparts[1], '>'); |
| 611 |
|
} |
| 612 |
|
else { |
| 613 |
|
$toname = ""; |
| 614 |
|
$toaddr = $torecipient; |
| 615 |
|
} |
| 616 |
|
$mail->AddAddress($toaddr, $toname); |
| 617 |
|
} |
| 618 |
|
|
| 619 |
|
$mail->Subject = $message['subject']; |
| 620 |
|
$mail->Body = $message['body']; |
| 621 |
|
|
| 622 |
|
watchdog('mail_api', t('Sending mail to: !to', array('!to' => $message['to']))); |
| 623 |
|
|
| 624 |
|
//Try to send email, if it fails set watchdog entry. |
| 625 |
|
if (!$mail->Send()) { |
| 626 |
|
watchdog('mail_api', t('Error sending e-mail from !from to !to: . Attempting any configured backup mail delivery methods.', array('!from' => $from, '!to' => $message['to'])) . $mail->ErrorInfo, NULL, WATCHDOG_ERROR); |
| 627 |
|
|
| 628 |
|
// change to backup smtp server information |
| 629 |
|
$mail->Host = $server->smtp_backup_hostname; |
| 630 |
|
$mail->Port = $server->smtp_backup_port; |
| 631 |
|
|
| 632 |
|
switch (strtolower($enc_options[$server->smtp_backup_encryption])) { |
| 633 |
|
case 'ssl': |
| 634 |
|
$mail->Protocol = 'ssl://'; |
| 635 |
|
break; |
| 636 |
|
case 'tls': |
| 637 |
|
$mail->Protocol = 'tls://'; |
| 638 |
|
break; |
| 639 |
|
case 'none': |
| 640 |
|
$mail->Protocol = ''; |
| 641 |
|
} |
| 642 |
|
|
| 643 |
|
$username = $server->smtp_backup_username; |
| 644 |
|
$password = $server->smtp_backup_password; |
| 645 |
|
$auth = $server->smtp_backup_auth ? TRUE : FALSE; |
| 646 |
|
|
| 647 |
|
if ($username == '' && $password == '') { |
| 648 |
|
$mail->Username = $user->name; |
| 649 |
|
$mail->Password = mail_api_get_password(); |
| 650 |
|
} |
| 651 |
|
else { |
| 652 |
|
$mail->Username = $username; |
| 653 |
|
$mail->Password = $password; |
| 654 |
|
} |
| 655 |
|
|
| 656 |
|
$mail->SMTPAuth = $auth; |
| 657 |
|
|
| 658 |
|
if (!$mail->Send()) { |
| 659 |
|
watchdog('mail_api', t('Error sending e-mail from !from to !to: from backup SMTP server.', array('!from' => $from, '!to' => $message['to'])) . $mail->ErrorInfo, NULL, WATCHDOG_ERROR); |
| 660 |
|
return FALSE; |
| 661 |
|
} |
| 662 |
|
} |
| 663 |
|
|
| 664 |
|
$mail->SmtpClose(); |
| 665 |
|
return TRUE; |
| 666 |
|
} |
| 667 |
|
else { |
| 668 |
|
// error: no supported methods for sending emails |
| 669 |
|
watchdog('mail_api', t('Error: no supported/configured methods for sending emails.'), NULL, WATCHDOG_ERROR); |
| 670 |
|
return FALSE; |
| 671 |
|
} |
| 672 |
|
} |
| 673 |
|
|
| 674 |
|
|
| 675 |
|
function mail_api_format_mailbox($mailbox) { |
| 676 |
|
global $mail_api_server_info; |
| 677 |
|
|
| 678 |
|
|
| 679 |
|
if (!$mail_api_server_info->hide_folder_prefix) return $mailbox; |
| 680 |
|
|
| 681 |
|
if (strstr($mailbox, $mail_api_server_info->folder_prefix)) { |
| 682 |
|
$new = substr($mailbox, strpos($mailbox, $mail_api_server_info->folder_prefix)+strlen($mail_api_server_info->folder_prefix), strlen($mailbox)); |
| 683 |
|
} |
| 684 |
|
|
| 685 |
|
return $new; |
| 686 |
|
|
| 687 |
|
} |
| 688 |
|
|
| 689 |
|
|
| 690 |
|
|
| 691 |
|
|
| 692 |
|
/** |
| 693 |
|
* returns the path to the inbox folder |
| 694 |
|
* |
| 695 |
|
* @param unknown_type $domain |
| 696 |
|
* @return unknown |
| 697 |
|
*/ |
| 698 |
|
function mail_api_inbox_path($domain) { |
| 699 |
|
if ($domain=="") return FALSE; |
| 700 |
|
$object = mail_api_server_configuration($domain); |
| 701 |
|
|
| 702 |
|
return $object->inbox_folder; |
| 703 |
|
} |
| 704 |
|
|
| 705 |
|
/** |
| 706 |
|
* returns the path to the sent folder |
| 707 |
|
* |
| 708 |
|
* @param unknown_type $domain |
| 709 |
|
* @return unknown |
| 710 |
|
*/ |
| 711 |
|
function mail_api_sent_path($domain) { |
| 712 |
|
if ($domain=="") return FALSE; |
| 713 |
|
$object = mail_api_server_configuration($domain); |
| 714 |
|
|
| 715 |
|
return $object->sent_folder; |
| 716 |
|
} |
| 717 |
|
|
| 718 |
|
/** |
| 719 |
|
* returns the path to the drafts folder |
| 720 |
|
* |
| 721 |
|
* @param unknown_type $domain |
| 722 |
|
* @return unknown |
| 723 |
|
*/ |
| 724 |
|
function mail_api_drafts_path($domain) { |
| 725 |
|
if ($domain=="") return FALSE; |
| 726 |
|
$object = mail_api_server_configuration($domain); |
| 727 |
|
|
| 728 |
|
return $object->drafts_folder; |
| 729 |
|
} |
| 730 |
|
|
| 731 |
|
/** |
| 732 |
|
* returns the path to the junk folder |
| 733 |
|
* |
| 734 |
|
* @param unknown_type $domain |
| 735 |
|
* @return unknown |
| 736 |
|
*/ |
| 737 |
|
function mail_api_junk_path($domain) { |
| 738 |
|
if ($domain=="") return FALSE; |
| 739 |
|
$object = mail_api_server_configuration($domain); |
| 740 |
|
|
| 741 |
|
return $object->junk_folder; |
| 742 |
|
} |
| 743 |
|
|
| 744 |
|
|
| 745 |
|
|
| 746 |
|
/** |
| 747 |
|
* returns the path to the trash folder |
| 748 |
|
* |
| 749 |
|
* @param unknown_type $domain |
| 750 |
|
* @return unknown |
| 751 |
|
*/ |
| 752 |
|
function mail_api_trash_path($domain) { |
| 753 |
|
if ($domain=="") return FALSE; |
| 754 |
|
$object = mail_api_server_configuration($domain); |
| 755 |
|
|
| 756 |
|
return $object->trash_folder; |
| 757 |
|
} |
| 758 |
|
|
| 759 |
|
/** |
| 760 |
|
* appends a message to the mailbox |
| 761 |
|
* |
| 762 |
|
*/ |
| 763 |
|
function mail_api_append($message) { |
| 764 |
|
$rs = mail_api_invoke('append', $message); |
| 765 |
|
return $rs; |
| 766 |
|
} |
| 767 |
|
|
| 768 |
|
/** |
| 769 |
|
* moves a message from the current folder to the specified folder |
| 770 |
|
* |
| 771 |
|
* @param unknown_type $message_uid |
| 772 |
|
* @param unknown_type $folder |
| 773 |
|
*/ |
| 774 |
|
function mail_api_move_message($message_uid, $folder) { |
| 775 |
|
$rs = mail_api_invoke('move_message', $message_uid, $folder); |
| 776 |
|
return $rs; |
| 777 |
|
} |
| 778 |
|
|
| 779 |
|
/** |
| 780 |
|
* deletes a message from the current folder |
| 781 |
|
* |
| 782 |
|
* @param unknown_type $message_uid |
| 783 |
|
* @param unknown_type $folder |
| 784 |
|
*/ |
| 785 |
|
function mail_api_delete_message($message_uid) { |
| 786 |
|
$rs = mail_api_invoke('delete_message', $message_uid); |
| 787 |
|
return $rs; |
| 788 |
|
} |
| 789 |
|
|
| 790 |
|
|
| 791 |
|
|
| 792 |
|
/** |
| 793 |
|
* flags a message in the current folder as read |
| 794 |
|
* |
| 795 |
|
* @param unknown_type $message_uid |
| 796 |
|
* @return unknown |
| 797 |
|
*/ |
| 798 |
|
function mail_api_flag_read($message_uid) { |
| 799 |
|
$rs = mail_api_invoke('mark_read', $message_uid); |
| 800 |
|
return $rs; |
| 801 |
|
} |
| 802 |
|
|
| 803 |
|
/** |
| 804 |
|
* flags a message in the current folder as unread |
| 805 |
|
* |
| 806 |
|
* @param unknown_type $message_uid |
| 807 |
|
* @return unknown |
| 808 |
|
*/ |
| 809 |
|
function mail_api_flag_unread($message_uid) { |
| 810 |
|
$rs = mail_api_invoke('mark_unread', $message_uid); |
| 811 |
|
return $rs; |
| 812 |
|
} |
| 813 |
|
|
| 814 |
|
|
| 815 |
|
function mail_api_get_structure($message_uid) { |
| 816 |
|
$rs = mail_api_invoke('get_structure', $message_uid); |
| 817 |
|
return $rs; |
| 818 |
|
} |
| 819 |
|
|
| 820 |
|
function mail_api_get_map($message_uid) { |
| 821 |
|
$rs = mail_api_invoke('get_map', $message_uid); |
| 822 |
|
return $rs; |
| 823 |
|
} |
| 824 |
|
|
| 825 |
|
/** |
| 826 |
|
* gets the plain text of the message body, the set_seen flag will set the message as seen if used |
| 827 |
|
* |
| 828 |
|
* @param unknown_type $message_uid |
| 829 |
|
* @param unknown_type $seen |
| 830 |
|
* @return unknown |
| 831 |
|
*/ |
| 832 |
|
function mail_api_get_plaintext($message_uid, $set_seen=NULL) { |
| 833 |
|
$rs = mail_api_invoke('get_plaintext', $message_uid, $seen); |
| 834 |
|
return $rs; |
| 835 |
|
} |
| 836 |
|
|
| 837 |
|
function mail_api_get_attachment_overview($message_uid) { |
| 838 |
|
$rs = mail_api_invoke('get_attachment_overview', $message_uid); |
| 839 |
|
return $rs; |
| 840 |
|
} |
| 841 |
|
|
| 842 |
|
function mail_api_get_attachment_files($message_uid) { |
| 843 |
|
$rs = mail_api_invoke('get_attachment_files', $message_uid); |
| 844 |
|
return $rs; |
| 845 |
|
} |
| 846 |
|
|
| 847 |
|
function mail_api_get_part($message_uid, $part_number) { |
| 848 |
|
$rs = mail_api_invoke('get_part', $message_uid, $part_number); |
| 849 |
|
return $rs; |
| 850 |
|
} |
| 851 |
|
|
| 852 |
|
|
| 853 |
|
|
| 854 |
|
function mail_api_get_part_object($message_uid, $part_number) { |
| 855 |
|
$rs = mail_api_invoke('get_part_object', $message_uid, $part_number); |
| 856 |
|
return $rs; |
| 857 |
|
} |
| 858 |
|
|
| 859 |
|
function mail_api_get_part_mime_type($message_uid, $part_number) { |
| 860 |
|
$rs = mail_api_invoke('get_part_mime_type', $message_uid, $part_number); |
| 861 |
|
return $rs; |
| 862 |
|
} |