truemail 3.0.2 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md DELETED
@@ -1,1539 +0,0 @@
1
- # ![Truemail - configurable framework agnostic plain Ruby email validator](https://truemail-rb.org/assets/images/truemail_logo.png)
2
-
3
- [![Maintainability](https://api.codeclimate.com/v1/badges/0fea6d2e64d78d66b149/maintainability)](https://codeclimate.com/github/truemail-rb/truemail/maintainability)
4
- [![Test Coverage](https://api.codeclimate.com/v1/badges/0fea6d2e64d78d66b149/test_coverage)](https://codeclimate.com/github/truemail-rb/truemail/test_coverage)
5
- [![CircleCI](https://circleci.com/gh/truemail-rb/truemail/tree/master.svg?style=svg)](https://circleci.com/gh/truemail-rb/truemail/tree/master)
6
- [![Gem Version](https://badge.fury.io/rb/truemail.svg)](https://badge.fury.io/rb/truemail)
7
- [![Downloads](https://img.shields.io/gem/dt/truemail.svg?colorA=004d99&colorB=0073e6)](https://rubygems.org/gems/truemail)
8
- [![In Awesome Ruby](https://raw.githubusercontent.com/sindresorhus/awesome/main/media/mentioned-badge.svg)](https://github.com/markets/awesome-ruby)
9
- [![Gitter](https://badges.gitter.im/truemail-rb/community.svg)](https://gitter.im/truemail-rb/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
10
- [![GitHub](https://img.shields.io/github/license/truemail-rb/truemail)](LICENSE.txt)
11
- [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v1.4%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)
12
-
13
- Configurable framework agnostic plain Ruby email validator. Verify email via Regex, DNS, SMTP and even more. Be sure that email address valid and exists.
14
-
15
- > Actual and maintainable documentation :books: for developers is living [here](https://truemail-rb.org/truemail-gem).
16
-
17
- ## Table of Contents
18
-
19
- - [Synopsis](#synopsis)
20
- - [Features](#features)
21
- - [Requirements](#requirements)
22
- - [Installation](#installation)
23
- - [Usage](#usage)
24
- - [Configuration features](#configuration-features)
25
- - [Setting global configuration](#setting-global-configuration)
26
- - [Read global configuration](#read-global-configuration)
27
- - [Update global configuration](#update-global-configuration)
28
- - [Reset global configuration](#reset-global-configuration)
29
- - [Using custom independent configuration](#using-custom-independent-configuration)
30
- - [Validation features](#validation-features)
31
- - [Whitelist/Blacklist check](#whitelistblacklist-check)
32
- - [Whitelist case](#whitelist-case)
33
- - [Whitelist validation case](#whitelist-validation-case)
34
- - [Blacklist case](#blacklist-case)
35
- - [Duplication case](#duplication-case)
36
- - [Regex validation](#regex-validation)
37
- - [With default regex pattern](#with-default-regex-pattern)
38
- - [With custom regex pattern](#with-custom-regex-pattern)
39
- - [DNS (MX) validation](#mx-validation)
40
- - [RFC MX lookup flow](#rfc-mx-lookup-flow)
41
- - [Not RFC MX lookup flow](#not-rfc-mx-lookup-flow)
42
- - [MX blacklist validation](#mx-blacklist-validation)
43
- - [SMTP validation](#smtp-validation)
44
- - [SMTP fail fast enabled](#smtp-fail-fast-enabled)
45
- - [SMTP safe check disabled](#smtp-safe-check-disabled)
46
- - [SMTP safe check enabled](#smtp-safe-check-enabled)
47
- - [Host audit features](#host-audit-features)
48
- - [IP audit](#ip-audit)
49
- - [DNS audit](#dns-audit)
50
- - [PTR audit](#ptr-audit)
51
- - [Example of using](#example-of-using)
52
- - [Event logger](#event-logger)
53
- - [Available tracking events](#available-tracking-events)
54
- - [JSON serializers](#json-serializers)
55
- - [Auditor JSON serializer](#auditor-json-serializer)
56
- - [Validator JSON serializer](#validator-json-serializer)
57
- - [Truemail helpers](#truemail-helpers)
58
- - [.valid?](#valid)
59
- - [#as_json](#as_json)
60
- - [Test environment](#test-environment)
61
- - [Truemail family](#truemail-family)
62
- - [Contributing](#contributing)
63
- - [License](#license)
64
- - [Code of Conduct](#code-of-conduct)
65
- - [Credits](#credits)
66
- - [Versioning](#versioning)
67
- - [Changelog](CHANGELOG.md)
68
-
69
- ## Synopsis
70
-
71
- Email validation is a tricky thing. There are a number of different ways to validate an email address and all mechanisms must conform with the best practices and provide proper validation. The Truemail gem helps you validate emails via regex pattern, presence of DNS records, and real existence of email account on a current email server.
72
-
73
- **Syntax Checking**: Checks the email addresses via regex pattern.
74
-
75
- **Mail Server Existence Check**: Checks the availability of the email address domain using DNS records.
76
-
77
- **Mail Existence Check**: Checks if the email address really exists and can receive email via SMTP connections and email-sending emulation techniques.
78
-
79
- Also Truemail gem allows performing an audit of the host in which runs.
80
-
81
- ## Features
82
-
83
- - Configurable validator, validate only what you need
84
- - Supporting of internationalized emails ([EAI](https://en.wikipedia.org/wiki/Email_address#Internationalization))
85
- - Whitelist/blacklist validation layers
86
- - Ability to configure different MX/SMTP validation flows
87
- - Ability to configure [DEA](https://en.wikipedia.org/wiki/Disposable_email_address) validation flow
88
- - Simple SMTP debugger
89
- - Event logger
90
- - Host auditor tools (helps to detect common host problems interfering to proper email verification)
91
- - JSON serializers
92
- - Ability to use the library as independent stateless microservice ([Truemail Server](https://truemail-rb.org/truemail-rack))
93
-
94
- ## Requirements
95
-
96
- Ruby MRI 2.5.0+
97
-
98
- ## Installation
99
-
100
- Add this line to your application's Gemfile:
101
-
102
- ```ruby
103
- gem 'truemail'
104
- ```
105
-
106
- And then execute:
107
-
108
- ```bash
109
- bundle
110
- ```
111
-
112
- Or install it yourself as:
113
-
114
- ```bash
115
- gem install truemail
116
- ```
117
-
118
- ## Usage
119
-
120
- ### Configuration features
121
-
122
- You can use global gem configuration or custom independent configuration. Available configuration options:
123
-
124
- - verifier email
125
- - verifier domain
126
- - email pattern
127
- - SMTP error body pattern
128
- - connection timeout
129
- - response timeout
130
- - connection attempts
131
- - default validation type
132
- - validation type for domains
133
- - whitelisted emails
134
- - blacklisted emails
135
- - whitelisted domains
136
- - blacklisted domains
137
- - whitelist validation
138
- - blacklisted mx ip-addresses
139
- - custom DNS gateway(s)
140
- - RFC MX lookup flow
141
- - SMTP port number
142
- - SMTP fail fast
143
- - SMTP safe check
144
- - event logger
145
- - JSON serializer
146
-
147
- #### Setting global configuration
148
-
149
- To have an access for `Truemail.configuration` and gem configuration features, you must configure it first as in the example below:
150
-
151
- ```ruby
152
- require 'truemail'
153
-
154
- Truemail.configure do |config|
155
- # Required parameter. Must be an existing email on behalf of which verification will be performed
156
- config.verifier_email = 'verifier@example.com'
157
-
158
- # Optional parameter. Must be an existing domain on behalf of which verification will be performed.
159
- # By default verifier domain based on verifier email
160
- config.verifier_domain = 'somedomain.com'
161
-
162
- # Optional parameter. You can override default regex pattern
163
- config.email_pattern = /regex_pattern/
164
-
165
- # Optional parameter. You can override default regex pattern
166
- config.smtp_error_body_pattern = /regex_pattern/
167
-
168
- # Optional parameter. Connection timeout in seconds.
169
- # It is equal to 2 by default.
170
- config.connection_timeout = 1
171
-
172
- # Optional parameter. A SMTP server response timeout in seconds.
173
- # It is equal to 2 by default.
174
- config.response_timeout = 1
175
-
176
- # Optional parameter. Total of connection attempts. It is equal to 2 by default.
177
- # This parameter uses in mx lookup timeout error and smtp request (for cases when
178
- # there is one mx server).
179
- config.connection_attempts = 3
180
-
181
- # Optional parameter. You can predefine default validation type for
182
- # Truemail.validate('email@email.com') call without with-parameter
183
- # Available validation types: :regex, :mx, :mx_blacklist, :smtp
184
- config.default_validation_type = :mx
185
-
186
- # Optional parameter. You can predefine which type of validation will be used for domains.
187
- # Also you can skip validation by domain.
188
- # Available validation types: :regex, :mx, :mx_blacklist, :smtp
189
- # This configuration will be used over current or default validation type parameter
190
- # All of validations for 'somedomain.com' will be processed with regex validation only.
191
- # And all of validations for 'otherdomain.com' will be processed with mx validation only.
192
- # It is equal to empty hash by default.
193
- config.validation_type_for = { 'somedomain.com' => :regex, 'otherdomain.com' => :mx }
194
-
195
- # Optional parameter. Validation of email which contains whitelisted emails always will
196
- # return true. Other validations will not processed even if it was defined in validation_type_for
197
- # It is equal to empty array by default.
198
- config.whitelisted_emails = %w[user@somedomain1.com user@somedomain2.com]
199
-
200
- # Optional parameter. Validation of email which contains blacklisted emails always will
201
- # return false. Other validations will not processed even if it was defined in validation_type_for
202
- # It is equal to empty array by default.
203
- config.blacklisted_emails = %w[user@somedomain3.com user@somedomain4.com]
204
-
205
- # Optional parameter. Validation of email which contains whitelisted domain always will
206
- # return true. Other validations will not processed even if it was defined in validation_type_for
207
- # It is equal to empty array by default.
208
- config.whitelisted_domains = %w[somedomain1.com somedomain2.com]
209
-
210
- # Optional parameter. Validation of email which contains blacklisted domain always will
211
- # return false. Other validations will not processed even if it was defined in validation_type_for
212
- # It is equal to empty array by default.
213
- config.blacklisted_domains = %w[somedomain3.com somedomain4.com]
214
-
215
- # Optional parameter. With this option Truemail will validate email which contains whitelisted
216
- # domain only, i.e. if domain whitelisted, validation will passed to Regex, MX or SMTP validators.
217
- # Validation of email which not contains whitelisted domain always will return false.
218
- # It is equal false by default.
219
- config.whitelist_validation = true
220
-
221
- # Optional parameter. With this option Truemail will filter out unwanted mx servers via
222
- # predefined list of ip addresses. It can be used as a part of DEA (disposable email
223
- # address) validations. It is equal to empty array by default.
224
- config.blacklisted_mx_ip_addresses = %w[1.1.1.1 2.2.2.2]
225
-
226
- # Optional parameter. This option will provide to use custom DNS gateway when Truemail
227
- # interacts with DNS. Valid port numbers are in the range 1-65535. If you won't specify
228
- # nameserver's ports Truemail will use default DNS TCP/UDP port 53. By default Truemail
229
- # uses DNS gateway from system settings and this option is equal to empty array.
230
- config.dns = %w[10.0.0.1 10.0.0.2:54]
231
-
232
- # Optional parameter. This option will provide to use not RFC MX lookup flow.
233
- # It means that MX and Null MX records will be cheked on the DNS validation layer only.
234
- # By default this option is disabled.
235
- config.not_rfc_mx_lookup_flow = true
236
-
237
- # Optional parameter. SMTP port number. It is equal to 25 by default.
238
- config.smtp_port = 2525
239
-
240
- # Optional parameter. This option will provide to use smtp fail fast behaviour. When
241
- # smtp_fail_fast = true it means that Truemail ends smtp validation session after first
242
- # attempt on the first mx server in any fail cases (network connection/timeout error,
243
- # smtp validation error). This feature helps to reduce total time of SMTP validation
244
- # session up to 1 second. By default this option is disabled.
245
- config.smtp_fail_fast = true
246
-
247
- # Optional parameter. This option will be parse bodies of SMTP errors. It will be helpful
248
- # if SMTP server does not return an exact answer that the email does not exist
249
- # By default this option is disabled, available for SMTP validation only.
250
- config.smtp_safe_check = true
251
-
252
- # Optional parameter. This option will enable tracking events. You can print tracking events to
253
- # stdout, write to file or both of these. Tracking event by default is :error
254
- # Available tracking event: :all, :unrecognized_error, :recognized_error, :error
255
- config.logger = { tracking_event: :all, stdout: true, log_absolute_path: '/home/app/log/truemail.log' }
256
- end
257
- ```
258
-
259
- ##### Read global configuration
260
-
261
- After successful configuration, you can read current Truemail configuration instance anywhere in your application.
262
-
263
- ```ruby
264
- Truemail.configuration
265
-
266
- => #<Truemail::Configuration:0x000055590cb17b40
267
- @connection_timeout=1,
268
- @email_pattern=/regex_pattern/,
269
- @smtp_error_body_pattern=/regex_pattern/,
270
- @response_timeout=1,
271
- @connection_attempts=3,
272
- @default_validation_type=:mx,
273
- @validation_type_by_domain={"somedomain.com" => :regex, "otherdomain.com" => :mx},
274
- @whitelisted_emails=["user@somedomain1.com", "user@somedomain2.com"],
275
- @blacklisted_emails=["user@somedomain3.com", "user@somedomain4.com"],
276
- @whitelisted_domains=["somedomain1.com", "somedomain2.com"],
277
- @blacklisted_domains=["somedomain3.com", "somedomain4.com"],
278
- @whitelist_validation=true,
279
- @blacklisted_mx_ip_addresses=["1.1.1.1", "2.2.2.2"],
280
- @dns=["10.0.0.1", "10.0.0.2:54"],
281
- @verifier_domain="somedomain.com",
282
- @verifier_email="verifier@example.com",
283
- @not_rfc_mx_lookup_flow=true,
284
- @smtp_port=2525,
285
- @smtp_fail_fast=true,
286
- @smtp_safe_check=true,
287
- @logger=#<Truemail::Logger:0x0000557f837450b0
288
- @event=:all, @file="/home/app/log/truemail.log", @stdout=true>>
289
- ```
290
-
291
- ##### Update global configuration
292
-
293
- ```ruby
294
- Truemail.configuration.connection_timeout = 3
295
- => 3
296
- Truemail.configuration.response_timeout = 4
297
- => 4
298
- Truemail.configuration.connection_attempts = 1
299
- => 1
300
-
301
- Truemail.configuration
302
- => #<Truemail::Configuration:0x000055590cb17b40
303
- @connection_timeout=3,
304
- @email_pattern=/regex_pattern/,
305
- @smtp_error_body_pattern=/regex_pattern/,
306
- @response_timeout=4,
307
- @connection_attempts=1,
308
- @default_validation_type=:mx,
309
- @validation_type_by_domain={"somedomain.com" => :regex, "otherdomain.com" => :mx},
310
- @whitelisted_emails=["user@somedomain1.com", "user@somedomain2.com"],
311
- @blacklisted_emails=["user@somedomain3.com", "user@somedomain4.com"],
312
- @whitelisted_domains=["somedomain1.com", "somedomain2.com"],
313
- @blacklisted_domains=["somedomain3.com", "somedomain4.com"],
314
- @whitelist_validation=true,
315
- @blacklisted_mx_ip_addresses=["1.1.1.1", "2.2.2.2"],
316
- @dns=["10.0.0.1", "10.0.0.2:54"],
317
- @verifier_domain="somedomain.com",
318
- @verifier_email="verifier@example.com",
319
- @not_rfc_mx_lookup_flow=true,
320
- @smtp_port=2525,
321
- @smtp_fail_fast=true,
322
- @smtp_safe_check=true,
323
- @logger=#<Truemail::Logger:0x0000557f837450b0
324
- @event=:all, @file="/home/app/log/truemail.log", @stdout=true>>
325
- ```
326
-
327
- ##### Reset global configuration
328
-
329
- Also you can reset Truemail configuration.
330
-
331
- ```ruby
332
- Truemail.reset_configuration!
333
- => nil
334
- Truemail.configuration
335
- => nil
336
- ```
337
-
338
- #### Using custom independent configuration
339
-
340
- Allows to use independent configuration for each validation/audition instance. When using this feature you do not need to have Truemail global configuration.
341
-
342
- ```ruby
343
- custom_configuration = Truemail::Configuration.new do |config|
344
- config.verifier_email = 'verifier@example.com'
345
- end
346
-
347
- Truemail.validate('email@example.com', custom_configuration: custom_configuration)
348
- Truemail.valid?('email@example.com', custom_configuration: custom_configuration)
349
- Truemail.host_audit('email@example.com', custom_configuration: custom_configuration)
350
- ```
351
-
352
- Please note, you should have global or custom configuration for use Truemail gem.
353
-
354
- ### Validation features
355
-
356
- #### Whitelist/Blacklist check
357
-
358
- Whitelist/Blacklist check is zero validation level. You can define white and black emails/domains lists. It means that validation of email which contains whitelisted email or domain always will return `true`, and for blacklisted email or domain will return `false`.
359
-
360
- Please note, other validations will not processed even if it was defined in `validation_type_for`.
361
-
362
- **Sequence of domain list check:**
363
-
364
- 1. Whitelist check
365
- 2. Whitelist validation check
366
- 3. Blacklist check
367
-
368
- Example of usage:
369
-
370
- ```ruby
371
- require 'truemail'
372
-
373
- Truemail.configure do |config|
374
- config.verifier_email = 'verifier@example.com'
375
- config.whitelisted_emails = %w[user@somedomain1.com user@somedomain2.com]
376
- config.blacklisted_emails = %w[user@somedomain3.com user@somedomain4.com]
377
- config.whitelisted_domains = %w[white-domain.com somedomain.com]
378
- config.blacklisted_domains = %w[black-domain.com somedomain.com]
379
- config.validation_type_for = { 'somedomain.com' => :mx }
380
- end
381
- ```
382
-
383
- ##### Whitelist case
384
-
385
- When email in whitelist, validation type will be redefined. Validation result returns ```true```
386
-
387
- ```ruby
388
- Truemail.validate('email@white-domain.com')
389
-
390
- #<Truemail::Validator:0x000055b8429f3490
391
- @result=#<struct Truemail::Validator::Result
392
- success=true,
393
- email="email@white-domain.com",
394
- domain="white-domain.com",
395
- mail_servers=[],
396
- errors={},
397
- smtp_debug=nil>,
398
- configuration=#<Truemail::Configuration:0x00005629f801bd28
399
- @whitelisted_emails=["user@somedomain1.com", "user@somedomain2.com"],
400
- @blacklisted_emails=["user@somedomain3.com", "user@somedomain4.com"],
401
- @blacklisted_domains=["black-domain.com", "somedomain.com"],
402
- @blacklisted_mx_ip_addresses=[],
403
- @dns=[],
404
- @connection_attempts=2,
405
- @connection_timeout=2,
406
- @default_validation_type=:smtp,
407
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
408
- @response_timeout=2,
409
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
410
- @not_rfc_mx_lookup_flow=false,
411
- @smtp_port=25,
412
- @smtp_fail_fast=false,
413
- @smtp_safe_check=false,
414
- @validation_type_by_domain={"somedomain.com"=>:mx},
415
- @verifier_domain="example.com",
416
- @verifier_email="verifier@example.com",
417
- @whitelist_validation=false,
418
- @whitelisted_domains=["white-domain.com", "somedomain.com"]>,
419
- @validation_type=:whitelist>
420
- ```
421
-
422
- ##### Whitelist validation case
423
-
424
- ```ruby
425
- require 'truemail'
426
-
427
- Truemail.configure do |config|
428
- config.verifier_email = 'verifier@example.com'
429
- config.whitelisted_domains = %w[white-domain.com]
430
- config.whitelist_validation = true
431
- end
432
- ```
433
-
434
- When email domain in whitelist and `whitelist_validation` is sets equal to `true` validation type will be passed to other validators. Validation of email which not contains whitelisted domain always will return `false`.
435
-
436
- ###### Email has whitelisted domain
437
-
438
- ```ruby
439
- Truemail.validate('email@white-domain.com', with: :regex)
440
-
441
- #<Truemail::Validator:0x000055b8429f3490
442
- @result=#<struct Truemail::Validator::Result
443
- success=true,
444
- email="email@white-domain.com",
445
- domain="white-domain.com",
446
- mail_servers=[],
447
- errors={},
448
- smtp_debug=nil>,
449
- configuration=
450
- #<Truemail::Configuration:0x0000563f0d2605c8
451
- @whitelisted_emails=[],
452
- @blacklisted_emails=[],
453
- @blacklisted_domains=[],
454
- @blacklisted_mx_ip_addresses=[],
455
- @dns=[],
456
- @connection_attempts=2,
457
- @connection_timeout=2,
458
- @default_validation_type=:smtp,
459
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
460
- @response_timeout=2,
461
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
462
- @not_rfc_mx_lookup_flow=false,
463
- @smtp_port=25,
464
- @smtp_fail_fast=false,
465
- @smtp_safe_check=false,
466
- @validation_type_by_domain={},
467
- @verifier_domain="example.com",
468
- @verifier_email="verifier@example.com",
469
- @whitelist_validation=true,
470
- @whitelisted_domains=["white-domain.com"]>,
471
- @validation_type=:regex>
472
- ```
473
-
474
- ###### Email hasn't whitelisted domain
475
-
476
- ```ruby
477
- Truemail.validate('email@domain.com', with: :regex)
478
-
479
- #<Truemail::Validator:0x000055b8429f3490
480
- @result=#<struct Truemail::Validator::Result
481
- success=false,
482
- email="email@domain.com",
483
- domain="domain.com",
484
- mail_servers=[],
485
- errors={},
486
- smtp_debug=nil>,
487
- configuration=
488
- #<Truemail::Configuration:0x0000563f0cd82ab0
489
- @whitelisted_emails=[],
490
- @blacklisted_emails=[],
491
- @blacklisted_domains=[],
492
- @blacklisted_mx_ip_addresses=[],
493
- @dns=[],
494
- @connection_attempts=2,
495
- @connection_timeout=2,
496
- @default_validation_type=:smtp,
497
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
498
- @response_timeout=2,
499
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
500
- @not_rfc_mx_lookup_flow=false,
501
- @smtp_port=25,
502
- @smtp_fail_fast=false,
503
- @smtp_safe_check=false,
504
- @validation_type_by_domain={},
505
- @verifier_domain="example.com",
506
- @verifier_email="verifier@example.com",
507
- @whitelist_validation=true,
508
- @whitelisted_domains=["white-domain.com"]>,
509
- @validation_type=:blacklist>
510
- ```
511
-
512
- ##### Blacklist case
513
-
514
- When email in blacklist, validation type will be redefined too. Validation result returns `false`.
515
-
516
- ```ruby
517
- Truemail.validate('email@black-domain.com')
518
-
519
- #<Truemail::Validator:0x000023y8429f3493
520
- @result=#<struct Truemail::Validator::Result
521
- success=false,
522
- email="email@black-domain.com",
523
- domain="black-domain.com",
524
- mail_servers=[],
525
- errors={},
526
- smtp_debug=nil>,
527
- configuration=
528
- #<Truemail::Configuration:0x0000563f0d36f4f0
529
- @whitelisted_emails=[],
530
- @blacklisted_emails=[],
531
- @blacklisted_domains=[],
532
- @blacklisted_mx_ip_addresses=[],
533
- @dns=[],
534
- @connection_attempts=2,
535
- @connection_timeout=2,
536
- @default_validation_type=:smtp,
537
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
538
- @response_timeout=2,
539
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
540
- @not_rfc_mx_lookup_flow=false,
541
- @smtp_port=25,
542
- @smtp_fail_fast=false,
543
- @smtp_safe_check=false,
544
- @validation_type_by_domain={},
545
- @verifier_domain="example.com",
546
- @verifier_email="verifier@example.com",
547
- @whitelist_validation=true,
548
- @whitelisted_domains=["white-domain.com"]>,
549
- @validation_type=:blacklist>
550
- ```
551
-
552
- ##### Duplication case
553
-
554
- Validation result for this email returns `true`, because it was found in whitelisted domains list first. Also `validation_type` for this case will be redefined.
555
-
556
- ```ruby
557
- Truemail.validate('email@somedomain.com')
558
-
559
- #<Truemail::Validator:0x000055b8429f3490
560
- @result=#<struct Truemail::Validator::Result
561
- success=true,
562
- email="email@somedomain.com",
563
- domain="somedomain.com",
564
- mail_servers=[],
565
- errors={},
566
- smtp_debug=nil>,
567
- configuration=
568
- #<Truemail::Configuration:0x0000563f0d3f8fc0
569
- @whitelisted_emails=[],
570
- @blacklisted_emails=[],
571
- @blacklisted_domains=[],
572
- @blacklisted_mx_ip_addresses=[],
573
- @dns=[],
574
- @connection_attempts=2,
575
- @connection_timeout=2,
576
- @default_validation_type=:smtp,
577
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
578
- @response_timeout=2,
579
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
580
- @not_rfc_mx_lookup_flow=false,
581
- @smtp_port=25,
582
- @smtp_fail_fast=false,
583
- @smtp_safe_check=false,
584
- @validation_type_by_domain={},
585
- @verifier_domain="example.com",
586
- @verifier_email="verifier@example.com",
587
- @whitelist_validation=true,
588
- @whitelisted_domains=["white-domain.com"]>,
589
- @validation_type=:whitelist>
590
- ```
591
-
592
- #### Regex validation
593
-
594
- Validation with regex pattern is the first validation level. It uses whitelist/blacklist check before running itself.
595
-
596
- ```code
597
- [Whitelist/Blacklist] -> [Regex validation]
598
- ```
599
-
600
- By default this validation not performs strictly following [RFC 5322](https://www.ietf.org/rfc/rfc5322.txt) standard, so you can override Truemail default regex pattern if you want.
601
-
602
- Example of usage:
603
-
604
- ##### With default regex pattern
605
-
606
- ```ruby
607
- require 'truemail'
608
-
609
- Truemail.configure do |config|
610
- config.verifier_email = 'verifier@example.com'
611
- end
612
-
613
- Truemail.validate('email@example.com', with: :regex)
614
-
615
- => #<Truemail::Validator:0x000055590cc9bdb8
616
- @result=
617
- #<struct Truemail::Validator::Result
618
- success=true,
619
- email="email@example.com",
620
- domain="example.com",
621
- mail_servers=[],
622
- errors={},
623
- smtp_debug=nil>,
624
- configuration=
625
- #<Truemail::Configuration:0x000055aa56a54d48
626
- @whitelisted_emails=[],
627
- @blacklisted_emails=[],
628
- @blacklisted_domains=[],
629
- @blacklisted_mx_ip_addresses=[],
630
- @dns=[],
631
- @connection_attempts=2,
632
- @connection_timeout=2,
633
- @default_validation_type=:smtp,
634
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
635
- @response_timeout=2,
636
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
637
- @not_rfc_mx_lookup_flow=false,
638
- @smtp_port=25,
639
- @smtp_fail_fast=false,
640
- @smtp_safe_check=false,
641
- @validation_type_by_domain={},
642
- @verifier_domain="example.com",
643
- @verifier_email="verifier@example.com",
644
- @whitelist_validation=false,
645
- @whitelisted_domains=[]>,
646
- @validation_type=:regex>
647
- ```
648
-
649
- ##### With custom regex pattern
650
-
651
- You should define your custom regex pattern in a gem configuration before.
652
-
653
- ```ruby
654
- require 'truemail'
655
-
656
- Truemail.configure do |config|
657
- config.verifier_email = 'verifier@example.com'
658
- config.email_pattern = /regex_pattern/
659
- end
660
-
661
- Truemail.validate('email@example.com', with: :regex)
662
-
663
- => #<Truemail::Validator:0x000055590ca8b3e8
664
- @result=
665
- #<struct Truemail::Validator::Result
666
- success=true,
667
- email="email@example.com",
668
- domain="example.com",
669
- mail_servers=[],
670
- errors={},
671
- smtp_debug=nil>,
672
- configuration=
673
- #<Truemail::Configuration:0x0000560e58d80830
674
- @whitelisted_emails=[],
675
- @blacklisted_emails=[],
676
- @blacklisted_domains=[],
677
- @blacklisted_mx_ip_addresses=[],
678
- @dns=[],
679
- @connection_attempts=2,
680
- @connection_timeout=2,
681
- @default_validation_type=:smtp,
682
- @email_pattern=/regex_pattern/,
683
- @response_timeout=2,
684
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
685
- @not_rfc_mx_lookup_flow=false,
686
- @smtp_port=25,
687
- @smtp_fail_fast=false,
688
- @smtp_safe_check=false,
689
- @validation_type_by_domain={},
690
- @verifier_domain="example.com",
691
- @verifier_email="verifier@example.com",
692
- @whitelist_validation=false,
693
- @whitelisted_domains=[]>,
694
- @validation_type=:regex>
695
- ```
696
-
697
- #### MX validation
698
-
699
- In fact it's DNS validation because it checks not MX records only. DNS validation is the second validation level, historically named as MX validation. It uses Regex validation before running itself. When regex validation has completed successfully then runs itself.
700
-
701
- ```code
702
- [Whitelist/Blacklist] -> [Regex validation] -> [MX validation]
703
- ```
704
-
705
- Please note, Truemail MX validator [not performs](https://github.com/truemail-rb/truemail/issues/26) strict compliance of the [RFC 5321](https://tools.ietf.org/html/rfc5321#section-5) standard for best validation outcome.
706
-
707
- ##### RFC MX lookup flow
708
-
709
- [Truemail MX lookup](https://slides.com/vladislavtrotsenko/truemail#/0/9) based on RFC 5321. It consists of 3 substeps: MX, CNAME and A record resolvers. The point of each resolver is attempt to extract the mail servers from email domain. If at least one server exists that validation is successful. Iteration is processing until resolver returns true.
710
-
711
- Example of usage:
712
-
713
- ```ruby
714
- require 'truemail'
715
-
716
- Truemail.configure do |config|
717
- config.verifier_email = 'verifier@example.com'
718
- end
719
-
720
- Truemail.validate('email@example.com', with: :mx)
721
-
722
- => #<Truemail::Validator:0x000055590c9c1c50
723
- @result=
724
- #<struct Truemail::Validator::Result
725
- success=true,
726
- email="email@example.com",
727
- domain="example.com",
728
- mail_servers=["127.0.1.1", "127.0.1.2"],
729
- errors={},
730
- smtp_debug=nil>,
731
- configuration=
732
- #<Truemail::Configuration:0x0000559b6e44af70
733
- @whitelisted_emails=[],
734
- @blacklisted_emails=[],
735
- @blacklisted_domains=[],
736
- @blacklisted_mx_ip_addresses=[],
737
- @dns=[],
738
- @connection_attempts=2,
739
- @connection_timeout=2,
740
- @default_validation_type=:smtp,
741
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
742
- @response_timeout=2,
743
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
744
- @not_rfc_mx_lookup_flow=false,
745
- @smtp_port=25,
746
- @smtp_fail_fast=false,
747
- @smtp_safe_check=false,
748
- @validation_type_by_domain={},
749
- @verifier_domain="example.com",
750
- @verifier_email="verifier@example.com",
751
- @whitelist_validation=false,
752
- @whitelisted_domains=[]>,
753
- @validation_type=:mx>
754
- ```
755
-
756
- ##### Not RFC MX lookup flow
757
-
758
- Also Truemail has possibility to use not RFC MX lookup flow. It means that will be used only one MX resolver on the DNS validation layer. By default this option is disabled.
759
-
760
- Example of usage:
761
-
762
- ```ruby
763
- require 'truemail'
764
-
765
- Truemail.configure do |config|
766
- config.verifier_email = 'verifier@example.com'
767
- config.not_rfc_mx_lookup_flow = true
768
- end
769
-
770
- Truemail.validate('email@example.com', with: :mx)
771
-
772
- => #<Truemail::Validator:0x000055590c9c1c50
773
- @result=
774
- #<struct Truemail::Validator::Result
775
- success=true,
776
- email="email@example.com",
777
- domain="example.com",
778
- mail_servers=["127.0.1.1", "127.0.1.2"],
779
- errors={},
780
- smtp_debug=nil>,
781
- configuration=
782
- #<Truemail::Configuration:0x0000559b6e44af70
783
- @whitelisted_emails=[],
784
- @blacklisted_emails=[],
785
- @blacklisted_domains=[],
786
- @blacklisted_mx_ip_addresses=[],
787
- @dns=[],
788
- @connection_attempts=2,
789
- @connection_timeout=2,
790
- @default_validation_type=:smtp,
791
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
792
- @response_timeout=2,
793
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
794
- @not_rfc_mx_lookup_flow=true,
795
- @smtp_port=25,
796
- @smtp_fail_fast=false,
797
- @smtp_safe_check=false,
798
- @validation_type_by_domain={},
799
- @verifier_domain="example.com",
800
- @verifier_email="verifier@example.com",
801
- @whitelist_validation=false,
802
- @whitelisted_domains=[]>,
803
- @validation_type=:mx>
804
- ```
805
-
806
- #### MX blacklist validation
807
-
808
- MX blacklist validation is the third validation level. This layer provides checking extracted mail server(s) IP address from MX validation with predefined blacklisted IP addresses list. It can be used as a part of DEA ([disposable email address](https://en.wikipedia.org/wiki/Disposable_email_address)) validations.
809
-
810
- ```code
811
- [Whitelist/Blacklist] -> [Regex validation] -> [MX validation] -> [MX blacklist validation]
812
- ```
813
-
814
- Example of usage:
815
-
816
- ```ruby
817
- require 'truemail'
818
-
819
- Truemail.configure do |config|
820
- config.verifier_email = 'verifier@example.com'
821
- config.blacklisted_mx_ip_addresses = %w[127.0.1.2]
822
- end
823
-
824
- Truemail.validate('email@example.com', with: :mx_blacklist)
825
-
826
- => #<Truemail::Validator:0x00007fca0c8aea70
827
- @result=
828
- #<struct Truemail::Validator::Result
829
- success=false,
830
- email="email@example.com",
831
- domain="example.com",
832
- mail_servers=["127.0.1.1", "127.0.1.2"],
833
- errors={:mx_blacklist=>"blacklisted mx server ip address"},
834
- smtp_debug=nil,
835
- configuration=
836
- #<Truemail::Configuration:0x00007fca0c8aeb38
837
- @whitelisted_emails=[],
838
- @blacklisted_emails=[],
839
- @blacklisted_domains=[],
840
- @blacklisted_mx_ip_addresses=["127.0.1.2"],
841
- @connection_attempts=2,
842
- @connection_timeout=2,
843
- @default_validation_type=:smtp,
844
- @dns=[],
845
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-.+]*)@((?i-mx:[\p{L}0-9]+([\-.]{1}[\p{L}0-9]+)*\.\p{L}{2,63}))\z)/,
846
- @not_rfc_mx_lookup_flow=false,
847
- @response_timeout=2,
848
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
849
- @smtp_port=25,
850
- @smtp_fail_fast=false,
851
- @smtp_safe_check=false,
852
- @validation_type_by_domain={},
853
- @verifier_domain="example.com",
854
- @verifier_email="verifier@example.com",
855
- @whitelist_validation=false,
856
- @whitelisted_domains=[]>>,
857
- @validation_type=:mx_blacklist>
858
- ```
859
-
860
- #### SMTP validation
861
-
862
- SMTP validation is a final, fourth validation level. This type of validation tries to check real existence of email account on a current email server. This validation runs a chain of previous validations and if they're complete successfully then runs itself.
863
-
864
- ```code
865
- [Whitelist/Blacklist] -> [Regex validation] -> [MX validation] -> [MX blacklist validation] -> [SMTP validation]
866
- ```
867
-
868
- If total count of MX servers is equal to one, `Truemail::Smtp` validator will use value from `Truemail.configuration.connection_attempts` as connection attempts. By default it's equal `2`.
869
-
870
- By default, you don't need pass with-parameter to use it. Example of usage is specified below:
871
-
872
- ##### SMTP fail fast enabled
873
-
874
- Truemail can use fail fast behaviour for SMTP validation layer. When `smtp_fail_fast = true` it means that `truemail` ends smtp validation session after first attempt on the first mx server in any fail cases (network connection/timeout error, smtp validation error). This feature helps to reduce total time of SMTP validation session up to 1 second.
875
-
876
- ```ruby
877
- require 'truemail'
878
-
879
- Truemail.configure do |config|
880
- config.verifier_email = 'verifier@example.com'
881
- config.smtp_fail_fast = true
882
- end
883
-
884
- Truemail.validate('email@example.com')
885
-
886
- # SMTP validation failed, smtp fail fast validation scenario
887
- => #<Truemail::Validator:0x00007fdc4504f460
888
- @result=
889
- #<struct Truemail::Validator::Result
890
- success=false,
891
- email="email@example.com",
892
- domain="example.com",
893
- mail_servers=["127.0.1.1", "127.0.1.2", "127.0.1.3"], # there are 3 mail servers in a row
894
- errors={:smtp=>"smtp error"},
895
- smtp_debug=
896
- [#<Truemail::Validate::Smtp::Request:0x00007fdc43150b90 # but iteration has been stopped after the first failure
897
- @attempts=nil,
898
- @configuration=
899
- #<Truemail::Validate::Smtp::Request::Configuration:0x00007fdc43150b18
900
- @connection_timeout=2,
901
- @response_timeout=2,
902
- @verifier_domain="example.com",
903
- @verifier_email="verifier@example.com">,
904
- @email="email@example.com",
905
- @host="127.0.1.1",
906
- @response=
907
- #<struct Truemail::Validate::Smtp::Response
908
- port_opened=false,
909
- connection=nil,
910
- helo=nil,
911
- mailfrom=nil,
912
- rcptto=nil,
913
- errors={}>>],
914
- configuration=
915
- #<Truemail::Configuration:0x00007fdc4504f5c8
916
- @whitelisted_emails=[],
917
- @blacklisted_emails=[],
918
- @blacklisted_domains=[],
919
- @blacklisted_mx_ip_addresses=[],
920
- @dns=[],
921
- @connection_attempts=2,
922
- @connection_timeout=2,
923
- @default_validation_type=:smtp,
924
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-.+]*)@((?i-mx:[\p{L}0-9]+([\-.]{1}[\p{L}0-9]+)*\.\p{L}{2,63}))\z)/,
925
- @not_rfc_mx_lookup_flow=false,
926
- @response_timeout=2,
927
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
928
- @smtp_port=25,
929
- @smtp_fail_fast=true,
930
- @smtp_safe_check=false,
931
- @validation_type_by_domain={},
932
- @verifier_domain="example.com",
933
- @verifier_email="verifier@example.com",
934
- @whitelist_validation=false,
935
- @whitelisted_domains=[]>>,
936
- @validation_type=:smtp>
937
- ```
938
-
939
- ##### SMTP safe check disabled
940
-
941
- With `smtp_safe_check = false`
942
-
943
- ```ruby
944
- require 'truemail'
945
-
946
- Truemail.configure do |config|
947
- config.verifier_email = 'verifier@example.com'
948
- end
949
-
950
- Truemail.validate('email@example.com')
951
-
952
- # Successful SMTP validation
953
- => #<Truemail::Validator:0x000055590c4dc118
954
- @result=
955
- #<struct Truemail::Validator::Result
956
- success=true,
957
- email="email@example.com",
958
- domain="example.com",
959
- mail_servers=["127.0.1.1", "127.0.1.2"],
960
- errors={},
961
- smtp_debug=nil>,
962
- configuration=
963
- #<Truemail::Configuration:0x00005615e87b9298
964
- @whitelisted_emails=[],
965
- @blacklisted_emails=[],
966
- @blacklisted_domains=[],
967
- @blacklisted_mx_ip_addresses=[],
968
- @dns=[],
969
- @connection_attempts=2,
970
- @connection_timeout=2,
971
- @default_validation_type=:smtp,
972
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
973
- @response_timeout=2,
974
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
975
- @not_rfc_mx_lookup_flow=false,
976
- @smtp_fail_fast=false,
977
- @smtp_safe_check=false,
978
- @validation_type_by_domain={},
979
- @verifier_domain="example.com",
980
- @verifier_email="verifier@example.com",
981
- @whitelist_validation=false,
982
- @whitelisted_domains=[]>,
983
- @validation_type=:smtp>
984
-
985
- # SMTP validation failed
986
- => #<Truemail::Validator:0x0000000002d5cee0
987
- @result=
988
- #<struct Truemail::Validator::Result
989
- success=false,
990
- email="email@example.com",
991
- domain="example.com",
992
- mail_servers=["127.0.1.1", "127.0.1.2"],
993
- errors={:smtp=>"smtp error"},
994
- smtp_debug=
995
- [#<Truemail::Validate::Smtp::Request:0x0000000002d49b10
996
- @configuration=
997
- #<Truemail::Validate::Smtp::Request::Configuration:0x00005615e8d21848
998
- @connection_timeout=2,
999
- @response_timeout=2,
1000
- @verifier_domain="example.com",
1001
- @verifier_email="verifier@example.com">,
1002
- @email="email@example.com",
1003
- @host="127.0.1.1",
1004
- @attempts=nil,
1005
- @response=
1006
- #<struct Truemail::Validate::Smtp::Response
1007
- port_opened=true,
1008
- connection=true,
1009
- helo=true,
1010
- mailfrom=
1011
- #<Net::SMTP::Response:0x0000000002d5a618
1012
- @status="250",
1013
- @string="250 OK\n">,
1014
- rcptto=false,
1015
- errors={:rcptto=>"550 User not found\n"}>>]>,
1016
- configuration=
1017
- #<Truemail::Configuration:0x00005615e87b9298
1018
- @whitelisted_emails=[],
1019
- @blacklisted_emails=[],
1020
- @blacklisted_domains=[],
1021
- @blacklisted_mx_ip_addresses=[],
1022
- @dns=[],
1023
- @connection_attempts=2,
1024
- @connection_timeout=2,
1025
- @default_validation_type=:smtp,
1026
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
1027
- @response_timeout=2,
1028
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
1029
- @not_rfc_mx_lookup_flow=false,
1030
- @smtp_port=25,
1031
- @smtp_fail_fast=false,
1032
- @smtp_safe_check=false,
1033
- @validation_type_by_domain={},
1034
- @verifier_domain="example.com",
1035
- @verifier_email="verifier@example.com",
1036
- @whitelist_validation=false,
1037
- @whitelisted_domains=[]>,
1038
- @validation_type=:smtp>
1039
- ```
1040
-
1041
- ##### SMTP safe check enabled
1042
-
1043
- With `smtp_safe_check = true`
1044
-
1045
- ```ruby
1046
- require 'truemail'
1047
-
1048
- Truemail.configure do |config|
1049
- config.verifier_email = 'verifier@example.com'
1050
- config.smtp_safe_check = true
1051
- end
1052
-
1053
- Truemail.validate('email@example.com')
1054
-
1055
- # Successful SMTP validation
1056
- => #<Truemail::Validator:0x0000000002ca2c70
1057
- @result=
1058
- #<struct Truemail::Validator::Result
1059
- success=true,
1060
- email="email@example.com",
1061
- domain="example.com",
1062
- mail_servers=["127.0.1.1", "127.0.1.2"],
1063
- errors={},
1064
- smtp_debug=
1065
- [#<Truemail::Validate::Smtp::Request:0x0000000002c95d40
1066
- @configuration=
1067
- #<Truemail::Validate::Smtp::Request::Configuration:0x00005615e8d21848
1068
- @connection_timeout=2,
1069
- @response_timeout=2,
1070
- @verifier_domain="example.com",
1071
- @verifier_email="verifier@example.com">,
1072
- @email="email@example.com",
1073
- @host="127.0.1.1",
1074
- @attempts=nil,
1075
- @response=
1076
- #<struct Truemail::Validate::Smtp::Response
1077
- port_opened=true,
1078
- connection=false,
1079
- helo=true,
1080
- mailfrom=false,
1081
- rcptto=nil,
1082
- errors={:mailfrom=>"554 5.7.1 Client host blocked\n", :connection=>"server dropped connection after response"}>>,]>,
1083
- configuration=
1084
- #<Truemail::Configuration:0x00005615e87b9298
1085
- @whitelisted_emails=[],
1086
- @blacklisted_emails=[],
1087
- @blacklisted_domains=[],
1088
- @blacklisted_mx_ip_addresses=[],
1089
- @dns=[],
1090
- @connection_attempts=2,
1091
- @connection_timeout=2,
1092
- @default_validation_type=:smtp,
1093
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
1094
- @response_timeout=2,
1095
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
1096
- @not_rfc_mx_lookup_flow=false,
1097
- @smtp_fail_fast=false,
1098
- @smtp_safe_check=false,
1099
- @validation_type_by_domain={},
1100
- @verifier_domain="example.com",
1101
- @verifier_email="verifier@example.com",
1102
- @whitelist_validation=false,
1103
- @whitelisted_domains=[]>,
1104
- @validation_type=:smtp>
1105
-
1106
- # SMTP validation failed
1107
- => #<Truemail::Validator:0x0000000002d5cee0
1108
- @result=
1109
- #<struct Truemail::Validator::Result
1110
- success=false,
1111
- email="email@example.com",
1112
- domain="example.com",
1113
- mail_servers=["127.0.1.1", "127.0.1.2"],
1114
- errors={:smtp=>"smtp error"},
1115
- smtp_debug=
1116
- [#<Truemail::Validate::Smtp::Request:0x0000000002d49b10
1117
- @configuration=
1118
- #<Truemail::Validate::Smtp::Request::Configuration:0x00005615e8d21848
1119
- @connection_timeout=2,
1120
- @response_timeout=2,
1121
- @verifier_domain="example.com",
1122
- @verifier_email="verifier@example.com">,
1123
- @email="email@example.com",
1124
- @host="127.0.1.1",
1125
- @attempts=nil,
1126
- @response=
1127
- #<struct Truemail::Validate::Smtp::Response
1128
- port_opened=true,
1129
- connection=true,
1130
- helo=true,
1131
- mailfrom=#<Net::SMTP::Response:0x0000000002d5a618 @status="250", @string="250 OK\n">,
1132
- rcptto=false,
1133
- errors={:rcptto=>"550 User not found\n"}>>]>,
1134
- configuration=
1135
- #<Truemail::Configuration:0x00005615e87b9298
1136
- @whitelisted_emails=[],
1137
- @blacklisted_emails=[],
1138
- @blacklisted_domains=[],
1139
- @blacklisted_mx_ip_addresses=[],
1140
- @dns=[],
1141
- @connection_attempts=2,
1142
- @connection_timeout=2,
1143
- @default_validation_type=:smtp,
1144
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
1145
- @response_timeout=2,
1146
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
1147
- @not_rfc_mx_lookup_flow=false,
1148
- @smtp_port=25,
1149
- @smtp_fail_fast=false,
1150
- @smtp_safe_check=false,
1151
- @validation_type_by_domain={},
1152
- @verifier_domain="example.com",
1153
- @verifier_email="verifier@example.com",
1154
- @whitelist_validation=false,
1155
- @whitelisted_domains=[]>,
1156
- @validation_type=:smtp>
1157
- ```
1158
-
1159
- ### Host audit features
1160
-
1161
- Truemail gem allows performing an audit of the host in which runs. It will help to detect common host problems interfering to proper email verification.
1162
-
1163
- #### IP audit
1164
-
1165
- Checks is current Truemail host has proper internet connection and detects current host ip address.
1166
-
1167
- #### DNS audit
1168
-
1169
- Checks is verifier domain refer to current Truemail host IP address.
1170
-
1171
- #### PTR audit
1172
-
1173
- So what is a PTR record? A PTR record, or pointer record, enables someone to perform a reverse DNS lookup. This allows them to determine your domain name based on your IP address. Because generic domain names without a PTR are often associated with spammers, incoming mail servers identify email from hosts without PTR records as spam and you can't verify yours emails qualitatively.
1174
-
1175
- Checks is PTR record exists for your Truemail host ip address exists and refers to current verifier domain.
1176
-
1177
- #### Example of using
1178
-
1179
- ```ruby
1180
- Truemail.host_audit
1181
- # Everything is good
1182
- => #<Truemail::Auditor:0x00005580df358828
1183
- @result=
1184
- #<struct Truemail::Auditor::Result
1185
- current_host_ip="127.0.0.1",
1186
- warnings={}>,
1187
- configuration=
1188
- #<Truemail::Configuration:0x00005615e86327a8
1189
- @whitelisted_emails=[],
1190
- @blacklisted_emails=[],
1191
- @blacklisted_domains=[],
1192
- @blacklisted_mx_ip_addresses=[],
1193
- @dns=[],
1194
- @connection_attempts=2,
1195
- @connection_timeout=2,
1196
- @default_validation_type=:smtp,
1197
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
1198
- @response_timeout=2,
1199
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
1200
- @not_rfc_mx_lookup_flow=false,
1201
- @smtp_port=25,
1202
- @smtp_fail_fast=false,
1203
- @smtp_safe_check=false,
1204
- @validation_type_by_domain={},
1205
- @verifier_domain="example.com",
1206
- @verifier_email="verifier@example.com",
1207
- @whitelist_validation=false,
1208
- @whitelisted_domains=[]>
1209
-
1210
- # Has audit warnings
1211
- => #<Truemail::Auditor:0x00005580df358828
1212
- @result=
1213
- #<struct Truemail::Auditor::Result
1214
- current_host_ip="127.0.0.1",
1215
- warnings={
1216
- :dns=>"A-record of verifier domain not refers to current host ip address",
1217
- :ptr=>"PTR-record does not reference to current verifier domain"
1218
- },
1219
- configuration=
1220
- #<Truemail::Configuration:0x00005615e86327a8
1221
- @whitelisted_emails=[],
1222
- @blacklisted_emails=[],
1223
- @blacklisted_domains=[],
1224
- @blacklisted_mx_ip_addresses=[],
1225
- @dns=[],
1226
- @connection_attempts=2,
1227
- @connection_timeout=2,
1228
- @default_validation_type=:smtp,
1229
- @email_pattern=/(?=\A.{6,255}\z)(\A([\p{L}0-9]+[\w|\-|\.|\+]*)@((?i-mx:[\p{L}0-9]+([\-\.]{1}[\p{L}0-9]+)*\.[\p{L}]{2,63}))\z)/,
1230
- @response_timeout=2,
1231
- @smtp_error_body_pattern=/(?=.*550)(?=.*(user|account|customer|mailbox)).*/i,
1232
- @not_rfc_mx_lookup_flow=false,
1233
- @smtp_port=25,
1234
- @smtp_fail_fast=false,
1235
- @smtp_safe_check=false,
1236
- @validation_type_by_domain={},
1237
- @verifier_domain="example.com",
1238
- @verifier_email="verifier@example.com",
1239
- @whitelist_validation=false,
1240
- @whitelisted_domains=[]>
1241
- ```
1242
-
1243
- ### Event logger
1244
-
1245
- Truemail gem allows to output tracking events to stdout/file or both of these. Please note, at least one of the outputs must exist. Tracking event by default is `:error`
1246
-
1247
- ```ruby
1248
- Truemail.configure do |config|
1249
- config.logger = { tracking_event: :all, stdout: true, log_absolute_path: '/home/app/log/truemail.log' }
1250
- end
1251
- ```
1252
-
1253
- #### Available tracking events
1254
-
1255
- - `:all`, all detected events including success validation cases
1256
- - `:unrecognized_error`, unrecognized errors only (when `smtp_safe_check = true` and SMTP server does not return an exact answer that the email does not exist)
1257
- - `:recognized_error`, recognized errors only
1258
- - `:error`, recognized and unrecognized errors only
1259
-
1260
- ### JSON serializers
1261
-
1262
- Truemail has built in JSON serializers for `Truemail::Auditor` and `Truemail::Validator` instances, so you can represent your host audition or email validation result as json. Also you can use [#as_json](#as_json) helper for shortcuting.
1263
-
1264
- #### Auditor JSON serializer
1265
-
1266
- ```ruby
1267
- Truemail::Log::Serializer::AuditorJson.call(Truemail.host_audit)
1268
-
1269
- =>
1270
- # Serialized Truemail::Auditor instance
1271
- {
1272
- "date": "2020-08-31 22:33:43 +0300",
1273
- "current_host_ip": "127.0.0.1",
1274
- "warnings": {
1275
- "dns": "A-record of verifier domain not refers to current host ip address", "ptr": "PTR-record does not reference to current verifier domain"
1276
- },
1277
- "configuration": {
1278
- "whitelisted_emails": null,
1279
- "blacklisted_emails": null,
1280
- "blacklisted_domains": null,
1281
- "blacklisted_mx_ip_addresses": null,
1282
- "dns": null,
1283
- "email_pattern": "default gem value",
1284
- "not_rfc_mx_lookup_flow": false,
1285
- "smtp_error_body_pattern": "default gem value",
1286
- "smtp_fail_fast": false,
1287
- "smtp_safe_check": false,
1288
- "validation_type_by_domain": null,
1289
- "whitelist_validation": false,
1290
- "whitelisted_domains": null
1291
- }
1292
- }
1293
- ```
1294
-
1295
- #### Validator JSON serializer
1296
-
1297
- ```ruby
1298
- Truemail::Log::Serializer::ValidatorJson.call(Truemail.validate('nonexistent_email@bestweb.com.ua'))
1299
-
1300
- =>
1301
- # Serialized Truemail::Validator instance
1302
- {
1303
- "date": "2019-10-28 10:15:51 +0200",
1304
- "email": "nonexistent_email@bestweb.com.ua",
1305
- "validation_type": "smtp",
1306
- "success": false,
1307
- "errors": {
1308
- "smtp": "smtp error"
1309
- },
1310
- "smtp_debug": [
1311
- {
1312
- "mail_host": "213.180.193.89",
1313
- "port_opened": true,
1314
- "connection": true,
1315
- "errors": {
1316
- "rcptto": "550 5.7.1 No such user!\n"
1317
- }
1318
- }
1319
- ],
1320
- "configuration": {
1321
- "whitelisted_emails": null,
1322
- "blacklisted_emails": null,
1323
- "blacklisted_domains": null,
1324
- "blacklisted_mx_ip_addresses": null,
1325
- "dns": null,
1326
- "email_pattern": "default gem value",
1327
- "not_rfc_mx_lookup_flow": false,
1328
- "smtp_error_body_pattern": "default gem value",
1329
- "smtp_fail_fast": false,
1330
- "smtp_safe_check": false,
1331
- "validation_type_by_domain": null,
1332
- "whitelist_validation": false,
1333
- "whitelisted_domains": null
1334
- }
1335
- }
1336
- ```
1337
-
1338
- ### Truemail helpers
1339
-
1340
- #### .valid?
1341
-
1342
- You can use the `.valid?` helper for quick validation of email address. It returns a boolean:
1343
-
1344
- ```ruby
1345
- # It is shortcut for Truemail.validate('email@example.com').result.valid?
1346
- Truemail.valid?('email@example.com')
1347
- => true
1348
- ```
1349
-
1350
- #### #as_json
1351
-
1352
- You can use `#as_json` helper for represent `Truemail::Auditor` or `Truemail::Validator` instances as json. Under the hood it uses internal json `Truemail::Log::Serializer::AuditorJson` and `Truemail::Log::Serializer::ValidatorJson` [serializers](#json-serializers):
1353
-
1354
- ```ruby
1355
- Truemail.host_audit.as_json
1356
-
1357
- =>
1358
- # Serialized Truemail::Auditor instance
1359
- {
1360
- "date": "2020-08-31 22:33:43 +0300",
1361
- "current_host_ip": "127.0.0.1",
1362
- "warnings": {
1363
- "dns": "A-record of verifier domain not refers to current host ip address", "ptr": "PTR-record does not reference to current verifier domain"
1364
- },
1365
- "configuration": {
1366
- "whitelisted_emails": null,
1367
- "blacklisted_emails": null,
1368
- "blacklisted_domains": null,
1369
- "blacklisted_mx_ip_addresses": null,
1370
- "dns": null,
1371
- "email_pattern": "default gem value",
1372
- "not_rfc_mx_lookup_flow": false,
1373
- "smtp_error_body_pattern": "default gem value",
1374
- "smtp_fail_fast": false,
1375
- "smtp_safe_check": false,
1376
- "validation_type_by_domain": null,
1377
- "whitelist_validation": false,
1378
- "whitelisted_domains": null
1379
- }
1380
- }
1381
-
1382
-
1383
- Truemail.validate('nonexistent_email@bestweb.com.ua').as_json
1384
-
1385
- =>
1386
- # Serialized Truemail::Validator instance
1387
- {
1388
- "date": "2020-05-10 10:00:00 +0200",
1389
- "email": "nonexistent_email@bestweb.com.ua",
1390
- "validation_type": "smtp",
1391
- "success": false,
1392
- "errors": {
1393
- "smtp": "smtp error"
1394
- },
1395
- "smtp_debug": [
1396
- {
1397
- "mail_host": "213.180.193.89",
1398
- "port_opened": true,
1399
- "connection": true,
1400
- "errors": {
1401
- "rcptto": "550 5.7.1 No such user!\n"
1402
- }
1403
- }
1404
- ],
1405
- "configuration": {
1406
- "whitelisted_emails": null,
1407
- "blacklisted_emails": null,
1408
- "blacklisted_domains": null,
1409
- "blacklisted_mx_ip_addresses": null,
1410
- "dns": null,
1411
- "email_pattern": "default gem value",
1412
- "not_rfc_mx_lookup_flow": false,
1413
- "smtp_error_body_pattern": "default gem value",
1414
- "smtp_fail_fast": false,
1415
- "smtp_safe_check": false,
1416
- "validation_type_by_domain": null,
1417
- "whitelist_validation": false,
1418
- "whitelisted_domains": null
1419
- }
1420
- }
1421
- ```
1422
-
1423
- ### Test environment
1424
-
1425
- You can stub out that validation for your test environment. Just add RSpec before action:
1426
-
1427
- ```ruby
1428
- # spec_helper.rb
1429
-
1430
- RSpec.configure do |config|
1431
- config.before { allow(Truemail).to receive(:valid?).and_return(true) }
1432
- # or
1433
- config.before { allow(Truemail).to receive(:validate).and_return(true) }
1434
- # or
1435
- config.before { allow(Truemail).to receive_message_chain(:validate, :result, :valid?).and_return(true) }
1436
- end
1437
- ```
1438
-
1439
- Or with [whitelist/blacklist Truemail feature](#whitelistblacklist-check) you can define validation behavior for test and staging environment:
1440
-
1441
- ```ruby
1442
- # config/initializers/truemail.rb
1443
-
1444
- Truemail.configure do |config|
1445
- config.verifier_email = Rails.configuration.default_sender_email
1446
-
1447
- unless Rails.env.production?
1448
- config.whitelisted_domains = Constants::Email::WHITE_DOMAINS
1449
- config.blacklisted_domains = Constants::Email::BLACK_DOMAINS
1450
- end
1451
- end
1452
- ```
1453
-
1454
- Or you can use end-to-end approach and mock DNS and SMTP services with [`dns_mock`](https://github.com/mocktools/ruby-dns-mock), [`smtp_mock`](https://github.com/mocktools/ruby-smtp-mock) for your test environment like in example below:
1455
-
1456
- ```ruby
1457
- # Gemfile
1458
-
1459
- group :test do
1460
- gem 'dns_mock', require: false
1461
- gem 'smtp_mock', require: false
1462
- end
1463
-
1464
- # spec/spec_helper.rb
1465
-
1466
- require 'dns_mock/test_framework/rspec'
1467
- require 'smtp_mock/test_framework/rspec'
1468
-
1469
- RSpec.configure do |config|
1470
- config.include DnsMock::TestFramework::RSpec::Helper
1471
- config.include SmtpMock::TestFramework::RSpec::Helper
1472
- end
1473
-
1474
- # spec/integration_spec.rb
1475
-
1476
- RSpec.describe 'integration tests' do
1477
- let(:target_email) { random_email }
1478
- let(:dns_mock_records) { dns_mock_records_by_email(target_email, dimension: 2) }
1479
-
1480
- before do
1481
- dns_mock_server.assign_mocks(dns_mock_records)
1482
- smtp_mock_server(**smtp_mock_server_options)
1483
-
1484
- Truemail.configuration.tap do |config|
1485
- config.dns = %W[127.0.0.1:#{dns_mock_server.port}]
1486
- config.smtp_port = smtp_mock_server.port
1487
- end
1488
- end
1489
-
1490
- context 'when checks real email' do
1491
- let(:smtp_mock_server_options) { {} }
1492
-
1493
- it { expect(Truemail.validate(target_email).result).to be_valid }
1494
- end
1495
-
1496
- context 'when checks fake email' do
1497
- let(:smtp_mock_server_options) { { not_registered_emails: [target_email] } }
1498
-
1499
- it { expect(Truemail.validate(target_email).result).not_to be_valid }
1500
- end
1501
- end
1502
- ```
1503
-
1504
- ---
1505
-
1506
- ## Truemail family
1507
-
1508
- All Truemail solutions: https://truemail-rb.org
1509
-
1510
- | Name | Type | Description |
1511
- | --- | --- | --- |
1512
- | [truemail-go](https://github.com/truemail-rb/truemail-go) | go package | Configurable Golang email validator, main core |
1513
- | [truemail server](https://github.com/truemail-rb/truemail-rack) | ruby app | Lightweight rack based web API wrapper for Truemail gem |
1514
- | [truemail-rack-docker](https://github.com/truemail-rb/truemail-rack-docker-image) | docker image | Lightweight rack based web API [dockerized image](https://hub.docker.com/r/truemail/truemail-rack) :whale: of Truemail server |
1515
- | [truemail-ruby-client](https://github.com/truemail-rb/truemail-ruby-client) | ruby gem | Web API Ruby client for Truemail Server |
1516
- | [truemail-crystal-client](https://github.com/truemail-rb/truemail-crystal-client) | crystal shard | Web API Crystal client for Truemail Server |
1517
- | [truemail-java-client](https://github.com/truemail-rb/truemail-java-client) | java lib | Web API Java client for Truemail Server |
1518
- | [truemail-rspec](https://github.com/truemail-rb/truemail-rspec) | ruby gem | Truemail configuration, auditor and validator RSpec helpers |
1519
-
1520
- ## Contributing
1521
-
1522
- Bug reports and pull requests are welcome on GitHub at https://github.com/truemail-rb/truemail. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. Please check the [open tickets](https://github.com/truemail-rb/truemail/issues). Be sure to follow Contributor Code of Conduct below and our [Contributing Guidelines](CONTRIBUTING.md).
1523
-
1524
- ## License
1525
-
1526
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
1527
-
1528
- ## Code of Conduct
1529
-
1530
- Everyone interacting in the Truemail project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
1531
-
1532
- ## Credits
1533
-
1534
- - [The Contributors](https://github.com/truemail-rb/truemail/graphs/contributors) for code and awesome suggestions
1535
- - [The Stargazers](https://github.com/truemail-rb/truemail/stargazers) for showing their support
1536
-
1537
- ## Versioning
1538
-
1539
- Truemail uses [Semantic Versioning 2.0.0](https://semver.org)