-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new option for password via environment - Closes #2
- Loading branch information
1 parent
003ecea
commit e9f5126
Showing
7 changed files
with
225 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ A simple command line application for sending mails. | |
[![Java Development Kit 11](https://img.shields.io/badge/JDK-11-green.svg)](https://openjdk.java.net/projects/jdk/11/) | ||
|
||
## Versions | ||
- 0.2.x = **Java 11** with new 'jakarta' namespace | ||
- > 0.2.x = **Java 11** with new 'jakarta' namespace | ||
- 0.1.0 = **Java 8** | ||
|
||
## Why? | ||
|
@@ -25,19 +25,20 @@ You can download the latest JAR file here: https://github.com/fuinorg/sjsm/relea | |
|
||
## Command line arguments | ||
|
||
| Argument | Value | Required | Example | | ||
| -------- |--------------------------------------------------------| -------- |--------------------------------------------------------------------------------------------------| | ||
| -host | SMTPS server name | yes | "smtp.no-where-no-no.com" | | ||
| -port | SMTPS port number (SSL/TLS) | yes | 465 | | ||
| -user | Your mailbox user | yes | "acc12345_from.not.exist" or "[email protected]" (depends on your mail provider) | | ||
| -pw | Your mailbox password | yes | "xxxxxxx" | | ||
| -from | Sender's email address | yes | "[email protected]" | | ||
| -to | Receiver's email address (multiple separated with ";") | yes | "[email protected]" or "[email protected];[email protected]" | | ||
| -subject | Mail subject | yes | "My subject" | | ||
| -message | Message body (TEXT or HTML) | yes | "<html><body><h1>This is a test mail</h1></body></html>" | | ||
| -html | - | no | - | | ||
| -charset | Mail encoding (defaults to "utf-8") | no | "utf-8" | | ||
| -important | Send High Priority Email (X-Priority flag) | no | - | | ||
| Argument | Value | Required | Example | | ||
|------------|---------------------------------------------------------------------|----------|--------------------------------------------------------------------------------------------------| | ||
| -host | SMTPS server name | yes | "smtp.no-where-no-no.com" | | ||
| -port | SMTPS port number (SSL/TLS) | yes | 465 | | ||
| -user | Your mailbox user | yes | "acc12345_from.not.exist" or "[email protected]" (depends on your mail provider) | | ||
| -pw | Your mailbox password (Either `pw`or `envPw` is mandatory) | no | - | | ||
| -envPw | Name of an environment variable that contains your mailbox password | no | - | | ||
| -from | Sender's email address | yes | "[email protected]" | | ||
| -to | Receiver's email address (multiple separated with ";") | yes | "[email protected]" or "[email protected];[email protected]" | | ||
| -subject | Mail subject | yes | "My subject" | | ||
| -message | Message body (TEXT or HTML) | yes | "<html><body><h1>This is a test mail</h1></body></html>" | | ||
| -html | - | no | - | | ||
| -charset | Mail encoding (defaults to "utf-8") | no | "utf-8" | | ||
| -important | Send High Priority Email (X-Priority flag) | no | - | | ||
|
||
## TEXT example | ||
|
||
|
@@ -65,7 +66,8 @@ You can download the latest JAR file here: https://github.com/fuinorg/sjsm/relea | |
-html \ | ||
|
||
## CAUTION | ||
<b>Be aware that passing your password via the command line will most probably be visible in your command line history.</b> | ||
:warning: Be aware that passing your password via the command line (`-pw`) will most probably be visible in your command line history. | ||
It's better to use an environment variable with `-envPw`. | ||
|
||
* * * | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.fuin.sjsm; | ||
|
||
import org.kohsuke.args4j.Localizable; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* Message constants. | ||
*/ | ||
public enum Messages implements Localizable { | ||
|
||
/** Neither 'pw' nor 'envPw' argument is set. */ | ||
MISSING_PASSWORD_OPTION, | ||
|
||
/** The environment variable from 'envPw' argument is not set. */ | ||
PASSWORD_ENV_VAR_NOT_SET; | ||
|
||
@Override | ||
public String formatWithLocale(final Locale locale, final Object... args) { | ||
final ResourceBundle localized = ResourceBundle.getBundle("sjsm-messages", locale); | ||
return MessageFormat.format(localized.getString(name()), args); | ||
} | ||
|
||
@Override | ||
public String format(final Object... args) { | ||
return formatWithLocale(Locale.getDefault(), args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MISSING_PASSWORD_OPTION=A password or an environment variable with the password is mandatory | ||
PASSWORD_ENV_VAR_NOT_SET=The environment variable {0} is not set (has no value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
/** | ||
* Copyright (C) 2015 Michael Schnell. All rights reserved. | ||
* Copyright (C) 2015 Michael Schnell. All rights reserved. | ||
* http://www.fuin.org/ | ||
* | ||
* <p> | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 3 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* <p> | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
* | ||
* <p> | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package org.fuin.sjsm; | ||
|
||
import static com.github.stefanbirkner.systemlambda.SystemLambda.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -122,4 +125,108 @@ void testSendMultipleReceiver() { | |
|
||
} | ||
|
||
@Test | ||
void testMissingPasswordOption() throws Exception { | ||
|
||
final String[] args = new String[]{ | ||
"-host", "localhost", | ||
"-port", "" + dumbster.getPort(), | ||
"-user", "myaccount", | ||
"-from", "[email protected]", | ||
"-to", "[email protected]", | ||
"-subject", "Whatever", | ||
"-message", "None" | ||
}; | ||
|
||
final AtomicLong exitCode = new AtomicLong(); | ||
final String systemErr = tapSystemErr(() -> { | ||
exitCode.set(catchSystemExit(() -> { | ||
SendMailApp.main(args); | ||
})); | ||
}); | ||
assertThat(systemErr).contains("A password or an environment variable with the password is mandatory"); | ||
assertThat(exitCode.get()).isEqualTo(1); | ||
|
||
} | ||
|
||
@Test | ||
void testPwOption() throws Exception { | ||
|
||
final String[] args = new String[]{ | ||
"-host", "localhost", | ||
"-port", "" + dumbster.getPort(), | ||
"-user", "myaccount", | ||
"-pw", "abc", | ||
"-from", "[email protected]", | ||
"-to", "[email protected]", | ||
"-subject", "Whatever", | ||
"-message", "None", | ||
"-smtp" | ||
}; | ||
|
||
final AtomicLong exitCode = new AtomicLong(); | ||
final String systemOut = tapSystemOut(() -> { | ||
exitCode.set(catchSystemExit(() -> { | ||
SendMailApp.main(args); | ||
})); | ||
}); | ||
assertThat(systemOut).contains("Successfully sent message 'Whatever' to '[email protected]"); | ||
assertThat(exitCode.get()).isEqualTo(0); | ||
|
||
} | ||
|
||
@Test | ||
void testEnvPwOption() throws Exception { | ||
|
||
final String[] args = new String[]{ | ||
"-host", "localhost", | ||
"-port", "" + dumbster.getPort(), | ||
"-user", "myaccount", | ||
"-envPw", "MAIL_PW_TEST", | ||
"-from", "[email protected]", | ||
"-to", "[email protected]", | ||
"-subject", "Whatever", | ||
"-message", "None", | ||
"-smtp" | ||
}; | ||
|
||
final AtomicLong exitCode = new AtomicLong(); | ||
final AtomicReference<String> systemOut = new AtomicReference<>(); | ||
withEnvironmentVariable("MAIL_PW_TEST", "abc").execute(() -> { | ||
systemOut.set(tapSystemOut(() -> { | ||
exitCode.set(catchSystemExit(() -> { | ||
SendMailApp.main(args); | ||
})); | ||
})); | ||
}); | ||
assertThat(systemOut.get()).contains("Successfully sent message 'Whatever' to '[email protected]"); | ||
assertThat(exitCode.get()).isEqualTo(0); | ||
|
||
} | ||
|
||
@Test | ||
void testMissingEnvPassword() throws Exception { | ||
|
||
final String[] args = new String[]{ | ||
"-host", "localhost", | ||
"-port", "" + dumbster.getPort(), | ||
"-user", "myaccount", | ||
"-envPw", "NOT_EXISTING_PW_ENV_VAR", | ||
"-from", "[email protected]", | ||
"-to", "[email protected]", | ||
"-subject", "Whatever", | ||
"-message", "None" | ||
}; | ||
|
||
final AtomicLong exitCode = new AtomicLong(); | ||
final String systemErr = tapSystemErr(() -> { | ||
exitCode.set(catchSystemExit(() -> { | ||
SendMailApp.main(args); | ||
})); | ||
}); | ||
assertThat(systemErr).contains("The environment variable NOT_EXISTING_PW_ENV_VAR is not set"); | ||
assertThat(exitCode.get()).isEqualTo(1); | ||
|
||
} | ||
|
||
} |