Skip to content
Snippets Groups Projects
Commit 5741f2a3 authored by Jean-Laurent DUZANT's avatar Jean-Laurent DUZANT
Browse files

FEAT #23044 TIME 0:20 add useSMTPAuth parameter && Replace sender email by...

FEAT #23044 TIME 0:20 add useSMTPAuth parameter && Replace sender email by SMTP sender email when useSMTPAuth is active && remove SMTP sender rejected errors
parent 4589da94
No related branches found
No related tags found
No related merge requests found
...@@ -46,11 +46,6 @@ use User\models\UserModel; ...@@ -46,11 +46,6 @@ use User\models\UserModel;
class EmailController class EmailController
{ {
private const SMTP_ERRORS_SENDER_REJECTED = [
'Client does not have permissions to send as this sender',
'Sender address rejected'
];
public function send(Request $request, Response $response) public function send(Request $request, Response $response)
{ {
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'sendmail', 'userId' => $GLOBALS['id']])) { if (!PrivilegeController::hasPrivilege(['privilegeId' => 'sendmail', 'userId' => $GLOBALS['id']])) {
...@@ -80,6 +75,16 @@ class EmailController ...@@ -80,6 +75,16 @@ class EmailController
return ['errors' => $check['errors'], 'code' => $check['code']]; return ['errors' => $check['errors'], 'code' => $check['code']];
} }
$configuration = ConfigurationModel::getByPrivilege(['privilege' => 'admin_email_server', 'select' => ['value']]);
$configuration = json_decode($configuration['value'], true);
if (empty($configuration)) {
return ['errors' => 'Configuration is missing'];
}
if (!empty($configuration['useSMTPAuth'])) {
$args['data']['sender']['email'] = $configuration['from'];
}
$id = EmailModel::create([ $id = EmailModel::create([
'userId' => $args['userId'], 'userId' => $args['userId'],
'sender' => empty($args['data']['sender']) ? '{}' : json_encode($args['data']['sender']), 'sender' => empty($args['data']['sender']) ? '{}' : json_encode($args['data']['sender']),
...@@ -121,12 +126,18 @@ class EmailController ...@@ -121,12 +126,18 @@ class EmailController
exec("php src/app/email/scripts/sendEmail.php {$customId} {$id} {$args['userId']} '{$encryptKey}' '{$options}' > /dev/null &"); exec("php src/app/email/scripts/sendEmail.php {$customId} {$id} {$args['userId']} '{$encryptKey}' '{$options}' > /dev/null &");
} }
if (!empty($isSent)) { if (!empty($isSent)) {
$info = _EMAIL_ADDED ;
if (!empty($configuration['useSMTPAuth'])) {
$info .= ' : ' . _SENDER_EMAIL_REPLACED_SMTP_SENDER;
}
HistoryController::add([ HistoryController::add([
'tableName' => 'emails', 'tableName' => 'emails',
'recordId' => $id, 'recordId' => $id,
'eventType' => 'ADD', 'eventType' => 'ADD',
'eventId' => 'emailCreation', 'eventId' => 'emailCreation',
'info' => _EMAIL_ADDED 'info' => $info
]); ]);
if (!empty($args['data']['document']['id'])) { if (!empty($args['data']['document']['id'])) {
...@@ -135,7 +146,7 @@ class EmailController ...@@ -135,7 +146,7 @@ class EmailController
'recordId' => $args['data']['document']['id'], 'recordId' => $args['data']['document']['id'],
'eventType' => 'ADD', 'eventType' => 'ADD',
'eventId' => 'emailCreation', 'eventId' => 'emailCreation',
'info' => _EMAIL_ADDED 'info' => $info
]); ]);
} }
} }
...@@ -289,6 +300,16 @@ class EmailController ...@@ -289,6 +300,16 @@ class EmailController
return $response->withStatus($check['code'])->withJson(['errors' => $check['errors']]); return $response->withStatus($check['code'])->withJson(['errors' => $check['errors']]);
} }
$configuration = ConfigurationModel::getByPrivilege(['privilege' => 'admin_email_server', 'select' => ['value']]);
$configuration = json_decode($configuration['value'], true);
if (empty($configuration)) {
return ['errors' => 'Configuration is missing'];
}
if (!empty($configuration['useSMTPAuth'])) {
$body['sender']['email'] = $configuration['from'];
}
EmailModel::update([ EmailModel::update([
'set' => [ 'set' => [
'sender' => empty($body['sender']) ? '{}' : json_encode($body['sender']), 'sender' => empty($body['sender']) ? '{}' : json_encode($body['sender']),
...@@ -331,12 +352,18 @@ class EmailController ...@@ -331,12 +352,18 @@ class EmailController
]); ]);
} }
} else { } else {
$info = _EMAIL_UPDATED ;
if (!empty($configuration['useSMTPAuth'])) {
$info .= ' : ' . _SENDER_EMAIL_REPLACED_SMTP_SENDER;
}
HistoryController::add([ HistoryController::add([
'tableName' => 'emails', 'tableName' => 'emails',
'recordId' => $args['id'], 'recordId' => $args['id'],
'eventType' => 'UP', 'eventType' => 'UP',
'eventId' => 'emailModification', 'eventId' => 'emailModification',
'info' => _EMAIL_UPDATED 'info' => $info
]); ]);
if (!empty($args['data']['document']['id'])) { if (!empty($args['data']['document']['id'])) {
...@@ -345,7 +372,7 @@ class EmailController ...@@ -345,7 +372,7 @@ class EmailController
'recordId' => $args['data']['document']['id'], 'recordId' => $args['data']['document']['id'],
'eventType' => 'UP', 'eventType' => 'UP',
'eventId' => 'emailModification', 'eventId' => 'emailModification',
'info' => _EMAIL_UPDATED 'info' => $info
]); ]);
} }
} }
...@@ -761,26 +788,6 @@ class EmailController ...@@ -761,26 +788,6 @@ class EmailController
$errors = !empty($history[0]['info']) ? $history[0]['info'] : $phpmailer->ErrorInfo; $errors = !empty($history[0]['info']) ? $history[0]['info'] : $phpmailer->ErrorInfo;
// If we cannot override from with the sender email address, we try sending the email with the from in the configuration
if ($email['sender']['email'] != $configuration['from']) {
$tryWithConfigFrom = false;
foreach (EmailController::SMTP_ERRORS_SENDER_REJECTED as $errorCandidate) {
if (stripos($errors, $errorCandidate) !== false) {
$tryWithConfigFrom = true;
break;
}
}
if ($tryWithConfigFrom) {
$sender = [
'email' => $configuration['from'],
'entityId' => $email['sender']['entityId'] ?? null
];
EmailModel::update(['set' => ['sender' => json_encode($sender)], 'where' => ['id = ?'], 'data' => [$args['emailId']]]);
return EmailController::sendEmail(['emailId' => $args['emailId'], 'userId' => $args['userId']]);
}
}
return ['errors' => $errors]; return ['errors' => $errors];
} }
......
...@@ -58,6 +58,7 @@ define('_DOCUMENT_OUT_PERIMETER', 'Courrier en dehors du périmètre'); ...@@ -58,6 +58,7 @@ define('_DOCUMENT_OUT_PERIMETER', 'Courrier en dehors du périmètre');
define('_EMAIL_ADDED', 'Courriel ajouté'); define('_EMAIL_ADDED', 'Courriel ajouté');
define('_EMAIL_REMOVED', 'Courriel supprimé'); define('_EMAIL_REMOVED', 'Courriel supprimé');
define('_EMAIL_UPDATED', 'Courriel mis à jour'); define('_EMAIL_UPDATED', 'Courriel mis à jour');
define('_SENDER_EMAIL_REPLACED_SMTP_SENDER', "L'email de l'expéditeur est remplacé par l'expéditeur SMTP");
define('_ENTITY_CREATION', 'Création entité'); define('_ENTITY_CREATION', 'Création entité');
define('_ENTITY_MODIFICATION', 'Modification entité'); define('_ENTITY_MODIFICATION', 'Modification entité');
define('_ENTITY_SUPPRESSION', 'Suppression entité'); define('_ENTITY_SUPPRESSION', 'Suppression entité');
......
...@@ -69,11 +69,19 @@ ...@@ -69,11 +69,19 @@
</div> </div>
<div class="form-group" <div class="form-group"
[style.opacity]="['smtp', 'mail'].indexOf(sendmail.type) > -1 ? '1' : '0.5'"> [style.opacity]="['smtp', 'mail'].indexOf(sendmail.type) > -1 ? '1' : '0.5'">
<div class="col-md-12"> <div class="col-md-4" style="margin-bottom: 10px;">
<mat-slide-toggle color="primary" name="SMTPAuth" [(ngModel)]="sendmail.auth" <mat-slide-toggle color="primary" name="SMTPAuth" [(ngModel)]="sendmail.auth"
[disabled]="['smtp', 'mail'].indexOf(sendmail.type) == -1" [disabled]="['smtp', 'mail'].indexOf(sendmail.type) == -1"
(change)="cleanAuthInfo($event)">{{'lang.enableAuth' | translate}}</mat-slide-toggle> (change)="cleanAuthInfo($event)">{{'lang.enableAuth' | translate}}</mat-slide-toggle>
</div> </div>
<div class="col-md-8" style="margin-bottom: 10px;">
<mat-slide-toggle color="primary" name="canUseIt"
[(ngModel)]="sendmail.useSMTPAuth"
[title]="'lang.useSMTPAuthDesc' | translate"
[disabled]="['smtp', 'mail'].indexOf(sendmail.type) == -1">
{{'lang.useSMTPAuth' | translate}}
</mat-slide-toggle>
</div>
</div> </div>
<div class="form-group" <div class="form-group"
[style.opacity]="['smtp', 'mail'].indexOf(sendmail.type) > -1 ? '1' : '0.5'"> [style.opacity]="['smtp', 'mail'].indexOf(sendmail.type) > -1 ? '1' : '0.5'">
......
...@@ -80,7 +80,8 @@ export class SendmailAdministrationComponent implements OnInit { ...@@ -80,7 +80,8 @@ export class SendmailAdministrationComponent implements OnInit {
recipientTest: string = ''; recipientTest: string = '';
passwordLabel: string = ''; passwordLabel: string = '';
useSMTPAuth: boolean = false;
constructor( constructor(
public translate: TranslateService, public translate: TranslateService,
public http: HttpClient, public http: HttpClient,
......
...@@ -2688,5 +2688,7 @@ ...@@ -2688,5 +2688,7 @@
"ixbus": "Ixbus", "ixbus": "Ixbus",
"iParapheur": "IParapheur", "iParapheur": "IParapheur",
"xParaph": "XParaph", "xParaph": "XParaph",
"fastParapheurWorkflow": "Circuit de visa Fast Parapheur" "fastParapheurWorkflow": "Circuit de visa Fast Parapheur",
"useSMTPAuth": "Utiliser l'adresse d'envoi configurée pour tous les envois de courriels",
"useSMTPAuthDesc": "Permet d'utiliser l'adresse d'envoi configurée pour toutes les emails sortante"
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment