2
2
3
3
namespace App \Integrations \Handlers ;
4
4
5
- use App \Rules \ OneEmailPerLine ;
5
+ use App \Notifications \ Forms \ FormEmailNotification ;
6
6
use Illuminate \Support \Facades \Log ;
7
7
use Illuminate \Support \Facades \Notification ;
8
- use App \Notifications \Forms \FormSubmissionNotification ;
8
+ use App \Open \MentionParser ;
9
+ use App \Service \Forms \FormSubmissionFormatter ;
9
10
10
11
class EmailIntegration extends AbstractEmailIntegrationHandler
11
12
{
13
+ public const RISKY_USERS_LIMIT = 120 ;
14
+
12
15
public static function getValidationRules (): array
13
16
{
14
17
return [
15
- 'notification_emails ' => ['required ' , new OneEmailPerLine ()],
16
- 'notification_reply_to ' => 'email|nullable ' ,
18
+ 'send_to ' => 'required ' ,
19
+ 'sender_name ' => 'required ' ,
20
+ 'sender_email ' => 'email|nullable ' ,
21
+ 'subject ' => 'required ' ,
22
+ 'email_content ' => 'required ' ,
23
+ 'include_submission_data ' => 'boolean ' ,
24
+ 'include_hidden_fields_submission_data ' => ['nullable ' , 'boolean ' ],
25
+ 'reply_to ' => 'nullable ' ,
17
26
];
18
27
}
19
28
20
29
protected function shouldRun (): bool
21
30
{
22
- return $ this ->integrationData ->notification_emails && parent ::shouldRun ();
31
+ return $ this ->integrationData ->send_to && parent ::shouldRun () && !$ this ->riskLimitReached ();
32
+ }
33
+
34
+ // To avoid phishing abuse we limit this feature for risky users
35
+ private function riskLimitReached (): bool
36
+ {
37
+ // This is a per-workspace limit for risky workspaces
38
+ if ($ this ->form ->workspace ->is_risky ) {
39
+ if ($ this ->form ->workspace ->submissions_count >= self ::RISKY_USERS_LIMIT ) {
40
+ Log::error ('!!!DANGER!!! Dangerous user detected! Attempting many email sending. ' , [
41
+ 'form_id ' => $ this ->form ->id ,
42
+ 'workspace_id ' => $ this ->form ->workspace ->id ,
43
+ ]);
44
+
45
+ return true ;
46
+ }
47
+ }
48
+
49
+ return false ;
23
50
}
24
51
25
52
public function handle (): void
@@ -28,19 +55,27 @@ public function handle(): void
28
55
return ;
29
56
}
30
57
31
- $ subscribers = collect (preg_split ("/ \r\n| \n| \r/ " , $ this ->integrationData ->notification_emails ))
58
+ if ($ this ->form ->is_pro ) { // For Send to field Mentions are Pro feature
59
+ $ formatter = (new FormSubmissionFormatter ($ this ->form , $ this ->submissionData ))->outputStringsOnly ();
60
+ $ parser = new MentionParser ($ this ->integrationData ->send_to , $ formatter ->getFieldsWithValue ());
61
+ $ sendTo = $ parser ->parse ();
62
+ } else {
63
+ $ sendTo = $ this ->integrationData ->send_to ;
64
+ }
65
+
66
+ $ recipients = collect (preg_split ("/ \r\n| \n| \r/ " , $ sendTo ))
32
67
->filter (function ($ email ) {
33
68
return filter_var ($ email , FILTER_VALIDATE_EMAIL );
34
69
});
35
70
Log::debug ('Sending email notification ' , [
36
- 'recipients ' => $ subscribers ->toArray (),
71
+ 'recipients ' => $ recipients ->toArray (),
37
72
'form_id ' => $ this ->form ->id ,
38
73
'form_slug ' => $ this ->form ->slug ,
39
74
'mailer ' => $ this ->mailer
40
75
]);
41
- $ subscribers ->each (function ($ subscriber ) {
76
+ $ recipients ->each (function ($ subscriber ) {
42
77
Notification::route ('mail ' , $ subscriber )->notify (
43
- new FormSubmissionNotification ($ this ->event , $ this ->integrationData , $ this ->mailer )
78
+ new FormEmailNotification ($ this ->event , $ this ->integrationData , $ this ->mailer )
44
79
);
45
80
});
46
81
}
0 commit comments