Improve test_send_mail CLI command

This commit is contained in:
Benjamin Renard 2024-02-19 19:28:21 +01:00
parent dc8e08b5d1
commit 34396a5fe1
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -69,11 +69,23 @@ function LSaddon_mail_support() {
'test_send_mail',
'cli_test_send_mail',
'Send a test email',
"[-s subject] [-b body] [recipient]",
"[-s subject] [-b body] [-H] [recipient1] [...]",
array (
" -s/--subject The test email subject (optional)",
" -b/--body The test email body (optional)",
" recipient The test email recipient (required)",
" -s/--subject The test email subject (optional)",
" -b/--body The test email body (optional)",
" -H/--HTML Enable HTML email body mode (optional)",
" --header Email header using format:",
" header_name=header_value",
" Multiple headers could be specified by using this",
" optional argument multiple time.",
" -a|--attachment Email attachment using format:",
" /path/to/attachment.file[:filename]",
" The filename is optional (default: using source filename).",
" Multiple attachments could be specified by using this",
" optional argument multiple time.",
" --bcc Add Blind Carbon Copy (BCC) recipient(s)",
" --cc Add Carbon Copy (CC) recipient(s)",
" recipients The test email recipient(s) (required).",
),
false, // This command does not need LDAP connection
'cli_test_send_mail_autocompleter'
@ -215,9 +227,12 @@ if (php_sapi_name() != 'cli')
* @return bool
*/
function cli_test_send_mail($command_args) {
$recipient = null;
$recipients = array();
$subject = "Test email";
$body = "This is a test message.";
$html = false;
$headers = array();
$attachments = array();
for ($i=0; $i < count($command_args); $i++) {
switch ($command_args[$i]) {
case '--subject':
@ -240,23 +255,68 @@ function cli_test_send_mail($command_args) {
LScli :: usage("Invalid body provided.");
break;
case '--html':
case '-H':
$html = true;
break;
case '--header':
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode('=', $command_args[$i]);
if (count($parts) != 2)
LScli :: usage('Invalid header string ('.$command_args[$i].').');
if (array_key_exists($parts[0], $headers))
LScli :: usage('Header "'.$parts[0].'" already specified.');
$headers[$parts[0]] = $parts[1];
break;
case '-a':
case '--attachment':
$i++;
LScli :: unquote_word($command_args[$i]);
$parts = explode(':', $command_args[$i]);
$path = $parts[0];
if (!is_file($path))
LScli :: usage('Invalid attachment "'.$command_args[$i].'": file not found.');
$attachments[$path] = count($parts) >= 2?$parts[1]:basename($path);
break;
case '--bcc':
$i++;
LScli :: unquote_word($command_args[$i]);
if (!checkEmail($command_args[$i]))
LScli :: usage('Invalid BCC recipient "'.$command_args[$i].'".');
$headers['BCC'] = isset($headers['BCC'])?ensureIsArray($headers['BCC']):[];
$headers['BCC'][] = $command_args[$i];
break;
case '--cc':
$i++;
LScli :: unquote_word($command_args[$i]);
if (!checkEmail($command_args[$i]))
LScli :: usage('Invalid CC recipient "'.$command_args[$i].'".');
$headers['CC'] = isset($headers['CC'])?ensureIsArray($headers['CC']):[];
$headers['CC'][] = $command_args[$i];
break;
default:
if (!$recipient && checkEmail($command_args[$i]))
$recipient = $command_args[$i];
if (checkEmail($command_args[$i]))
$recipients[] = $command_args[$i];
else
LScli :: usage("Invalid parameter '".$command_args[$i]."'.");
}
}
if (!$recipient)
LScli :: usage("You must provide test email recipient as first positional parameter");
if (!$recipients)
LScli :: usage("You must provide as least one email recipient as positional parameter");
$logger = LSlog :: get_logger('LSaddon_mail');
if (!sendMail($recipient, $subject, $body)) {
$logger -> error("Fail to send test email sent to '$recipient'.");
if (!sendMail($recipients, $subject, $body, $headers, $attachments, null, null, $html)) {
$logger -> error("Fail to send test email sent to '".implode(', ', $recipients)."'.");
return false;
}
$logger -> info("Test email sent to '$recipient'.");
$logger -> info("Test email sent to '".implode(', ', $recipients)."'.");
return true;
}
@ -271,19 +331,29 @@ function cli_test_send_mail($command_args) {
* @return array<string> List of available options for the word to autocomplete
**/
function cli_test_send_mail_autocompleter($comp_words, $comp_word_num, $comp_word, $opts) {
switch ($comp_words[$comp_word_num-1]) {
case '-s':
case '--subject':
case '-b':
case '--body':
return array();
break;
}
if (isset($comp_words[$comp_word_num-1]))
switch ($comp_words[$comp_word_num-1]) {
case '-s':
case '--subject':
case '-b':
case '--body':
case '--header':
case '-a':
case '--attachment':
case '--bcc':
case '--cc':
return array();
break;
}
$opts = array_merge(
$opts,
array (
'-s', '--subject',
'-b', '--body',
'-H', '--html',
'--header',
'-a', '--attachment',
'--bcc', '--cc',
)
);
return LScli :: autocomplete_opts($opts, $comp_word);