EmailPlugin now better supports to= variable; fixes #11

pull/16/head
Chris Caron 2018-05-30 20:14:41 -04:00
parent ae2f11f27e
commit 6177e6553f
2 changed files with 31 additions and 28 deletions

View File

@ -128,15 +128,12 @@ class NotifyEmail(NotifyBase):
# Default SMTP Timeout (in seconds) # Default SMTP Timeout (in seconds)
connect_timeout = 15 connect_timeout = 15
def __init__(self, to, **kwargs): def __init__(self, **kwargs):
""" """
Initialize Email Object Initialize Email Object
""" """
super(NotifyEmail, self).__init__(**kwargs) super(NotifyEmail, self).__init__(**kwargs)
# Store To Addr
self.to_addr = to
# Handle SMTP vs SMTPS (Secure vs UnSecure) # Handle SMTP vs SMTPS (Secure vs UnSecure)
if not self.port: if not self.port:
if self.secure: if self.secure:
@ -156,22 +153,21 @@ class NotifyEmail(NotifyBase):
# addresses from the URL provided # addresses from the URL provided
self.from_name = kwargs.get('name', None) self.from_name = kwargs.get('name', None)
self.from_addr = kwargs.get('from', None) self.from_addr = kwargs.get('from', None)
self.to_addr = kwargs.get('to', self.from_addr)
if not NotifyBase.is_email(self.from_addr):
# Parse Source domain based on from_addr
raise TypeError('Invalid ~From~ email format: %s' % self.from_addr)
if not NotifyBase.is_email(self.to_addr): if not NotifyBase.is_email(self.to_addr):
raise TypeError('Invalid ~To~ email format: %s' % self.to_addr) raise TypeError('Invalid ~To~ email format: %s' % self.to_addr)
match = NotifyBase.is_email(self.from_addr)
if not match:
# Parse Source domain based on from_addr
raise TypeError('Invalid ~From~ email format: %s' % self.to_addr)
# Now detect the SMTP Server # Now detect the SMTP Server
self.smtp_host = kwargs.get('smtp_host', '') self.smtp_host = kwargs.get('smtp_host', '')
# Apply any defaults based on certain known configurations # Apply any defaults based on certain known configurations
self.NotifyEmailDefaults() self.NotifyEmailDefaults()
# Using the match, we want to extract the user id and domain
return return
def NotifyEmailDefaults(self): def NotifyEmailDefaults(self):
@ -190,7 +186,7 @@ class NotifyEmail(NotifyBase):
self.logger.debug('Scanning %s against %s' % ( self.logger.debug('Scanning %s against %s' % (
self.to_addr, WEBBASE_LOOKUP_TABLE[i][0] self.to_addr, WEBBASE_LOOKUP_TABLE[i][0]
)) ))
match = WEBBASE_LOOKUP_TABLE[i][1].match(self.to_addr) match = WEBBASE_LOOKUP_TABLE[i][1].match(self.from_addr)
if match: if match:
self.logger.info( self.logger.info(
'Applying %s Defaults' % 'Applying %s Defaults' %
@ -325,29 +321,32 @@ class NotifyEmail(NotifyBase):
if len(format) > 0 and format[0] == 't': if len(format) > 0 and format[0] == 't':
results['notify_format'] = NotifyFormat.TEXT results['notify_format'] = NotifyFormat.TEXT
if 'to' in results['qsd'] and len(results['qsd']['to']): # Attempt to detect 'from' email address
to_addr = NotifyBase.unquote(results['qsd']['to']).strip() if 'from' in results['qsd'] and len(results['qsd']['from']):
from_addr = NotifyBase.unquote(results['qsd']['from'])
else: else:
# get 'To' email address # get 'To' email address
to_addr = '%s@%s' % ( from_addr = '%s@%s' % (
re.split( re.split(
'[\s@]+', NotifyBase.unquote(results['user']))[0], '[\s@]+', NotifyBase.unquote(results['user']))[0],
results.get('host', '') results.get('host', '')
) )
# Attempt to detect 'from' email address
from_addr = to_addr
if 'from' in results['qsd'] and len(results['qsd']['from']):
from_addr = NotifyBase.unquote(results['qsd']['from'])
if not NotifyBase.is_email(from_addr):
# Lets be clever and attempt to make the from # Lets be clever and attempt to make the from
# address an email based on the to address # address an email based on the to address
from_addr = '%s@%s' % ( from_addr = '%s@%s' % (
re.split('[\s@]+', from_addr)[0], re.split('[\s@]+', from_addr)[0],
re.split('[\s@]+', to_addr)[-1], re.split('[\s@]+', from_addr)[-1],
) )
# Attempt to detect 'to' email address
if 'to' in results['qsd'] and len(results['qsd']['to']):
to_addr = NotifyBase.unquote(results['qsd']['to']).strip()
if not to_addr:
# Send to ourselves if not otherwise specified to do so
to_addr = from_addr
if 'name' in results['qsd'] and len(results['qsd']['name']): if 'name' in results['qsd'] and len(results['qsd']['name']):
# Extract from name to associate with from address # Extract from name to associate with from address
results['name'] = NotifyBase.unquote(results['qsd']['name']) results['name'] = NotifyBase.unquote(results['qsd']['name'])

View File

@ -101,19 +101,23 @@ TEST_URLS = (
('mailtos://user:@nuxref.com', { ('mailtos://user:@nuxref.com', {
'instance': plugins.NotifyEmail, 'instance': plugins.NotifyEmail,
}), }),
# Invalid From Address (falls back to using To Address) # Invalid From Address
('mailtos://user:pass@nuxref.com?from=@', { ('mailtos://user:pass@nuxref.com?from=@', {
'exception': TypeError, 'exception': TypeError,
}), }),
# Invalid To Address # Invalid From Address
('mailtos://nuxref.com?user=&pass=.', { ('mailtos://nuxref.com?user=&pass=.', {
'exception': TypeError, 'exception': TypeError,
}), }),
# Invalid To Address
('mailtos://user:pass@nuxref.com?to=@', {
'exception': TypeError,
}),
# Valid URL, but can't structure a proper email # Valid URL, but can't structure a proper email
('mailtos://nuxref.com?user=%20!&pass=.', { ('mailtos://nuxref.com?user=%20!&pass=.', {
'exception': TypeError, 'exception': TypeError,
}), }),
# Invalid To Address # Invalid From (and To) Address
('mailtos://nuxref.com?to=test', { ('mailtos://nuxref.com?to=test', {
'exception': TypeError, 'exception': TypeError,
}), }),