1010
1111use OCP \Defaults ;
1212use OCP \IConfig ;
13+ use OCP \IL10N ;
1314use OCP \IURLGenerator ;
1415use OCP \IUserManager ;
1516use OCP \IUserSession ;
@@ -40,53 +41,83 @@ public function __construct(
4041 * @param $uid
4142 * @throws \Exception
4243 */
43- public function sendGuestInviteMail (string $ uid , string $ shareWith , Share \ IShare $ share , string $ token , string $ language = '' ): void {
44+ public function sendGuestInviteMail (string $ uid , string $ guest , string $ token , string $ language = '' , ? Share \ IShare $ share = null ): void {
4445 if ($ language === '' ) {
4546 $ language = null ;
4647 }
4748 $ l10n = $ this ->l10nFactory ->get ('guests ' , $ language );
4849
4950 $ passwordLink = $ this ->urlGenerator ->linkToRouteAbsolute (
5051 'core.lost.resetform ' ,
51- ['userId ' => $ shareWith , 'token ' => $ token ]
52+ ['userId ' => $ guest , 'token ' => $ token ]
5253 );
5354
54- $ this ->logger ->debug ("sending invite to $ shareWith: $ passwordLink " , ['app ' => 'guests ' ]);
55-
56- $ targetUser = $ this ->userManager ->get ($ shareWith );
57- $ shareWithEmail = $ targetUser ->getEMailAddress ();
58- if (!$ shareWithEmail ) {
55+ $ targetUser = $ this ->userManager ->get ($ guest );
56+ $ guestEmail = $ targetUser ->getEMailAddress ();
57+ if (!$ guestEmail ) {
5958 throw new \Exception ('Guest user created without email ' );
6059 }
6160 $ replyTo = $ this ->userManager ->get ($ uid )->getEMailAddress ();
6261 $ senderDisplayName = $ this ->userSession ->getUser ()->getDisplayName ();
6362
63+ if (empty ($ share )) {
64+ [ $ subject , $ emailTemplate ] = $ this ->composeInviteMessage ($ senderDisplayName , $ guestEmail , $ passwordLink , $ l10n );
65+ } else {
66+ [ $ subject , $ emailTemplate ] = $ this ->composeShareMessage ($ share , $ senderDisplayName , $ guestEmail , $ passwordLink , $ l10n );
67+ }
68+
69+ try {
70+ $ message = $ this ->mailer ->createMessage ();
71+ $ message ->setTo ([$ guestEmail => $ targetUser ->getDisplayName ()]);
72+ $ message ->setSubject ($ subject );
73+ $ message ->setHtmlBody ($ emailTemplate ->renderHtml ());
74+ $ message ->setPlainBody ($ emailTemplate ->renderText ());
75+ $ message ->setFrom ([
76+ Util::getDefaultEmailAddress ('sharing-noreply ' ) =>
77+ $ l10n ->t ('%s via %s ' , [
78+ $ senderDisplayName ,
79+ $ this ->defaults ->getName ()
80+ ]),
81+ ]);
82+
83+ if (!is_null ($ replyTo )) {
84+ $ message ->setReplyTo ([$ replyTo ]);
85+ }
86+
87+ $ this ->mailer ->send ($ message );
88+ } catch (\Exception $ e ) {
89+ throw new \Exception ($ l10n ->t (
90+ 'Couldn \'t send reset email. Please contact your administrator. '
91+ ));
92+ }
93+ }
94+
95+ private function composeShareMessage (Share \IShare $ share , string $ senderDisplayName , string $ guestEmail , string $ passwordLink , IL10N $ l10n ): array {
6496 $ filename = trim ($ share ->getTarget (), '/ ' );
65- $ subject = $ l10n ->t ('%s shared »%s« with you ' , [$ senderDisplayName, $ filename ]);
97+ $ subject = $ l10n ->t ('%s shared a file with you ' , [$ senderDisplayName ]);
6698 $ expiration = $ share ->getExpirationDate ();
6799
68100 $ link = $ this ->urlGenerator ->linkToRouteAbsolute (
69101 'files.viewcontroller.showFile ' , ['fileid ' => $ share ->getNodeId (), 'direct ' => 1 ]
70102 );
71-
72- $ emailTemplate = $ this ->mailer ->createEMailTemplate ('guest.invite ' );
103+ $ emailTemplate = $ this ->mailer ->createEMailTemplate ('guest.share ' );
73104
74105 $ emailTemplate ->addHeader ();
75- $ emailTemplate ->addHeading ($ l10n ->t ('Incoming share ' ));
106+ $ emailTemplate ->addHeading ($ l10n ->t ('%s shared a file with you ' , [ $ senderDisplayName ] ));
76107
77108 $ emailTemplate ->addBodyText (
78109 $ l10n ->t ('Hey there, ' )
79110 );
80111
81112 $ emailTemplate ->addBodyText (
82- $ l10n ->t ('%s just shared »%s« with you. ' , [$ senderDisplayName , $ filename ])
113+ $ l10n ->t ('%s just invited you and shared »%s« with you. ' , [$ senderDisplayName , $ filename ])
83114 );
84115
85116 $ emailTemplate ->addBodyText (
86117 $ l10n ->t ('You can access the shared file by activating your guest account. ' )
87118 );
88119 $ emailTemplate ->addBodyText (
89- $ l10n ->t ('After your account is activated you can view the share by logging in with %s. ' , [$ shareWithEmail ])
120+ $ l10n ->t ('After your account is activated you can view the share by logging in with %s. ' , [$ guestEmail ])
90121 );
91122
92123 if ($ expiration ) {
@@ -104,29 +135,38 @@ public function sendGuestInviteMail(string $uid, string $shareWith, Share\IShare
104135 );
105136 $ emailTemplate ->addFooter ();
106137
107- try {
108- $ message = $ this ->mailer ->createMessage ();
109- $ message ->setTo ([$ shareWithEmail => $ targetUser ->getDisplayName ()]);
110- $ message ->setSubject ($ subject );
111- $ message ->setHtmlBody ($ emailTemplate ->renderHtml ());
112- $ message ->setPlainBody ($ emailTemplate ->renderText ());
113- $ message ->setFrom ([
114- Util::getDefaultEmailAddress ('sharing-noreply ' ) =>
115- $ l10n ->t ('%s via %s ' , [
116- $ senderDisplayName ,
117- $ this ->defaults ->getName ()
118- ]),
119- ]);
138+ return [ $ subject , $ emailTemplate ];
139+ }
120140
121- if (!is_null ($ replyTo )) {
122- $ message ->setReplyTo ([$ replyTo ]);
123- }
141+ private function composeInviteMessage (string $ senderDisplayName , string $ guestEmail , string $ passwordLink , IL10N $ l10n ): array {
142+ $ subject = $ l10n ->t ('%s invited you as a guest ' , [$ senderDisplayName ]);
124143
125- $ this ->mailer ->send ($ message );
126- } catch (\Exception $ e ) {
127- throw new \Exception ($ l10n ->t (
128- 'Couldn \'t send reset email. Please contact your administrator. '
129- ));
130- }
144+ $ emailTemplate = $ this ->mailer ->createEMailTemplate ('guest.invite ' );
145+
146+ $ emailTemplate ->addHeader ();
147+ $ emailTemplate ->addHeading ($ l10n ->t ('You have been invited ' ));
148+
149+ $ emailTemplate ->addBodyText (
150+ $ l10n ->t ('Hey there, ' )
151+ );
152+
153+ $ emailTemplate ->addBodyText (
154+ $ l10n ->t ('%s just invited you. ' , [$ senderDisplayName ])
155+ );
156+
157+ $ emailTemplate ->addBodyText (
158+ $ l10n ->t ('You can activate your guest account with the button below. ' )
159+ );
160+ $ emailTemplate ->addBodyText (
161+ $ l10n ->t ('After your account is activated you can log in with %s. ' , [$ guestEmail ])
162+ );
163+
164+ $ emailTemplate ->addBodyButton (
165+ $ l10n ->t ('Activate account ' ),
166+ $ passwordLink
167+ );
168+ $ emailTemplate ->addFooter ();
169+
170+ return [ $ subject , $ emailTemplate ];
131171 }
132172}
0 commit comments