mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-22 09:59:06 +01:00
Improve test_send_mail CLI command
This commit is contained in:
parent
dc8e08b5d1
commit
34396a5fe1
1 changed files with 90 additions and 20 deletions
|
@ -69,11 +69,23 @@ function LSaddon_mail_support() {
|
||||||
'test_send_mail',
|
'test_send_mail',
|
||||||
'cli_test_send_mail',
|
'cli_test_send_mail',
|
||||||
'Send a test email',
|
'Send a test email',
|
||||||
"[-s subject] [-b body] [recipient]",
|
"[-s subject] [-b body] [-H] [recipient1] [...]",
|
||||||
array (
|
array (
|
||||||
" -s/--subject The test email subject (optional)",
|
" -s/--subject The test email subject (optional)",
|
||||||
" -b/--body The test email body (optional)",
|
" -b/--body The test email body (optional)",
|
||||||
" recipient The test email recipient (required)",
|
" -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
|
false, // This command does not need LDAP connection
|
||||||
'cli_test_send_mail_autocompleter'
|
'cli_test_send_mail_autocompleter'
|
||||||
|
@ -215,9 +227,12 @@ if (php_sapi_name() != 'cli')
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function cli_test_send_mail($command_args) {
|
function cli_test_send_mail($command_args) {
|
||||||
$recipient = null;
|
$recipients = array();
|
||||||
$subject = "Test email";
|
$subject = "Test email";
|
||||||
$body = "This is a test message.";
|
$body = "This is a test message.";
|
||||||
|
$html = false;
|
||||||
|
$headers = array();
|
||||||
|
$attachments = array();
|
||||||
for ($i=0; $i < count($command_args); $i++) {
|
for ($i=0; $i < count($command_args); $i++) {
|
||||||
switch ($command_args[$i]) {
|
switch ($command_args[$i]) {
|
||||||
case '--subject':
|
case '--subject':
|
||||||
|
@ -240,23 +255,68 @@ function cli_test_send_mail($command_args) {
|
||||||
LScli :: usage("Invalid body provided.");
|
LScli :: usage("Invalid body provided.");
|
||||||
break;
|
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:
|
default:
|
||||||
if (!$recipient && checkEmail($command_args[$i]))
|
if (checkEmail($command_args[$i]))
|
||||||
$recipient = $command_args[$i];
|
$recipients[] = $command_args[$i];
|
||||||
else
|
else
|
||||||
LScli :: usage("Invalid parameter '".$command_args[$i]."'.");
|
LScli :: usage("Invalid parameter '".$command_args[$i]."'.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$recipient)
|
if (!$recipients)
|
||||||
LScli :: usage("You must provide test email recipient as first positional parameter");
|
LScli :: usage("You must provide as least one email recipient as positional parameter");
|
||||||
|
|
||||||
$logger = LSlog :: get_logger('LSaddon_mail');
|
$logger = LSlog :: get_logger('LSaddon_mail');
|
||||||
if (!sendMail($recipient, $subject, $body)) {
|
if (!sendMail($recipients, $subject, $body, $headers, $attachments, null, null, $html)) {
|
||||||
$logger -> error("Fail to send test email sent to '$recipient'.");
|
$logger -> error("Fail to send test email sent to '".implode(', ', $recipients)."'.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$logger -> info("Test email sent to '$recipient'.");
|
$logger -> info("Test email sent to '".implode(', ', $recipients)."'.");
|
||||||
return true;
|
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
|
* @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) {
|
function cli_test_send_mail_autocompleter($comp_words, $comp_word_num, $comp_word, $opts) {
|
||||||
switch ($comp_words[$comp_word_num-1]) {
|
if (isset($comp_words[$comp_word_num-1]))
|
||||||
case '-s':
|
switch ($comp_words[$comp_word_num-1]) {
|
||||||
case '--subject':
|
case '-s':
|
||||||
case '-b':
|
case '--subject':
|
||||||
case '--body':
|
case '-b':
|
||||||
return array();
|
case '--body':
|
||||||
break;
|
case '--header':
|
||||||
}
|
case '-a':
|
||||||
|
case '--attachment':
|
||||||
|
case '--bcc':
|
||||||
|
case '--cc':
|
||||||
|
return array();
|
||||||
|
break;
|
||||||
|
}
|
||||||
$opts = array_merge(
|
$opts = array_merge(
|
||||||
$opts,
|
$opts,
|
||||||
array (
|
array (
|
||||||
'-s', '--subject',
|
'-s', '--subject',
|
||||||
'-b', '--body',
|
'-b', '--body',
|
||||||
|
'-H', '--html',
|
||||||
|
'--header',
|
||||||
|
'-a', '--attachment',
|
||||||
|
'--bcc', '--cc',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
return LScli :: autocomplete_opts($opts, $comp_word);
|
return LScli :: autocomplete_opts($opts, $comp_word);
|
||||||
|
|
Loading…
Reference in a new issue