File Coverage

lib/Sisimai/Lhost/Gmail.pm
Criterion Covered Total %
statement 75 77 97.4
branch 31 38 81.5
condition 18 22 81.8
subroutine 6 6 100.0
pod 2 2 100.0
total 132 145 91.0


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Gmail;
2 38     38   4115 use parent 'Sisimai::Lhost';
  38         305  
  38         276  
3 38     38   2988 use v5.26;
  38         163  
4 38     38   203 use strict;
  38         72  
  38         1052  
5 38     38   182 use warnings;
  38         153  
  38         43941  
6              
7 1     1 1 5 sub description { 'Gmail: https://mail.google.com/' }
8             sub inquire {
9             # Detect an error from Gmail
10             # @param [Hash] mhead Message headers of a bounce email
11             # @param [String] mbody Message body of a bounce email
12             # @return [Hash] Bounce data list and message/rfc822 part
13             # @return [undef] failed to decode or the arguments are missing
14             # @since v4.0.0
15 1045     1045 1 3619 my $class = shift;
16 1045   100     3270 my $mhead = shift // return undef;
17 1044   100     3594 my $mbody = shift // return undef;
18              
19             # Google Mail
20             # From: Mail Delivery Subsystem
21             # Received: from vw-in-f109.1e100.net [74.125.113.109] by ...
22             #
23             # * Check the body part
24             # This is an automatically generated Delivery Status Notification
25             # Delivery to the following recipient failed permanently:
26             #
27             # recipient-address-here@example.jp
28             #
29             # Technical details of permanent failure:
30             # Google tried to deliver your message, but it was rejected by the
31             # recipient domain. We recommend contacting the other email provider
32             # for further information about the cause of this error. The error
33             # that the other server returned was:
34             # 550 550 : User unknown (state 14).
35             #
36             # -- OR --
37             # THIS IS A WARNING MESSAGE ONLY.
38             #
39             # YOU DO NOT NEED TO RESEND YOUR MESSAGE.
40             #
41             # Delivery to the following recipient has been delayed:
42             #
43             # mailboxfull@example.jp
44             #
45             # Message will be retried for 2 more day(s)
46             #
47             # Technical details of temporary failure:
48             # Google tried to deliver your message, but it was rejected by the recipient
49             # domain. We recommend contacting the other email provider for further infor-
50             # mation about the cause of this error. The error that the other server re-
51             # turned was: 450 450 4.2.2 ... Mailbox Full (state 14).
52             #
53             # -- OR --
54             #
55             # Delivery to the following recipient failed permanently:
56             #
57             # userunknown@example.jp
58             #
59             # Technical details of permanent failure:=20
60             # Google tried to deliver your message, but it was rejected by the server for=
61             # the recipient domain example.jp by mx.example.jp. [192.0.2.59].
62             #
63             # The error that the other server returned was:
64             # 550 5.1.1 ... User Unknown
65             #
66 1043 100       5477 return undef unless rindex($mhead->{'from'}, '') > -1;
67 312 50       1298 return undef unless index($mhead->{'subject'}, 'Delivery Status Notification') > -1;
68              
69 312         692 state $indicators = __PACKAGE__->INDICATORS;
70 312         654 state $boundaries = ['----- Original message -----', '----- Message header follows -----'];
71 312         541 state $startingof = {
72             'message' => ['Delivery to the following recipient'],
73             'error' => ['The error that the other server returned was:'],
74             };
75 312         570 state $messagesof = {
76             'expired' => [
77             'DNS Error: Could not contact DNS servers',
78             'Delivery to the following recipient has been delayed',
79             'The recipient server did not accept our requests to connect',
80             ],
81             'hostunknown' => [
82             'DNS Error: Domain name not found',
83             'DNS Error: DNS server returned answer with no data',
84             ],
85             };
86 312         722 state $statetable = {
87             # Technical details of permanent failure:
88             # Google tried to deliver your message, but it was rejected by the recipient domain.
89             # We recommend contacting the other email provider for further information about the
90             # cause of this error. The error that the other server returned was:
91             # 500 Remote server does not support TLS (state 6).
92             '6' => { 'command' => 'MAIL', 'reason' => 'failedstarttls' },
93              
94             # https://www.google.td/support/forum/p/gmail/thread?tid=08a60ebf5db24f7b&hl=en
95             # Technical details of permanent failure:
96             # Google tried to deliver your message, but it was rejected by the recipient domain.
97             # We recommend contacting the other email provider for further information about the
98             # cause of this error. The error that the other server returned was:
99             # 535 SMTP AUTH failed with the remote server. (state 8).
100             '8' => { 'command' => 'AUTH', 'reason' => 'systemerror' },
101              
102             # https://www.google.co.nz/support/forum/p/gmail/thread?tid=45208164dbca9d24&hl=en
103             # Technical details of temporary failure:
104             # Google tried to deliver your message, but it was rejected by the recipient domain.
105             # We recommend contacting the other email provider for further information about the
106             # cause of this error. The error that the other server returned was:
107             # 454 454 TLS missing certificate: error:0200100D:system library:fopen:Permission denied (#4.3.0) (state 9).
108             '9' => { 'command' => 'AUTH', 'reason' => 'failedstarttls' },
109              
110             # https://www.google.com/support/forum/p/gmail/thread?tid=5cfab8c76ec88638&hl=en
111             # Technical details of permanent failure:
112             # Google tried to deliver your message, but it was rejected by the recipient domain.
113             # We recommend contacting the other email provider for further information about the
114             # cause of this error. The error that the other server returned was:
115             # 500 Remote server does not support SMTP Authenticated Relay (state 12).
116             '12' => { 'command' => 'AUTH', 'reason' => 'norelaying' },
117              
118             # Technical details of permanent failure:
119             # Google tried to deliver your message, but it was rejected by the recipient domain.
120             # We recommend contacting the other email provider for further information about the
121             # cause of this error. The error that the other server returned was:
122             # 550 550 5.7.1 <****@gmail.com>... Access denied (state 13).
123             '13' => { 'command' => 'EHLO', 'reason' => 'blocked' },
124              
125             # Technical details of permanent failure:
126             # Google tried to deliver your message, but it was rejected by the recipient domain.
127             # We recommend contacting the other email provider for further information about the
128             # cause of this error. The error that the other server returned was:
129             # 550 550 5.1.1 <******@*********.**>... User Unknown (state 14).
130             # 550 550 5.2.2 <*****@****.**>... Mailbox Full (state 14).
131             #
132             '14' => { 'command' => 'RCPT', 'reason' => 'userunknown' },
133              
134             # https://www.google.cz/support/forum/p/gmail/thread?tid=7090cbfd111a24f9&hl=en
135             # Technical details of permanent failure:
136             # Google tried to deliver your message, but it was rejected by the recipient domain.
137             # We recommend contacting the other email provider for further information about the
138             # cause of this error. The error that the other server returned was:
139             # 550 550 5.7.1 SPF unauthorized mail is prohibited. (state 15).
140             # 554 554 Error: no valid recipients (state 15).
141             '15' => { 'command' => 'DATA', 'reason' => 'filtered' },
142              
143             # https://www.google.com/support/forum/p/Google%20Apps/thread?tid=0aac163bc9c65d8e&hl=en
144             # Technical details of permanent failure:
145             # Google tried to deliver your message, but it was rejected by the recipient domain.
146             # We recommend contacting the other email provider for further information about the
147             # cause of this error. The error that the other server returned was:
148             # 550 550 <****@***.**> No such user here (state 17).
149             # 550 550 #5.1.0 Address rejected ***@***.*** (state 17).
150             '17' => { 'command' => 'DATA', 'reason' => 'filtered' },
151              
152             # Technical details of permanent failure:
153             # Google tried to deliver your message, but it was rejected by the recipient domain.
154             # We recommend contacting the other email provider for further information about the
155             # cause of this error. The error that the other server returned was:
156             # 550 550 Unknown user *****@***.**.*** (state 18).
157             '18' => { 'command' => 'DATA', 'reason' => 'filtered' },
158             };
159 312         1347 require Sisimai::Address;
160              
161 312         1447 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  312         717  
162 312         1620 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
163 312         625 my $readcursor = 0; # (Integer) Points the current cursor position
164 312         513 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
165              
166 312         4788 for my $e ( split("\n", $emailparts->[0]) ) {
167             # Read error messages and delivery status lines from the head of the email to the previous
168             # line of the beginning of the original message.
169 13181 100       18308 unless( $readcursor ) {
170             # Beginning of the bounce message or message/delivery-status part
171 12703 100       21310 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
172             }
173 13181 100 100     27176 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
174              
175             # Technical details of permanent failure:=20
176             # Google tried to deliver your message, but it was rejected by the recipient =
177             # domain. We recommend contacting the other email provider for further inform=
178             # ation about the cause of this error. The error that the other server return=
179             # ed was: 554 554 5.7.0 Header error (state 18).
180             #
181             # -- OR --
182             #
183             # Technical details of permanent failure:=20
184             # Google tried to deliver your message, but it was rejected by the server for=
185             # the recipient domain example.jp by mx.example.jp. [192.0.2.49].
186             #
187             # The error that the other server returned was:
188             # 550 5.1.1 ... User Unknown
189             #
190 376         3623 $v = $dscontents->[-1];
191              
192 376 100 66     1073 if( index($e, ' ') == 0 && index($e, '@') > 0 ) {
193             # kijitora@example.jp: 550 5.2.2 ... Mailbox Full
194 76 50       222 if( $v->{'recipient'} ) {
195             # There are multiple recipient addresses in the message body.
196 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
197 0         0 $v = $dscontents->[-1];
198             }
199              
200 76         771 my $r = Sisimai::Address->s3s4(substr($e, rindex($e, ' ') + 1,));
201 76 50       801 next unless Sisimai::Address->is_emailaddress($r);
202 76         299 $v->{'recipient'} = $r;
203 76         189 $recipients++;
204              
205             } else {
206 300         821 $v->{'diagnosis'} .= $e.' ';
207             }
208             }
209 312 100       4285 return undef unless $recipients;
210              
211 76         460 require Sisimai::String;
212 76         309 require Sisimai::RFC1123;
213 76         178 for my $e ( @$dscontents ) {
214 76         405 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
215             # Get the value of remote host
216 76 100       381 if( Sisimai::String->aligned(\$e->{'diagnosis'}, [' by ', '. [', ']. ']) ) {
217             # Google tried to deliver your message, but it was rejected by the server for the recipient
218             # domain example.jp by mx.example.jp. [192.0.2.153].
219 11         35 my $p1 = rindex($e->{'diagnosis'}, ' by ');
220 11         33 my $p2 = rindex($e->{'diagnosis'}, '. [' );
221 11         40 my $hostname = substr($e->{'diagnosis'}, $p1 + 4, $p2 - $p1 - 4);
222 11         38 my $ipv4addr = substr($e->{'diagnosis'}, $p2 + 3, rindex($e->{'diagnosis'}, ']. ') - $p2 - 3);
223              
224 11 50       53 $e->{'rhost'} = $hostname if Sisimai::RFC1123->is_internethost($hostname);
225 11   33     38 $e->{'rhost'} ||= $ipv4addr;
226             }
227              
228 76         194 while(1) {
229             # Find "(state 18)" and pick "18" as a key of statetable
230 76 100       238 my $p1 = rindex($e->{'diagnosis'}, ' (state '); last if $p1 < 0;
  76         450  
231 30 50       82 my $p2 = rindex($e->{'diagnosis'}, ')'); last if $p1 < 0;
  30         89  
232 30 50       99 last if $p1 > $p2;
233 30         108 my $cu = substr($e->{'diagnosis'}, $p1 + 8, $p2 - $p1 - 8);
234 30 50       91 last unless $cu;
235              
236 30         118 $e->{'reason'} = $statetable->{ $cu }->{'reason'};
237 30         68 $e->{'command'} = $statetable->{ $cu }->{'command'};
238 30         58 last;
239             }
240 76 100       238 unless( $e->{'reason'} ) {
241             # There is no state code in the error message
242 46         171 FINDREASON: for my $r ( keys %$messagesof ) {
243             # Verify each regular expression of session errors
244 86 100       191 next unless grep { index($e->{'diagnosis'}, $_) > -1 } $messagesof->{ $r }->@*;
  218         610  
245 30         79 $e->{'reason'} = $r;
246 30         66 last;
247             }
248             }
249 76 100 100     294 $e->{'reason'} ||= ''; next unless $e->{'reason'};
  76         215  
250              
251             # Set pseudo status code and override bounce reason
252 60   100     865 $e->{'status'} = Sisimai::SMTP::Status->find($e->{'diagnosis'}) || '';
253 60 100 100     370 next if length($e->{'status'}) == 0 || index($e->{'status'}, '.0') > 0;
254 20   50     104 $e->{'reason'} = Sisimai::SMTP::Status->name($e->{'status'}) || '';
255             }
256 76         561 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
257             }
258              
259             1;
260             __END__