File Coverage

lib/Sisimai/Lhost/Gmail.pm
Criterion Covered Total %
statement 67 69 97.1
branch 27 34 79.4
condition 18 22 81.8
subroutine 6 6 100.0
pod 2 2 100.0
total 120 133 90.2


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Gmail;
2 39     39   3716 use parent 'Sisimai::Lhost';
  39         67  
  39         218  
3 39     39   2720 use v5.26;
  39         123  
4 39     39   165 use strict;
  39         47  
  39         950  
5 39     39   289 use warnings;
  39         67  
  39         32144  
6              
7 1     1 1 2 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 1043     1043 1 3538 my $class = shift;
16 1043   100     2007 my $mhead = shift // return undef;
17 1042   100     1928 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 1041 100       3154 return undef unless rindex($mhead->{'from'}, '') > -1;
67 312 50       787 return undef unless index($mhead->{'subject'}, 'Delivery Status Notification') > -1;
68              
69 312         429 state $indicators = __PACKAGE__->INDICATORS;
70 312         305 state $boundaries = ['----- Original message -----', '----- Message header follows -----'];
71 312         411 state $startingof = {
72             'message' => ['Delivery to the following recipient'],
73             'error' => ['The error that the other server returned was:'],
74             };
75 312         455 state $statetable = {
76             # Technical details of permanent failure:
77             # Google tried to deliver your message, but it was rejected by the recipient domain.
78             # We recommend contacting the other email provider for further information about the
79             # cause of this error. The error that the other server returned was:
80             # 500 Remote server does not support TLS (state 6).
81             '6' => { 'command' => 'MAIL', 'reason' => 'failedstarttls' },
82              
83             # https://www.google.td/support/forum/p/gmail/thread?tid=08a60ebf5db24f7b&hl=en
84             # Technical details of permanent failure:
85             # Google tried to deliver your message, but it was rejected by the recipient domain.
86             # We recommend contacting the other email provider for further information about the
87             # cause of this error. The error that the other server returned was:
88             # 535 SMTP AUTH failed with the remote server. (state 8).
89             '8' => { 'command' => 'AUTH', 'reason' => 'systemerror' },
90              
91             # https://www.google.co.nz/support/forum/p/gmail/thread?tid=45208164dbca9d24&hl=en
92             # Technical details of temporary failure:
93             # Google tried to deliver your message, but it was rejected by the recipient domain.
94             # We recommend contacting the other email provider for further information about the
95             # cause of this error. The error that the other server returned was:
96             # 454 454 TLS missing certificate: error:0200100D:system library:fopen:Permission denied (#4.3.0) (state 9).
97             '9' => { 'command' => 'AUTH', 'reason' => 'failedstarttls' },
98              
99             # https://www.google.com/support/forum/p/gmail/thread?tid=5cfab8c76ec88638&hl=en
100             # Technical details of permanent failure:
101             # Google tried to deliver your message, but it was rejected by the recipient domain.
102             # We recommend contacting the other email provider for further information about the
103             # cause of this error. The error that the other server returned was:
104             # 500 Remote server does not support SMTP Authenticated Relay (state 12).
105             '12' => { 'command' => 'AUTH', 'reason' => 'norelaying' },
106              
107             # Technical details of permanent failure:
108             # Google tried to deliver your message, but it was rejected by the recipient domain.
109             # We recommend contacting the other email provider for further information about the
110             # cause of this error. The error that the other server returned was:
111             # 550 550 5.7.1 <****@gmail.com>... Access denied (state 13).
112             '13' => { 'command' => 'EHLO', 'reason' => 'blocked' },
113              
114             # Technical details of permanent failure:
115             # Google tried to deliver your message, but it was rejected by the recipient domain.
116             # We recommend contacting the other email provider for further information about the
117             # cause of this error. The error that the other server returned was:
118             # 550 550 5.1.1 <******@*********.**>... User Unknown (state 14).
119             # 550 550 5.2.2 <*****@****.**>... Mailbox Full (state 14).
120             #
121             '14' => { 'command' => 'RCPT', 'reason' => 'userunknown' },
122              
123             # https://www.google.cz/support/forum/p/gmail/thread?tid=7090cbfd111a24f9&hl=en
124             # Technical details of permanent failure:
125             # Google tried to deliver your message, but it was rejected by the recipient domain.
126             # We recommend contacting the other email provider for further information about the
127             # cause of this error. The error that the other server returned was:
128             # 550 550 5.7.1 SPF unauthorized mail is prohibited. (state 15).
129             # 554 554 Error: no valid recipients (state 15).
130             '15' => { 'command' => 'DATA', 'reason' => 'filtered' },
131              
132             # https://www.google.com/support/forum/p/Google%20Apps/thread?tid=0aac163bc9c65d8e&hl=en
133             # Technical details of permanent failure:
134             # Google tried to deliver your message, but it was rejected by the recipient domain.
135             # We recommend contacting the other email provider for further information about the
136             # cause of this error. The error that the other server returned was:
137             # 550 550 <****@***.**> No such user here (state 17).
138             # 550 550 #5.1.0 Address rejected ***@***.*** (state 17).
139             '17' => { 'command' => 'DATA', 'reason' => 'filtered' },
140              
141             # Technical details of permanent failure:
142             # Google tried to deliver your message, but it was rejected by the recipient domain.
143             # We recommend contacting the other email provider for further information about the
144             # cause of this error. The error that the other server returned was:
145             # 550 550 Unknown user *****@***.**.*** (state 18).
146             '18' => { 'command' => 'DATA', 'reason' => 'filtered' },
147             };
148 312         871 require Sisimai::Address;
149              
150 312         886 my $dscontents = [__PACKAGE__->DELIVERYSTATUS]; my $v = undef;
  312         525  
151 312         966 my $emailparts = Sisimai::RFC5322->part($mbody, $boundaries);
152 312         424 my $readcursor = 0; # (Integer) Points the current cursor position
153 312         336 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
154              
155 312         3209 for my $e ( split("\n", $emailparts->[0]) ) {
156             # Read error messages and delivery status lines from the head of the email to the previous
157             # line of the beginning of the original message.
158 13181 100       12763 unless( $readcursor ) {
159             # Beginning of the bounce message or message/delivery-status part
160 12703 100       15111 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
161             }
162 13181 100 100     17163 next if ($readcursor & $indicators->{'deliverystatus'}) == 0 || $e eq "";
163              
164             # Technical details of permanent failure:=20
165             # Google tried to deliver your message, but it was rejected by the recipient =
166             # domain. We recommend contacting the other email provider for further inform=
167             # ation about the cause of this error. The error that the other server return=
168             # ed was: 554 554 5.7.0 Header error (state 18).
169             #
170             # -- OR --
171             #
172             # Technical details of permanent failure:=20
173             # Google tried to deliver your message, but it was rejected by the server for=
174             # the recipient domain example.jp by mx.example.jp. [192.0.2.49].
175             #
176             # The error that the other server returned was:
177             # 550 5.1.1 ... User Unknown
178             #
179 376         339 $v = $dscontents->[-1];
180              
181 376 100 66     690 if( index($e, ' ') == 0 && index($e, '@') > 0 ) {
182             # kijitora@example.jp: 550 5.2.2 ... Mailbox Full
183 76 50       136 if( $v->{'recipient'} ) {
184             # There are multiple recipient addresses in the message body.
185 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
186 0         0 $v = $dscontents->[-1];
187             }
188              
189 76         523 my $r = Sisimai::Address->s3s4(substr($e, rindex($e, ' ') + 1,));
190 76 50       282 next unless Sisimai::Address->is_emailaddress($r);
191 76         197 $v->{'recipient'} = $r;
192 76         99 $recipients++;
193              
194             } else {
195 300         536 $v->{'diagnosis'} .= $e.' ';
196             }
197             }
198 312 100       2294 return undef unless $recipients;
199              
200 76         287 require Sisimai::String;
201 76         215 require Sisimai::RFC1123;
202 76         142 for my $e ( @$dscontents ) {
203             # Get the value of remote host
204 76 100       278 if( Sisimai::String->aligned(\$e->{'diagnosis'}, [' by ', '. [', ']. ']) ) {
205             # Google tried to deliver your message, but it was rejected by the server for the recipient
206             # domain example.jp by mx.example.jp. [192.0.2.153].
207 11         23 my $p1 = rindex($e->{'diagnosis'}, ' by ');
208 11         23 my $p2 = rindex($e->{'diagnosis'}, '. [' );
209 11         28 my $hostname = substr($e->{'diagnosis'}, $p1 + 4, $p2 - $p1 - 4);
210 11         31 my $ipv4addr = substr($e->{'diagnosis'}, $p2 + 3, rindex($e->{'diagnosis'}, ']. ') - $p2 - 3);
211              
212 11 50       29 $e->{'rhost'} = $hostname if Sisimai::RFC1123->is_internethost($hostname);
213 11   33     27 $e->{'rhost'} ||= $ipv4addr;
214             }
215              
216 76         114 while(1) {
217             # Find "(state 18)" and pick "18" as a key of statetable
218 76 100       146 my $p1 = rindex($e->{'diagnosis'}, ' (state '); last if $p1 < 0;
  76         121  
219 30 50       47 my $p2 = rindex($e->{'diagnosis'}, ')'); last if $p1 < 0;
  30         49  
220 30 50       65 last if $p1 > $p2;
221 30         67 my $cu = substr($e->{'diagnosis'}, $p1 + 8, $p2 - $p1 - 8);
222 30 50       54 last unless $cu;
223              
224 30         72 $e->{'reason'} = $statetable->{ $cu }->{'reason'};
225 30         41 $e->{'command'} = $statetable->{ $cu }->{'command'};
226 30         34 last;
227             }
228 76 100 100     209 $e->{'reason'} ||= ''; next unless $e->{'reason'};
  76         158  
229              
230             # Set pseudo status code and override bounce reason
231 30   100     182 $e->{'status'} = Sisimai::SMTP::Status->find($e->{'diagnosis'}) || '';
232 30 100 100     131 next if length($e->{'status'}) == 0 || index($e->{'status'}, '.0') > 0;
233 20   50     67 $e->{'reason'} = Sisimai::SMTP::Status->name($e->{'status'}) || '';
234             }
235 76         375 return {"ds" => $dscontents, "rfc822" => $emailparts->[1]};
236             }
237              
238             1;
239             __END__