File Coverage

lib/Sisimai/Rhost/GoogleApps.pm
Criterion Covered Total %
statement 30 30 100.0
branch 10 16 62.5
condition 2 2 100.0
subroutine 4 4 100.0
pod 0 1 0.0
total 46 53 86.7


line stmt bran cond sub pod time code
1             package Sisimai::Rhost::GoogleApps;
2 7     7   1631 use feature ':5.10';
  7         13  
  7         675  
3 7     7   53 use strict;
  7         14  
  7         166  
4 7     7   32 use warnings;
  7         11  
  7         4497  
5              
6             sub get {
7             # Detect bounce reason from Google Workspace
8             # @param [Sisimai::Data] argvs Parsed email object
9             # @return [String] The bounce reason for Google Workspace
10             # @see https://support.google.com/a/answer/3726730?hl=en
11 82     82 0 1616 my $class = shift;
12 82   100     273 my $argvs = shift // return undef;
13              
14 81 50       273 return $argvs->reason if $argvs->reason;
15 81 50       603 return '' unless $argvs->replycode;
16 81 50       613 return '' unless $argvs->diagnosticcode;
17 81 50       521 return '' unless $argvs->deliverystatus;
18 81 50       465 return '' unless $argvs->deliverystatus =~ /\A[245][.]\d[.]\d+\z/;
19              
20 81         1038 state $messagesof = {
21             'blocked' => [
22             ['421', '4.7.0', 'ip not in whitelist for rcpt domain, closing connection.'],
23              
24             # - 421 4.7.0 Our system has detected an unusual rate of unsolicited mail originating
25             # from your IP address. To protect our users from spam, mail sent from your IP ad-
26             # dress has been temporarily blocked.
27             # For more information, visit https://support.google.com/mail/answer/81126
28             ['421', '4.7.0', 'our system has detected an unusual rate of unsolicited mail originating from your ip address'],
29              
30             # - 421 4.7.0 Try again later, closing connection. This usually indicates a Denial of
31             # Service (DoS) for the SMTP relay at the HELO stage.
32             ['421', '4.7.0', 'try again later, closing connection.'],
33              
34             # - 501 5.5.4 HELO/EHLO argument is invalid. For more information, visit [HELO/EHLO e-
35             # mail error].
36             ['501', '5.5.4', 'helo/ehlo argument is invalid'],
37              
38             # - 550 5.7.1 Our system has detected an unusual rate of unsolicited mail originating
39             # from your IP address. To protect our users from spam, mail sent from your IP ad-
40             # dress has been blocked. Review https://support.google.com/mail/answer/81126
41             ['550', '5.7.1', 'our system has detected an unusual rate of unsolicited mail originating from your ip address'],
42              
43             # - 550 5.7.1 The IP you're using to send mail is not authorized to send email directly
44             # to our servers. Please use the SMTP relay at your service provider instead. For
45             # more information, visit https://support.google.com/mail/answer/10336
46             ['550', '5.7.1', "the ip you're using to send mail is not authorized to send email directly to our servers"],
47              
48             # - 550 5.7.25 The IP address sending this message does not have a PTR record setup, or
49             # the corresponding forward DNS entry does not point to the sending IP. As a policy,
50             # Gmail does not accept messages from IPs with missing PTR records.
51             ['550', '5.7.25', 'the ip address sending this message does not have a ptr record setup'],
52              
53             # - 550 5.7.26 Unauthenticated email from domain-name is not accepted due to domain's
54             # DMARC policy. Please contact the administrator of domain-name domain. If this was
55             # a legitimate mail please visit [Control unauthenticated mail from your domain] to
56             # learn about the DMARC initiative. If the messages are valid and aren't spam, con-
57             # tact the administrator of the receiving mail server to determine why your outgoing
58             # messages don't pass authentication checks.
59             ['550', '5.7.26', "is not accepted due to domain's dmarc policy"],
60              
61             # - 550 5.7.26 This message does not have authentication information or fails to pass
62             # authentication checks (SPF or DKIM). To best protect our users from spam, the mes-
63             # sage has been blocked. Please visit https://support.google.com/mail/answer/81126
64             # for more information.
65             ['550', '5.7.1', 'fails to pass authentication checks'],
66             ['550', '5.7.26', 'fails to pass authentication checks'],
67              
68             # - 550 5.7.26 This message fails to pass SPF checks for an SPF record with a hard fail
69             # policy (-all). To best protect our users from spam and phishing, the message has
70             # been blocked. Please visit https://support.google.com/mail/answer/81126 for more
71             # information.
72             ['550', '5.7.26', 'this message fails to pass spf checks for an spf record with a hard fail'],
73             # - 550 5.7.1 Our system has detected that this message is likely suspicious due to the
74             # very low reputation of the sending IP address. To best protect our users from spam,
75             # the message has been blocked.
76             # Please visit https://support.google.com/mail/answer/188131 for more information.
77             ['550', '5.7.1', 'this message is likely suspicious due to the very low reputation of the sending ip address'],
78             ],
79             'contenterror' => [
80             ['554', '5.6.0', 'mail message is malformed. Not accepted'],
81             ],
82             'exceedlimit' => [
83             # - 552 5.2.3 Your message exceeded Google's message size limits. For more information,
84             # visit https://support.google.com/mail/answer/6584
85             ['552', '5.2.3', "your message exceeded Google's message size limits"],
86             ],
87             'expired' => [
88             ['451', '4.4.2', 'timeout - closing connection'],
89             ],
90             'mailboxfull' => [
91             # - 452 4.2.2 The email account that you tried to reach is over quota. Please direct
92             # the recipient to Clear Google Drive space & increase storage.
93             ['452', '4.2.2', 'the email account that you tried to reach is over quota'],
94             ['552', '5.2.2', 'the email account that you tried to reach is over quota'],
95             ['550', '5.7.1', 'email quota exceeded'],
96             ],
97             'networkerror' => [
98             ['554', '5.6.0', 'message exceeded 50 hops, this may indicate a mail loop'],
99             ],
100             'norelaying' => [
101             ['550', '5.7.0', 'mail relay denied'],
102             ],
103             'policyviolation' => [
104             ['550', '5.7.1', 'messages with multiple addresses in from: header are not accepted'],
105              
106             # - 550 5.7.1 The user or domain that you are sending to (or from) has a policy that
107             # prohibited the mail that you sent. Please contact your domain administrator for
108             # further details.
109             # For more information, visit https://support.google.com/a/answer/172179
110             ['550', '5.7.1', 'the user or domain that you are sending to (or from) has a policy that prohibited'],
111              
112             # - 552 5.7.0 Our system detected an illegal attachment on your message. Please visit
113             # http://mail.google.com/support/bin/answer.py?answer=6590 to review our attachment
114             # guidelines.
115             ['552', '5.7.0', 'our system detected an illegal attachment on your message'],
116              
117             # - 552 5.7.0 This message was blocked because its content presents a potential securi-
118             # ty issue. Please visit https://support.google.com/mail/?p=BlockedMessage to review
119             # our message content and attachment content guidelines.
120             ['552', '5.7.0', 'this message was blocked because its content presents a potential security issue'],
121             ],
122             'rejected' => [
123             # - 550 5.7.0, Mail Sending denied. This error occurs if the sender account is disabled
124             # or not registered within your Google Workspace domain.
125             ['550', '5.7.0', 'mail sending denied'],
126              
127             ['550', '5.7.1', 'unauthenticated email is not accepted from this domain'],
128             ],
129             'securityerror' => [
130             ['421', '4.7.0', 'tls required for rcpt domain, closing connection'],
131             ['501', '5.5.2', 'cannot decode response'], # 2FA related error, maybe.
132              
133             # - 530 5.5.1 Authentication Required. For more information, visit
134             # https://support.google.com/accounts/troubleshooter/2402620
135             ['530', '5.5.1', 'authentication required.'],
136              
137             # - 535 5.7.1 Application-specific password required.
138             # For more information, visit https://support.google.com/accounts/answer/185833
139             ['535', '5.7.1', 'application-specific password required'],
140              
141             # - 535 5.7.1 Please log in with your web browser and then try again. For more infor-
142             # mation, visit https://support.google.com/mail/bin/accounts/answer/78754
143             ['535', '5.7.1', 'please log in with your web browser and then try again'],
144              
145             # - 535 5.7.1 Username and Password not accepted. For more information, visit
146             # https://support.google.com/accounts/troubleshooter/2402620
147             ['535', '5.7.1', 'username and password not accepted'],
148              
149             ['550', '5.7.1', 'invalid credentials for relay'],
150             ],
151             'spamdetected' => [
152             # - 550 5.7.1 Our system has detected that this message is likely unsolicited mail. To
153             # reduce the amount of spam sent to Gmail, this message has been blocked.
154             # For more information, visit https://support.google.com/mail/answer/188131
155             ['550', '5.7.1', 'our system has detected that this message is likely unsolicited mail'],
156             ],
157             'suspend' => [
158             [550, '5.2.1', 'the email account that you tried to reach is disabled'],
159             ],
160             'syntaxerror' => [
161             ['451', '4.5.0', 'smtp protocol violation, visit rfc 2821'],
162             ['454', '4.5.0', 'smtp protocol violation, no commands allowed to pipeline after starttls'],
163             ['454', '5.5.1', 'starttls may not be repeated'],
164             ['502', '5.5.1', 'too many unrecognized commands, goodbye'],
165             ['502', '5.5.1', 'unimplemented command'],
166             ['502', '5.5.1', 'unrecognized command'],
167             ['503', '5.5.1', 'ehlo/helo first'],
168             ['503', '5.5.1', 'mail first'],
169             ['503', '5.5.1', 'rcpt first'],
170             ['503', '5.7.0', 'no identity changes permitted'],
171             ['504', '5.7.4', 'unrecognized authentication type'],
172             ['530', '5.7.0', 'must issue a starttls command first'],
173             ['535', '5.5.4', 'optional argument not permitted for that auth mode'],
174             ['554', '5.7.0', 'too many unauthenticated commands'],
175             ['555', '5.5.2', 'syntax error'],
176             ],
177             'systemerror' => [
178             ['421', '4.3.0', 'temporary system problem'],
179             ['421', '4.7.0', 'temporary system problem'],
180             ['421', '4.4.5', 'server busy'],
181             ['451', '4.3.0', 'mail server temporarily rejected message'],
182             ['454', '4.7.0', 'cannot authenticate due to temporary system problem'],
183              
184             # - 452 4.5.3 Domain policy size per transaction exceeded, please try this recipient in
185             # a separate transaction. This message means the email policy size (size of policies,
186             # number of policies, or both) for the recipient domain has been exceeded.
187             ['452', '4.5.3', 'domain policy size per transaction exceeded'],
188             ],
189             'toomanyconn' => [
190             ['451', '4.3.0', 'multiple destination domains per transaction is unsupported'],
191              
192             # - 452 4.5.3 Your message has too many recipients. For more information regarding
193             # Google's sending limits, visit https://support.google.com/mail/answer/6592
194             ['452', '4.5.3', 'your message has too many recipients'],
195              
196             # - 450 4.2.1 The user you are trying to contact is receiving mail too quickly. Please
197             # resend your message at a later time. If the user is able to receive mail at that
198             # time, your message will be delivered.
199             # For more information, visit https://support.google.com/mail/answer/22839
200             ['450', '4.2.1', 'is receiving mail too quickly'],
201              
202             # - 450 4.2.1 The user you are trying to contact is receiving mail at a rate that pre-
203             # vents additional messages from being delivered. Please resend your message at a
204             # later time. If the user is able to receive mail at that time, your message will be
205             # delivered. For more information, visit https://support.google.com/mail/answer/6592
206             ['450', '4.2.1', 'is receiving mail at a rate that prevents additional messages from being delivered'],
207             ['550', '5.2.1', 'is receiving mail at a rate that prevents additional messages from being delivered'],
208              
209             # - 450 4.2.1 Peak SMTP relay limit exceeded for customer. This is a temporary error.
210             # For more information on SMTP relay limits, please contact your administrator or
211             # visit https://support.google.com/a/answer/6140680
212             ['450', '4.2.1', 'peak smtp relay limit exceeded for customer'],
213              
214             # - 550 5.4.5 Daily SMTP relay limit exceeded for user. For more information on SMTP
215             # relay sending limits please contact your administrator or visit SMTP relay service
216             # error messages.
217             ['550', '5.4.5', 'daily smtp relay limit exceeded for user'],
218             ['550', '5.4.5', 'daily sending quota exceeded'],
219              
220             # - 550 5.7.1 Daily SMTP relay limit exceeded for customer. For more information on
221             # SMTP relay sending limits please contact your administrator or visit
222             # https://support.google.com/a/answer/6140680
223             ['550', '5.7.1', 'daily smtp relay limit exceeded for customer'],
224             ],
225             'userunknown' => [
226             # - 550 5.1.1 The email account that you tried to reach does not exist. Please try dou-
227             # ble-checking the recipient's email address for typos or unnecessary spaces.
228             # For more information, visit https://support.google.com/mail/answer/6596
229             ['550', '5.1.1', 'the email account that you tried to reach does not exist'],
230              
231             # - 553 5.1.2 We weren't able to find the recipient domain. Please check for any spell-
232             # ing errors, and make sure you didn't enter any spaces, periods, or other punctua-
233             # tion after the recipient's email address.
234             ['553', '5.1.2', "we weren't able to find the recipient domain"],
235             ],
236             };
237              
238 81         370 my $statuscode = substr($argvs->deliverystatus, 2); # 421 => 21
239 81         548 my $esmtpreply = substr($argvs->replycode, 1, 2); # 5.7.1 => 7.1
240 81         469 my $esmtperror = lc $argvs->diagnosticcode;
241 81         424 my $reasontext = '';
242              
243 81         548 REASON: for my $e ( keys %$messagesof ) {
244             # Each key is a reason name
245 508         552 for my $f ( @{ $messagesof->{ $e } } ) {
  508         829  
246             # Try to match an SMTP reply code, a D.S.N., and an error message
247 2016 100       5299 next unless index($esmtperror, $f->[2]) > -1;
248 81 50       263 next unless index($f->[0], $esmtpreply) > 0;
249 81 100       267 next unless index($f->[1], $statuscode) > 1;
250 76         115 $reasontext = $e;
251 76         164 last REASON;
252             }
253             }
254 81         356 return $reasontext;
255             }
256              
257             1;
258             __END__