मेलिंग सूचियों के सत्यापन के लिए। (मैं रेल 4.1.6 का उपयोग करता हूं)
मुझे here से मेरा regexp मिला। यह एक बहुत ही पूर्ण प्रतीत होता है, और यह बड़ी संख्या में संयोजनों के खिलाफ परीक्षण किया गया है। आप उस पृष्ठ पर परिणाम देख सकते हैं।
मैं थोड़ा एक रूबी regexp करने के लिए इसे बदल गया है, और मेरे lib/validators/email_list_validator.rb
में रख कोड यह रहा:
require 'mail'
class EmailListValidator < ActiveModel::EachValidator
# Regexp source: https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php
EMAIL_VALIDATION_REGEXP = Regexp.new('\A(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))\z', true)
def validate_each(record, attribute, value)
begin
invalid_emails = Mail::AddressList.new(value).addresses.map do |mail_address|
# check if domain is present and if it passes validation through the regex
(mail_address.domain.present? && mail_address.address =~ EMAIL_VALIDATION_REGEXP) ? nil : mail_address.address
end
invalid_emails.uniq!
invalid_emails.compact!
record.errors.add(attribute, :invalid_emails, :emails => invalid_emails.to_sentence) if invalid_emails.present?
rescue Mail::Field::ParseError => e
# Parse error on email field.
# exception attributes are:
# e.element : Kind of element that was wrong (in case of invalid addres it is Mail::AddressListParser)
# e.value: mail adresses passed to parser (string)
# e.reason: Description of the problem. A message that is not very user friendly
if e.reason.include?('Expected one of')
record.errors.add(attribute, :invalid_email_list_characters)
else
record.errors.add(attribute, :invalid_emails_generic)
end
end
end
end
और मैं मॉडल में इस तरह इसका इस्तेमाल:
validates :emails, :presence => true, :email_list => true
यह अलग-अलग विभाजक और सिंथैक्स के साथ मेलिंग सूचियों को मान्य करेगा:
mail_list = 'John Doe <[email protected]>, [email protected]; David G. <[email protected]>'
इस regexp का उपयोग करने से पहले, मैंने Devise.email_regexp
का उपयोग किया, लेकिन यह एक बहुत ही सरल regexp है और मुझे आवश्यक सभी मामलों को नहीं मिला। कुछ ईमेल टक्कर लगी।
मैंने वेब से अन्य regexps की कोशिश की, लेकिन अब तक इसका सबसे अच्छा परिणाम मिला है। उम्मीद है कि यह आपके मामले में मदद करता है।
रेगेक्स से निपटने के बिना यहां एक सुपर-आसान तरीका है: [पता लगाने-वैध-ईमेल-पता] (http: // lindsaar।नेट/2008/4/14/टिप -4-डिटेक्टिंग-ए-वैध-ईमेल-पता) – Zabba
क्या आप अधिक विस्तृत कारण दे सकते हैं कि एमएक्स सर्वर पूछताछ क्यों विफल हो रही है? मैं जानना चाहता हूं इसलिए मैं देख सकता हूं कि ये ठीक हैं या नहीं। – lulalala