forked from maorlipchuk/Wiki-Link-Validator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendMail.py
executable file
·63 lines (56 loc) · 2.24 KB
/
sendMail.py
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
#!/usr/bin/env python
import logging
import logging.handlers
import ConfigParser
class TlsSMTPHandler(logging.handlers.SMTPHandler):
def emit(self, record):
"""
Emit a record.
Format the record and send it to the specified addresses.
"""
try:
import smtplib
import string # for tls add this line
try:
from email.utils import formatdate
except ImportError:
formatdate = self.date_time
port = self.mailport
if not port:
port = smtplib.SMTP_PORT
smtp = smtplib.SMTP(self.mailhost, port)
msg = self.format(record)
msg = 'From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s' % (
self.fromaddr,
string.join(self.toaddrs, ','),
self.getSubject(record),
formatdate(), msg)
if self.username:
smtp.ehlo() # for tls add this line
smtp.starttls() # for tls add this line
smtp.ehlo() # for tls add this line
smtp.login(self.username, self.password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
# Get all configuration values
configParser = ConfigParser.RawConfigParser()
configFilePath = 'conf/mail.conf'
configParser.read(configFilePath)
mailhost = configParser.get('mail-wiki-links-validator', 'MAILHOST')
port = configParser.get('mail-wiki-links-validator', 'PORT')
subject = configParser.get('mail-wiki-links-validator', 'SUBJECT')
fromaddr = configParser.get('mail-wiki-links-validator', 'FROMADDR')
username = configParser.get('mail-wiki-links-validator', 'USERNAME')
password = configParser.get('mail-wiki-links-validator', 'PASSWORD')
def send_mail(msg, toaddr):
logger = logging.getLogger('mail')
addr = '[email protected]'
gm = TlsSMTPHandler((mailhost, port), fromaddr, addr,
subject, (username, password))
logger.addHandler(gm)
logger.log(logging.ERROR, msg)
logger.removeHandler(gm)