File Coverage

lib/Sisimai/Lhost/Gmail.pm
Criterion Covered Total %
statement 66 69 95.6
branch 30 36 83.3
condition 6 8 75.0
subroutine 6 6 100.0
pod 2 2 100.0
total 110 121 90.9


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::Gmail;
2 15     15   6011 use parent 'Sisimai::Lhost';
  15         37  
  15         84  
3 15     15   931 use feature ':5.10';
  15         30  
  15         1222  
4 15     15   113 use strict;
  15         166  
  15         350  
5 15     15   68 use warnings;
  15         32  
  15         16847  
6              
7 2     2 1 1167 sub description { 'Gmail: https://mail.google.com' }
8             sub make {
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 parse or the arguments are missing
14             # @since v4.0.0
15 284     284 1 909 my $class = shift;
16 284   100     794 my $mhead = shift // return undef;
17 283   50     806 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 283 100       1062 return undef unless rindex($mhead->{'from'}, '') > -1;
67 76 50       309 return undef unless index($mhead->{'subject'}, 'Delivery Status Notification') > -1;
68              
69 76         186 state $indicators = __PACKAGE__->INDICATORS;
70 76         128 state $rebackbone = qr/^[ ]*-----[ ](?:Original[ ]message|Message[ ]header[ ]follows)[ ]-----/m;
71 76         153 state $startingof = {
72             'message' => ['Delivery to the following recipient'],
73             'error' => ['The error that the other server returned was:'],
74             };
75 76         152 state $markingsof = { 'start' => qr/Technical details of (?:permanent|temporary) failure:/ };
76 76         143 state $messagesof = {
77             'expired' => [
78             'DNS Error: Could not contact DNS servers',
79             'Delivery to the following recipient has been delayed',
80             'The recipient server did not accept our requests to connect',
81             ],
82             'hostunknown' => [
83             'DNS Error: Domain name not found',
84             'DNS Error: DNS server returned answer with no data',
85             ],
86             };
87 76         215 state $statetable = {
88             # Technical details of permanent failure:
89             # Google tried to deliver your message, but it was rejected by the recipient domain.
90             # We recommend contacting the other email provider for further information about the
91             # cause of this error. The error that the other server returned was:
92             # 500 Remote server does not support TLS (state 6).
93             '6' => { 'command' => 'MAIL', 'reason' => 'systemerror' },
94              
95             # https://www.google.td/support/forum/p/gmail/thread?tid=08a60ebf5db24f7b&hl=en
96             # Technical details of permanent failure:
97             # Google tried to deliver your message, but it was rejected by the recipient domain.
98             # We recommend contacting the other email provider for further information about the
99             # cause of this error. The error that the other server returned was:
100             # 535 SMTP AUTH failed with the remote server. (state 8).
101             '8' => { 'command' => 'AUTH', 'reason' => 'systemerror' },
102              
103             # https://www.google.co.nz/support/forum/p/gmail/thread?tid=45208164dbca9d24&hl=en
104             # Technical details of temporary failure:
105             # Google tried to deliver your message, but it was rejected by the recipient domain.
106             # We recommend contacting the other email provider for further information about the
107             # cause of this error. The error that the other server returned was:
108             # 454 454 TLS missing certificate: error:0200100D:system library:fopen:Permission denied (#4.3.0) (state 9).
109             '9' => { 'command' => 'AUTH', 'reason' => 'systemerror' },
110              
111             # https://www.google.com/support/forum/p/gmail/thread?tid=5cfab8c76ec88638&hl=en
112             # Technical details of permanent failure:
113             # Google tried to deliver your message, but it was rejected by the recipient domain.
114             # We recommend contacting the other email provider for further information about the
115             # cause of this error. The error that the other server returned was:
116             # 500 Remote server does not support SMTP Authenticated Relay (state 12).
117             '12' => { 'command' => 'AUTH', 'reason' => 'relayingdenied' },
118              
119             # Technical details of permanent failure:
120             # Google tried to deliver your message, but it was rejected by the recipient domain.
121             # We recommend contacting the other email provider for further information about the
122             # cause of this error. The error that the other server returned was:
123             # 550 550 5.7.1 <****@gmail.com>... Access denied (state 13).
124             '13' => { 'command' => 'EHLO', 'reason' => 'blocked' },
125              
126             # Technical details of permanent failure:
127             # Google tried to deliver your message, but it was rejected by the recipient domain.
128             # We recommend contacting the other email provider for further information about the
129             # cause of this error. The error that the other server returned was:
130             # 550 550 5.1.1 <******@*********.**>... User Unknown (state 14).
131             # 550 550 5.2.2 <*****@****.**>... Mailbox Full (state 14).
132             #
133             '14' => { 'command' => 'RCPT', 'reason' => 'userunknown' },
134              
135             # https://www.google.cz/support/forum/p/gmail/thread?tid=7090cbfd111a24f9&hl=en
136             # Technical details of permanent failure:
137             # Google tried to deliver your message, but it was rejected by the recipient domain.
138             # We recommend contacting the other email provider for further information about the
139             # cause of this error. The error that the other server returned was:
140             # 550 550 5.7.1 SPF unauthorized mail is prohibited. (state 15).
141             # 554 554 Error: no valid recipients (state 15).
142             '15' => { 'command' => 'DATA', 'reason' => 'filtered' },
143              
144             # https://www.google.com/support/forum/p/Google%20Apps/thread?tid=0aac163bc9c65d8e&hl=en
145             # Technical details of permanent failure:
146             # Google tried to deliver your message, but it was rejected by the recipient domain.
147             # We recommend contacting the other email provider for further information about the
148             # cause of this error. The error that the other server returned was:
149             # 550 550 <****@***.**> No such user here (state 17).
150             # 550 550 #5.1.0 Address rejected ***@***.*** (state 17).
151             '17' => { 'command' => 'DATA', 'reason' => 'filtered' },
152              
153             # Technical details of permanent failure:
154             # Google tried to deliver your message, but it was rejected by the recipient domain.
155             # We recommend contacting the other email provider for further information about the
156             # cause of this error. The error that the other server returned was:
157             # 550 550 Unknown user *****@***.**.*** (state 18).
158             '18' => { 'command' => 'DATA', 'reason' => 'filtered' },
159             };
160              
161 76         224 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
162 76         396 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
163 76         149 my $readcursor = 0; # (Integer) Points the current cursor position
164 76         128 my $recipients = 0; # (Integer) The number of 'Final-Recipient' header
165 76         184 my $v = undef;
166              
167 76         529 for my $e ( split("\n", $emailsteak->[0]) ) {
168             # Read error messages and delivery status lines from the head of the email
169             # to the previous line of the beginning of the original message.
170 674 100       1014 unless( $readcursor ) {
171             # Beginning of the bounce message or message/delivery-status part
172 196 100       741 $readcursor |= $indicators->{'deliverystatus'} if index($e, $startingof->{'message'}->[0]) == 0;
173             }
174 674 100       1154 next unless $readcursor & $indicators->{'deliverystatus'};
175 554 100       856 next unless length $e;
176              
177             # Technical details of permanent failure:=20
178             # Google tried to deliver your message, but it was rejected by the recipient =
179             # domain. We recommend contacting the other email provider for further inform=
180             # ation about the cause of this error. The error that the other server return=
181             # ed was: 554 554 5.7.0 Header error (state 18).
182             #
183             # -- OR --
184             #
185             # Technical details of permanent failure:=20
186             # Google tried to deliver your message, but it was rejected by the server for=
187             # the recipient domain example.jp by mx.example.jp. [192.0.2.49].
188             #
189             # The error that the other server returned was:
190             # 550 5.1.1 ... User Unknown
191             #
192 376         438 $v = $dscontents->[-1];
193              
194 376 100       1044 if( $e =~ /\A[ \t]+([^ ]+[@][^ ]+)\z/ ) {
195             # kijitora@example.jp: 550 5.2.2 ... Mailbox Full
196 76 50       276 if( $v->{'recipient'} ) {
197             # There are multiple recipient addresses in the message body.
198 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
199 0         0 $v = $dscontents->[-1];
200             }
201              
202 76         469 my $r = Sisimai::Address->s3s4($1);
203 76 50       347 next unless Sisimai::RFC5322->is_emailaddress($r);
204 76         238 $v->{'recipient'} = $r;
205 76         176 $recipients++;
206              
207             } else {
208 300         871 $v->{'diagnosis'} .= $e.' ';
209             }
210             }
211 76 50       300 return undef unless $recipients;
212              
213 76         166 for my $e ( @$dscontents ) {
214 76         491 $e->{'diagnosis'} = Sisimai::String->sweep($e->{'diagnosis'});
215              
216 76 50       253 unless( $e->{'rhost'} ) {
217             # Get the value of remote host
218 76 100       703 if( $e->{'diagnosis'} =~ /[ \t]+by[ \t]+([^ ]+)[.][ \t]+\[(\d+[.]\d+[.]\d+[.]\d+)\][.]/ ) {
219             # Google tried to deliver your message, but it was rejected by # the server
220             # for the recipient domain example.jp by mx.example.jp. [192.0.2.153].
221 11         42 my $hostname = $1;
222 11         23 my $ipv4addr = $2;
223 11 50       77 if( $hostname =~ /[-0-9a-zA-Z]+[.][a-zA-Z]+\z/ ) {
224             # Maybe valid hostname
225 11         33 $e->{'rhost'} = $hostname;
226             } else {
227             # Use IP address instead
228 0         0 $e->{'rhost'} = $ipv4addr;
229             }
230             }
231             }
232              
233 76 100       445 my $statecode0 = $e->{'diagnosis'} =~ /[(]state[ ](\d+)[)][.]/ ? $1 : 0;
234 76 100       249 if( exists $statetable->{ $statecode0 } ) {
235             # (state *)
236 30         94 $e->{'reason'} = $statetable->{ $statecode0 }->{'reason'};
237 30         77 $e->{'command'} = $statetable->{ $statecode0 }->{'command'};
238             } else {
239             # No state code
240 46         189 SESSION: for my $r ( keys %$messagesof ) {
241             # Verify each regular expression of session errors
242 62 100       97 next unless grep { index($e->{'diagnosis'}, $_) > -1 } @{ $messagesof->{ $r } };
  170         624  
  62         182  
243 30         93 $e->{'reason'} = $r;
244 30         69 last;
245             }
246             }
247 76 100       249 next unless $e->{'reason'};
248              
249             # Set pseudo status code and override bounce reason
250 60   100     392 $e->{'status'} = Sisimai::SMTP::Status->find($e->{'diagnosis'}) || '';
251 60 100       302 next unless $e->{'status'} =~ /\A[45][.][1-7][.][1-9]\z/;
252 20   50     84 $e->{'reason'} = Sisimai::SMTP::Status->name($e->{'status'}) || '';
253             }
254 76         451 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
255             }
256              
257             1;
258             __END__