1010 */
1111namespace WebFiori \Framework \Cli \Commands ;
1212
13+ use WebFiori \Cli \Argument ;
1314use WebFiori \Cli \Command ;
1415use WebFiori \Mail \Exceptions \SMTPException ;
1516use WebFiori \Mail \SMTPAccount ;
2425 */
2526class AddSmtpConnectionCommand extends Command {
2627 public function __construct () {
27- parent ::__construct ('add:smtp-connection ' , [], 'Add an SMTP account. ' );
28+ parent ::__construct ('add:smtp-connection ' , [
29+ new Argument ('--host ' , 'The address of SMTP server host. ' , true ),
30+ new Argument ('--port ' , 'Port number of the SMTP server. ' , true ),
31+ new Argument ('--user ' , 'The username to use when connecting to the server. ' , true ),
32+ new Argument ('--password ' , 'The password to use when connecting to the server. ' , true ),
33+ new Argument ('--sender-address ' , 'The email address that will appear as sender. ' , true ),
34+ new Argument ('--sender-name ' , 'The name that will appear as sender. ' , true ),
35+ new Argument ('--name ' , 'A friendly name to identify the connection. ' , true ),
36+ new Argument ('--oauth-token ' , 'OAuth access token to use for authentication instead of password. ' , true ),
37+ new Argument ('--no-check ' , 'If provided, the connection will be added without the attempt to check if provided credentials are valid. ' , true ),
38+ ], 'Add an SMTP account. ' );
2839 }
2940 /**
3041 * Execute the command.
@@ -33,9 +44,12 @@ public function __construct() {
3344 */
3445 public function exec () : int {
3546 $ smtpConn = new SMTPAccount ();
36- $ smtpConn ->setServerAddress ($ this ->getInput ('SMTP Server address: ' , '127.0.0.1 ' ));
47+
48+ $ hostArg = $ this ->getArgValue ('--host ' );
49+ $ smtpConn ->setServerAddress ($ hostArg !== null ? $ hostArg : $ this ->getInput ('SMTP Server address: ' , '127.0.0.1 ' ));
50+
3751 $ smtpConn ->setPort (25 );
38- $ addr = $ smtpConn ->getAddress ();
52+ $ addr = $ smtpConn ->getServerAddress ();
3953
4054 if ($ addr == 'smtp.outlook.com '
4155 || $ addr == 'outlook.office365.com '
@@ -45,12 +59,39 @@ public function exec() : int {
4559 || $ addr == 'smtp.mail.yahoo.com ' ) {
4660 $ smtpConn ->setPort (465 );
4761 }
48- $ smtpConn ->setPort ($ this ->getInput ('Port number: ' , $ smtpConn ->getPort ()));
49- $ smtpConn ->setUsername ($ this ->getInput ('Username: ' ));
50- $ smtpConn ->setPassword ($ this ->getMaskedInput ('Password: ' ));
51- $ smtpConn ->setAddress ($ this ->getInput ('Sender email address: ' , $ smtpConn ->getUsername ()));
52- $ smtpConn ->setSenderName ($ this ->getInput ('Sender name: ' , $ smtpConn ->getAddress ()));
53- $ smtpConn ->setAccountName ($ this ->getInput ('Give your connection a friendly name: ' , 'smtp-connection- ' .count (App::getConfig ()->getSMTPConnections ())));
62+
63+ $ portArg = $ this ->getArgValue ('--port ' );
64+ $ smtpConn ->setPort ($ portArg !== null ? (int ) $ portArg : $ this ->getInput ('Port number: ' , $ smtpConn ->getPort ()));
65+
66+ $ userArg = $ this ->getArgValue ('--user ' );
67+ $ smtpConn ->setUsername ($ userArg !== null ? $ userArg : $ this ->getInput ('Username: ' ));
68+
69+ $ passArg = $ this ->getArgValue ('--password ' );
70+ $ smtpConn ->setPassword ($ passArg !== null ? $ passArg : $ this ->getMaskedInput ('Password: ' ));
71+
72+ $ senderAddrArg = $ this ->getArgValue ('--sender-address ' );
73+ $ smtpConn ->setAddress ($ senderAddrArg !== null ? $ senderAddrArg : $ this ->getInput ('Sender email address: ' , $ smtpConn ->getUsername ()));
74+
75+ $ senderNameArg = $ this ->getArgValue ('--sender-name ' );
76+ $ smtpConn ->setSenderName ($ senderNameArg !== null ? $ senderNameArg : $ this ->getInput ('Sender name: ' , $ smtpConn ->getAddress ()));
77+
78+ $ defaultName = 'smtp-connection- ' .count (App::getConfig ()->getSMTPConnections ());
79+ $ nameArg = $ this ->getArgValue ('--name ' );
80+ $ smtpConn ->setAccountName ($ nameArg !== null ? $ nameArg : $ this ->getInput ('Give your connection a friendly name: ' , $ defaultName ));
81+
82+ $ tokenArg = $ this ->getArgValue ('--oauth-token ' );
83+
84+ if ($ tokenArg !== null ) {
85+ $ smtpConn ->setAccessToken ($ tokenArg );
86+ }
87+
88+ if ($ this ->isArgProvided ('--no-check ' )) {
89+ App::getConfig ()->addOrUpdateSMTPAccount ($ smtpConn );
90+ $ this ->success ('Connection information was stored in application configuration. ' );
91+
92+ return 0 ;
93+ }
94+
5495 $ this ->println ('Trying to connect. This can take up to 1 minute... ' );
5596 $ server = new SMTPServer ($ smtpConn ->getServerAddress (), $ smtpConn ->getPort ());
5697
0 commit comments