function _engagements_imap_get_body

7.x engagements.module _engagements_imap_get_body($imapLink, $msgno)
6.x engagements.module _engagements_imap_get_body($imapLink, $msgno)
1 call to _engagements_imap_get_body()
engagements_imap_get_all_received_messages in modules/engagements/engagements.module
Connect to our imap server, download all received messages from students (or others). We will then delete them, so they don't get read twice.

File

modules/engagements/engagements.module, line 2333
This is the primary module file for the engagements module.

Code

function _engagements_imap_get_body($imapLink, $msgno) 
 {

  $obj_structure = imap_fetchstructure($imapLink, $msgno);
  // Recherche de la section contenant le corps du message et extraction du contenu
  $obj_section = $obj_structure;



  //$text = imap_fetchbody($imapLink, $msgno, $section);

  // So it turns out the $section for HTML can be 1.2, but it might also be just "2" in simple messages.
  // Try to get HTML first....
  $text = trim(imap_fetchbody($imapLink, $msgno, 1.2));
  if ($text == "") {
    $text = trim(imap_fetchbody($imapLink, $msgno, 2));
  }
  if ($text == "") {
    $text = trim(imap_fetchbody($imapLink, $msgno, 1.1));
  }
  if ($text == "") {
    $text = trim(imap_fetchbody($imapLink, $msgno, 1));
  }


  // Décodage éventuel
  if ($obj_section->encoding == 3) {
    $text = imap_base64($text);
  }
  else if ($obj_section->encoding == 4) {
    $text = imap_qprint($text);
  }
  // Encodage éventuel
  foreach ($obj_section->parameters as $obj_param) {
    if (($obj_param->attribute == "charset") && (mb_strtoupper($obj_param->value) != "UTF-8")) {
      $text = utf8_encode($text);
      break;
    }
  }



  $text = filter_markup($text, 'basic');

  // Sometimes, emails contain odd encoding.  Ex:  =C2=A0, which is apparently a non-breaking space.  Let's convert just in case.
  // From: https://stackoverflow.com/questions/12682208/parsing-email-body-with-7bit-content-transfer-encoding-php
  // and: https://github.com/geerlingguy/Imap/blob/c4b54e52576bf71a93045d2a4581589eab9a00b6/JJG/Imap.php#L398
  // Manually convert common encoded characters into their UTF-8 equivalents.
  $characters = array(
    '=20' => ' ', // space.
    '=2C' => ',', // comma.
    '=E2=80=99' => "'", // single quote.
    '=C2=A0' => ' ', // non-breaking space.      
    '=0A' => "\r\n", // line break.
    '=0D' => "\r\n", // carriage return.
    '=A0' => ' ', // non-breaking space.
    "=\r\n" => '', // joined line.
    '=E2=80=A6' => '…', // ellipsis.
    '=E2=80=A2' => '•', // bullet.
    '=E2=80=93' => '–', // en dash.
    '=E2=80=94' => '—', // em dash.
  );

  // Loop through the encoded characters and replace any that are found.
  foreach ($characters as $key => $value) {
    $text = str_replace($key, $value, $text);
  }



  return $text;
}