new: [internal] Log if e-mail was send encrypted or not

pull/5240/head
Jakub Onderka 2020-04-25 14:59:01 +02:00
parent 652df56ba6
commit 6d1757188a
2 changed files with 18 additions and 14 deletions

View File

@ -331,7 +331,7 @@ class SendEmail
* @param string $body
* @param string|null $bodyWithoutEncryption
* @param array $replyToUser
* @return bool
* @return bool True if e-mail is encrypted, false if not.
* @throws SendEmailException
*/
public function sendToUser(array $user, $subject, $body, $bodyWithoutEncryption = null, array $replyToUser = array())
@ -370,6 +370,8 @@ class SendEmail
}
}
$encrypted = false;
if ($canEncryptGpg) {
if (!$this->gpg) {
throw new SendEmailException("GPG encryption is enabled, but GPG is not configured.");
@ -388,6 +390,7 @@ class SendEmail
try {
$this->gpg->addEncryptKey($fingerprint);
$this->encryptByGpg($email);
$encrypted = true;
} catch (Exception $e) {
throw new SendEmailException("The message could not be encrypted.", 0, $e);
}
@ -396,11 +399,12 @@ class SendEmail
if (!$canEncryptGpg && $canEncryptSmime) {
$this->signBySmime($email);
$this->encryptBySmime($email, $user['User']['certif_public']);
$encrypted = true;
}
try {
$email->send();
return true;
return $encrypted;
} catch (Exception $e) {
throw new SendEmailException("The message could be sent.", 0, $e);
}
@ -620,7 +624,7 @@ class SendEmail
} else {
$outputFile->delete();
throw new SendEmailException('Failed while attempting to sign the SMIME message: ' . openssl_error_string());
throw new SendEmailException('Failed while attempting to sign the S/MIME message: ' . openssl_error_string());
}
}
@ -648,7 +652,7 @@ class SendEmail
return $parts[1];
} else {
$outputFile->delete();
throw new SendEmailException('Could not encrypt the SMIME message: ' . openssl_error_string());
throw new SendEmailException('Could not encrypt the S/MIME message: ' . openssl_error_string());
}
}

View File

@ -319,11 +319,11 @@ class User extends AppModel
return true;
}
} catch (Exception $e) {
$this->log($e->getMessage());
$this->logException("Exception during importing GPG key", $e);
return false;
}
} catch (Exception $e) {
$this->log($e->getMessage());
$this->logException("Exception during initializing GPG", $e);
return true;
}
}
@ -565,9 +565,6 @@ class User extends AppModel
} catch (Exception $e) {
$this->log($e->getMessage());
}
unlink($msg_test);
unlink($msg_test_encrypted);
return $result;
}
public function verifyCertificate()
@ -757,7 +754,7 @@ class User extends AppModel
}
$this->Log = ClassRegistry::init('Log');
$replyToLog = $replyToUser ? 'from ' . $replyToUser['User']['email'] : '';
$replyToLog = $replyToUser ? ' from ' . $replyToUser['User']['email'] : '';
try {
$gpg = $this->initializeGpg();
@ -767,10 +764,10 @@ class User extends AppModel
$sendEmail = new SendEmail($gpg);
try {
$sendEmail->sendToUser($user, $subject, $body, $bodyNoEnc ?: null, $replyToUser ?: array());
$encrypted = $sendEmail->sendToUser($user, $subject, $body, $bodyNoEnc ?: null, $replyToUser ?: array());
} catch (SendEmailException $e) {
$this->log($e->getMessage()); // TODO: Log whole exception
$this->logException("Exception during sending e-mail", $e);
$this->Log->create();
$this->Log->save(array(
'org' => 'SYSTEM',
@ -778,12 +775,15 @@ class User extends AppModel
'model_id' => $user['User']['id'],
'email' => $user['User']['email'],
'action' => 'email',
'title' => 'Email ' . $replyToLog . ' to ' . $user['User']['email'] . ', titled "' . $subject . '" failed. Reason: ' . $e->getMessage(),
'title' => 'Email' . $replyToLog . ' to ' . $user['User']['email'] . ', titled "' . $subject . '" failed. Reason: ' . $e->getMessage(),
'change' => null,
));
return false;
}
$logTitle = $encrypted ? 'Encrypted email' : 'Email';
$logTitle .= $replyToLog . ' to ' . $user['User']['email'] . ' sent, titled "' . $subject . '".';
$this->Log->create();
$this->Log->save(array(
'org' => 'SYSTEM',
@ -791,7 +791,7 @@ class User extends AppModel
'model_id' => $user['User']['id'],
'email' => $user['User']['email'],
'action' => 'email',
'title' => 'Email ' . $replyToLog . ' to ' . $user['User']['email'] . ' sent, titled "' . $subject . '".',
'title' => $logTitle,
'change' => null,
));
return true;