-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_verify.inc
911 lines (877 loc) · 32.4 KB
/
email_verify.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
<?php
/**
* @file
* Checks the email address for validity.
*/
/**
* Manage cache before calling internal verification function.
*/
function _email_verify_check($mail, $use_caching = FALSE) {
// Check to see if debugging is enables, and if it is, start collectiong
// debugging information.
$config = config('email_verify.settings');
$debugging_mode = $config->get('debug_mode');
$date_time_format = $config->get('debug_mode_date_format');
$debugging_text = array();
if ($debugging_mode) {
$debugging_text[] = t(
'Beginning the verification of email address "%mail" (!date_time).',
array(
'%mail' => $mail,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
$results = array();
// SMTP servers will eventually block the IP address used to verify email
// addresses, so it is better to try and cache them.
$cache_key = 'email_block_' . $mail;
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if the results will be returned from the cache (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if ($use_caching && $cache = cache_get($cache_key, 'cache_email_verify')) {
if ($debugging_mode) {
$debugging_text[] = t(
'Caching has been selected, so the results will be returned from the cache (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
// An empty data property means the email verification had some issue
// (rejected, etc.).
if (!empty($cache->data)) {
watchdog('email_verify', 'Cached verification for [@mail]: @result', array('@mail' => $mail, '@result' => $cache->data));
}
$results = $cache->data;
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'Caching was not selected, so the results will be returned from a fresh verification (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$results = _email_verify_check_internal($mail, $debugging_mode, $debugging_text, $date_time_format);
cache_set($cache_key, $results, 'cache_email_verify', CACHE_TEMPORARY);
}
if ($debugging_mode) {
$results['debugging_text'][] = t(
'Ending the verification of email address "%mail". It %verification_results verification (!date_time).',
array(
'%mail' => $mail,
'%verification_results' => empty($results['verification_message']) ? 'passed' : 'failed',
'!date_time' => format_date(time(), $date_time_format),
)
);
}
return $results;
}
/**
* Checks the email address for validity.
*/
function _email_verify_check_internal($mail, $debugging_mode, $debugging_text, $date_time_format) {
$config = config('email_verify.settings');
if ($debugging_mode) {
$debugging_text[] = t(
'Checking if the address is syntactically incorrect (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (!valid_email_address($mail)) {
// The address is syntactically incorrect. The problem will be caught by the
// user module, so avoid duplicating the error reporting by just returning.
if ($debugging_mode) {
$debugging_text[] = t(
'The address is not syntactically correct, so verification is stopping here (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
return array(
'verification_message' => '',
'debugging_text' => $debugging_text,
);
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'The address is syntactically correct, so verification is continuing (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// If this is a Windows based computer, load the Windows compatibilty file.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if Microsoft Windows compatible functions are needed (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if ($debugging_mode) {
$debugging_text[] = t(
'Microsoft Windows was detected, so the compatible functions are being loaded (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
module_load_include('inc', 'email_verify', 'windows_compat');
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'Microsoft Windows was not detected, so the compatible functions are not being loaded (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
$host = backdrop_substr(strstr($mail, '@'), 1);
if ($debugging_mode) {
$debugging_text[] = t(
'The host that will be used for verifying the email address is "%host" (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if a dot should be added to the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if ($config->get('methods_add_dot')) {
$host = $host . '.';
if ($debugging_mode) {
$debugging_text[] = t(
'Adding a dot to the domain name. The new host to use for verification is "%host" (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'Not adding a dot to the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// Check the DNS records of the email address' domain name to see if anything
// is reported.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if the checkdnsrr() method should be used to verify the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if ($config->get('methods_checkdnsrr')) {
if ($debugging_mode) {
$debugging_text[] = t(
'The checkdnsrr() method will be used to verify the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (!checkdnsrr($host, 'ANY')) {
if ($debugging_mode) {
$debugging_text[] = t(
'No DNS records were found for host "%host", so verification is stopping here (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog('email_verify', 'No DNS records were found, using checkdnsrr() with "%host" for host and "ANY" for type.', array('%host' => $host));
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('No DNS records were found, using checkdnsrr() with "%host" for host and "ANY" for type.', array('%host' => $host)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => t('%host is not a valid email host. Please check the spelling and try again.', array('%host' => "$host")),
'debugging_text' => $debugging_text,
);
}
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'DNS records were found for host "%host", so verification is continuing (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
}
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'The checkdnsrr() method will not be used to verify the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// Check to see if the email address' domain name resolves to an IPv4 address.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if the gethostbyname() method should be used to verify the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if ($config->get('methods_gethostbyname')) {
if ($debugging_mode) {
$debugging_text[] = t(
'The gethostbyname() method will be used to verify the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (gethostbyname($host) == $host) {
if ($debugging_mode) {
$debugging_text[] = t(
'No IPv4 address was found for host "%host", so verification is stopping here (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog('email_verify', 'No IPv4 address was found, using gethostbyname() with %host.', array('%host' => $host));
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('No IPv4 address was found, using gethostbyname() with %host.', array('%host' => $host)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => t('%host is not a valid email host. Please check the spelling and try again.', array('%host' => "$host")),
'debugging_text' => $debugging_text,
);
}
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'IPv4 address was found for host "%host", so verification is continuing (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
}
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'The gethostbyname() method will not be used to verify the domain name (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// If install found port 25 closed or fsockopen() disabled, we can't test
// mailboxes.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if the "email_verify_skip_mailbox" variable is set to TRUE, indicating that the system is unable to check mailboxes (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if ($config->get('skip_mailbox')) {
if ($debugging_mode) {
$debugging_text[] = t(
'The "email_verify_skip_mailbox" variable is set to TRUE, so verification is stopping here (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
return array(
'verification_message' => '',
'debugging_text' => $debugging_text,
);
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'The "email_verify_skip_mailbox" variable is set to FALSE, so verification is continuing (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// What SMTP servers should we contact?
if ($debugging_mode) {
$debugging_text[] = t(
'Retrieving any MX records corresponding to the specified host "%host" (!date_time).',
array(
'%host' => $host,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
$mx_hosts = array();
if (!getmxrr($host, $mx_hosts)) {
// When there is no MX record, the host itself should be used.
if ($debugging_mode) {
$debugging_text[] = t(
'No MX records were found, so the host itself will be used to check the system capability (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$mx_hosts[] = $host;
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'MX records were found, so they will be used to check the system capability (!date_time): !mx_hosts',
array(
'!date_time' => format_date(time(), $date_time_format),
'!mx_hosts' => '<pre>' . print_r($mx_hosts, TRUE) . '</pre>',
)
);
}
}
$timeout = $config->get('test_options_timeout');
if ($debugging_mode) {
$debugging_text[] = t(
'The timeout setting for checking the system capability is "%timeout" seconds (!date_time).',
array(
'%timeout' => $timeout,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
// Try to connect to one SMTP server.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking the host(s) to see if a connection can be made to any of them (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
foreach ($mx_hosts as $smtp) {
$connect = @fsockopen($smtp, 25, $errno, $errstr, $timeout);
if ($debugging_mode) {
if ($connect === FALSE) {
$debugging_text[] = t(
'The attempt to connect to host "%smtp" failed. If provided, the error number was "%errno", and the error string was "%errstr" (!date_time).',
array(
'%smtp' => $smtp,
'%errno' => $errno,
'%errstr' => $errstr,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
else {
$debugging_text[] = t(
'The attempt to connect to host "%smtp" succeeded. If provided, the error number was "%errno", and the error string was "%errstr" (!date_time).',
array(
'%smtp' => $smtp,
'%errno' => $errno,
'%errstr' => $errstr,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
}
if (!$connect) {
if ($debugging_mode) {
$debugging_text[] = t(
'The system could not connect to "%smtp" and is continuing to the next host in the list (!date_time).',
array(
'%smtp' => $smtp,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($connect === FALSE && $errno === 0) {
if ($debugging_mode) {
$debugging_text[] = t(
'The result of fsockopen() was FALSE, and the error number was 0, which indicates a potential problem initializing initializing the socket. This is the error string: "%errstr" (!date_time).',
array(
'%errstr' => $errstr,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog('email_verify', 'There was a potential problem initializing the socket when attempting to check an email address.', array(), WATCHDOG_WARNING);
}
continue;
}
// Set a limit on the amount of time to wait for a socket to be opened.
stream_set_timeout($connect, 60);
if ($debugging_mode) {
$debugging_text[] = t(
'The timeout setting for opening a socket is "60" seconds (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (preg_match("/^220/", $connect_result = fgets($connect, 1024))) {
// An SMTP connection was made.
if ($debugging_mode) {
$debugging_text[] = t(
'A connection was made to "%smtp", and so verification will stop connecting to hosts and continue (!date_time).',
array(
'%smtp' => $smtp,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
break;
}
else {
// The SMTP server probably does not like us (dynamic/residential IP for
// aol.com for instance).
// Be on the safe side and accept the address. At least it has a valid
// domain part.
// The connection might have timed-out, or there might have been some
// other network related errors. The details can be obtained from
// stream_get_meta_data().
$metadata = stream_get_meta_data($connect);
if ($debugging_mode) {
$debugging_text[] = t(
'A connection was made to "%smtp", but a positive response was not recieved. There may be additional information from the server: "%metadata". Verification is stopping here (!date_time).',
array(
'%smtp' => $smtp,
'%metadata' => $metadata,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog(
'email_verify',
'Could not verify email address "%address" at host "%host": "%connect_result". Timed out at: %timedout',
array(
'%address' => $mail,
'%host' => $host,
'%connect_result' => $connect_result,
'%timedout' => $metadata['timed_out'],
),
WATCHDOG_WARNING
);
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('Could not verify email address at host %host: %connect_result', array('%host' => $host, '%connect_result' => $connect_result)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => '',
'debugging_text' => $debugging_text,
);
}
}
}
if (!$connect) {
if ($debugging_mode) {
$debugging_text[] = t(
'No connection could be made to any host, so verification is stopping here (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
return array(
'verification_message' => t('%host is not a valid email host. Please check the spelling and try again or contact us for clarification.', array('%host' => "$host")),
'debugging_text' => $debugging_text,
);
}
$from_address = config_get('system.core', 'site_mail');
if ($debugging_mode) {
$debugging_text[] = t(
'The email address to use as the From address for the verification tests is "%from_address" (!date_time).',
array(
'%from_address' => $from_address,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
// Extract the <...> part, if there is one.
if ($debugging_mode) {
$debugging_text[] = t(
'Checking to see if From address needs to have non-email address text removed (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
if (preg_match('/\<(.*)\>/', $from_address, $match) > 0) {
$from_address = $match[1];
if ($debugging_mode) {
$debugging_text[] = t(
'The From address needed to have non-email address text removed. The new From address to use for verification is "%from_address" (!date_time).',
array(
'%from_address' => $from_address,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
}
else {
if ($debugging_mode) {
$debugging_text[] = t(
'The From address did not need to have non-email address text removed (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
}
// Should be good enough for RFC compliant SMTP servers.
$localhost = $_SERVER["HTTP_HOST"];
if ($debugging_mode) {
$debugging_text[] = t(
'The domain to use as the From host for the verification tests is "%localhost" (!date_time).',
array(
'%localhost' => $localhost,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if (!$localhost) {
if ($debugging_mode) {
$debugging_text[] = t(
'The domain to use as the From host was empty, "localhost" wil be used instead (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$localhost = 'localhost';
}
// Conduct the test.
if ($debugging_mode) {
$debugging_text[] = t(
'Connecting to the host with the command "HELO %localhost" (!date_time).',
array(
'%localhost' => $localhost,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
fwrite($connect, "HELO $localhost\r\n");
$connect_result = _email_verify_read_all_lines($connect, $debugging_mode, $debugging_text, $date_time_format);
if ($debugging_mode) {
$debugging_text[] = t(
'The first 1024 charcters of the response to the HELO command: "%connect_result" (!date_time).',
array(
'%connect_result' => $connect_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($debugging_mode) {
$debugging_text[] = t(
'Connecting to the host with the command "MAIL FROM: %from_address" (!date_time).',
array(
'%from_address' => $from_address,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
fwrite($connect, "MAIL FROM: <$from_address>\r\n");
$from_result = _email_verify_read_all_lines($connect, $debugging_mode, $debugging_text, $date_time_format);
if ($debugging_mode) {
$debugging_text[] = t(
'The first 1024 charcters of the response to the MAIL FROM command: "%from_result" (!date_time).',
array(
'%from_result' => $from_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($debugging_mode) {
$debugging_text[] = t(
'Connecting to the host with the command "RCPT TO: {%mail}" (!date_time).',
array(
'%mail' => $mail,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
fwrite($connect, "RCPT TO: <{$mail}>\r\n");
$to_result = _email_verify_read_all_lines($connect, $debugging_mode, $debugging_text, $date_time_format);
if ($debugging_mode) {
$debugging_text[] = t(
'The first 1024 charcters of the response to the RCPT TO command: "%to_result" (!date_time).',
array(
'%to_result' => $to_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
if ($debugging_mode) {
$debugging_text[] = t(
'Connecting to the host with the command "QUIT" (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
fwrite($connect, "QUIT\r\n");
if ($debugging_mode) {
$debugging_text[] = t(
'Closing the connection to the host (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
fclose($connect);
// Check the results.
if (!preg_match("/^250/", $from_result)) {
// Again, something went wrong before we could really test the address.
// Be on the safe side and accept it.
if ($debugging_mode) {
$debugging_text[] = t(
'A positive response was not recieved from the mail server. The result was "%from_result". Verification is stopping here (!date_time).',
array(
'%from_result' => $from_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog(
'email_verify',
'Could not verify email address "%address" at host "%host": <p>Results: Connection: %connect_result</p><p>From address: %from_result</p><p>To address: %to_result</p>',
array(
'%address' => $mail,
'%host' => $host,
'%connect_result' => $connect_result,
'%from_result' => $from_result,
'%to_result' => $to_result,
),
WATCHDOG_WARNING
);
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('Could not verify email address at host %host: %from_result', array('%host' => $host, '%from_result' => $from_result)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => '',
'debugging_text' => $debugging_text,
);
}
}
if (
// This server does not like us (noos.fr behaves like this for instance).
preg_match("/(Client host|Helo command) rejected/", $to_result) ||
// Any 4xx response means we couldn't really check, usually due to being
// temporarily unavailable. However, these can also be Greylisting responses
// from the receiving mail server, used to filter incoming mail.
preg_match("/^4/", $to_result)) {
// In those cases, accept the email, but log a warning.
if ($debugging_mode) {
$debugging_text[] = t(
'A positive response was not recieved from the mail server. The result was "%to_result". Verification is stopping here (!date_time).',
array(
'%to_result' => $to_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog(
'email_verify',
'Could not verify email address "%address" at host "%host": <p>Results: Connection: %connect_result</p><p>From address: %from_result</p><p>To address: %to_result</p>',
array(
'%address' => $mail,
'%host' => $host,
'%connect_result' => $connect_result,
'%from_result' => $from_result,
'%to_result' => $to_result,
),
WATCHDOG_WARNING
);
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('Could not verify email address at host %host: %to_result', array('%host' => $host, '%to_result' => $to_result)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => '',
'debugging_text' => $debugging_text,
);
}
}
// If there is a network level error, such as timeout, $to_result will be
// empty.
if (empty($to_result)) {
if ($debugging_mode) {
$debugging_text[] = t(
'There was potentially a network related error, as the result from the To address "%mail" check was empty. The result was "%to_result". Verification is stopping here (!date_time).',
array(
'%mail' => $mail,
'%to_result' => $to_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog(
'email_verify',
'Could not verify email address "%address" at host "%host": <p>Results: Connection: %connect_result</p><p>From address: %from_result</p><p>To address: %to_result</p>',
array(
'%address' => $mail,
'%host' => $host,
'%connect_result' => $connect_result,
'%from_result' => $from_result,
'%to_result' => $to_result,
)
);
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('Could not verify email address "%mail", potentially due to a network error. Reason: %to_result', array('%mail' => $mail, '%to_result' => $to_result)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => t('Could not verify email address "%mail", potentially due to a network error. Please check the spelling, wait a moment, and try again or contact us for clarification.', array('%mail' => "$mail")),
'debugging_text' => $debugging_text,
);
}
}
// This is an explicit rejection.
if (!preg_match("/^250/", $to_result)) {
if ($debugging_mode) {
$debugging_text[] = t(
'The To "%mail" address was rejected from the mail server. The result was "%to_result". Verification is stopping here (!date_time).',
array(
'%mail' => $mail,
'%to_result' => $to_result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
watchdog(
'email_verify',
'Rejected email address: "%address". Reason: <p>Connection: %connect_result</p><p>From address: %from_result</p><p>To address: %to_result</p>',
array(
'%address' => $mail,
'%connect_result' => $connect_result,
'%from_result' => $from_result,
'%to_result' => $to_result,
),
WATCHDOG_WARNING
);
if (function_exists('email_verify_access_people_email_verify') && email_verify_access_people_email_verify()) {
return array(
'verification_message' => t('Rejected email address: %mail. Reason: %to_result', array('%mail' => $mail, '%to_result' => $to_result)),
'debugging_text' => $debugging_text,
);
}
else {
return array(
'verification_message' => t('%mail is not a valid email address. Please check the spelling and try again or contact us for clarification.', array('%mail' => "$mail")),
'debugging_text' => $debugging_text,
);
}
}
// A pass is signified by returning an empty verification_message string.
return array(
'verification_message' => '',
'debugging_text' => $debugging_text,
);
}
/**
* Read response from SMTP server, multiline compatible.
*
* @param string $connect
* The connection response string from the server.
* @param bool $debugging_mode
* Whether to collect debugging information or not.
* @param array $debugging_text
* An array of strings, each of which are a line of debugging information.
* @param string $date_time_format
* The data and time format to use for the debugging information.
*
* @return string
* The response from the server, formatted for use by this module.
*/
function _email_verify_read_all_lines($connect, $debugging_mode, array &$debugging_text, $date_time_format) {
if ($debugging_mode) {
$debugging_text[] = t(
'Reading all lines of the connection response (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$result = '';
do {
$line = _email_verify_read_line($connect, $debugging_mode, $debugging_text, $date_time_format);
$result .= $line['line'];
} while ($line['next_char'] == '-');
if ($debugging_mode) {
$debugging_text[] = t(
'Done reading all lines of the connection response. The result was: "%result" (!date_time).',
array(
'%result' => $result,
'!date_time' => format_date(time(), $date_time_format),
)
);
}
return $result;
}
/**
* SMTP Single line read.
*
* Reads a single response line from SMTP server and identifies response CODE
* and Following Character (" " or "-")
*
* @param string $connect
* A single line from the connection response string from the server.
* @param bool $debugging_mode
* Whether to collect debugging information or not.
* @param array $debugging_text
* An array of strings, each of which are a line of debugging information.
* @param string $date_time_format
* The data and time format to use for the debugging information.
*
* @return array
* A sinlge line of the response from the server, formatted for use by this
* module. THe array consists of three elements:
* - code: The numeric response code from the server.
* - next_char: A character indicating if there are more lines to follow, or
* if this is the last line. A "-" indicates that more text is following,
* and a " " (space) indicates that there is no more text.
* - line: The string of text for this line from the server's response.
*/
function _email_verify_read_line($connect, $debugging_mode, array &$debugging_text, $date_time_format) {
if ($debugging_mode) {
$debugging_text[] = t(
'Reading a single line of the connection response (!date_time).',
array('!date_time' => format_date(time(), $date_time_format))
);
}
$response = fgets($connect, 1024);
$strlen = strlen($response);
$code = '';
// Try to get response code: all numeric characters at the begginging of the
// response string.
$i = 0;
for ($i = 0; $i <= $strlen; $i++) {
$char = substr($response, $i, 1);
if (!is_numeric($char)) {
break;
}
$code .= $char;
}
// If there is a slash character after the response code then more lines are
// to be expected.
// @see https://bugs.php.net/bug.php?id=6537
// (That bug was fixed in PHP between 2001-01-10 and 2002-06-02. The reference
// is included here for background information.)
$next_char = substr($response, $i, 1);
if ($debugging_mode) {
$debugging_text[] = t(
'Done reading a single line of the connection response. The results were: Code: "%code", More lines: "@next_char" (!more_lines), Response: "%response" (!date_time).',
array(
'%code' => $code,
'@next_char' => $next_char,
'%response' => $response,
'!more_lines' => ($next_char == " ") ? 'no' : 'yes',
'!date_time' => format_date(time(), $date_time_format),
)
);
}
return array('code' => $code, 'next_char' => $next_char, 'line' => $response);
}