Class for handling gmail notifications
| 9 | from notifiers.utils import NotifierUtils |
| 10 | |
| 11 | class GmailNotifier(NotifierUtils): |
| 12 | """Class for handling gmail notifications |
| 13 | """ |
| 14 | |
| 15 | def __init__(self, username, password, destination_addresses): |
| 16 | """Initialize GmailNotifier class |
| 17 | |
| 18 | Args: |
| 19 | username (str): Username of the gmail account to use for sending message. |
| 20 | password (str): Password of the gmail account to use for sending message. |
| 21 | destination_addresses (list): A list of email addresses to notify. |
| 22 | """ |
| 23 | |
| 24 | self.logger = structlog.get_logger() |
| 25 | self.smtp_server = 'smtp.gmail.com:587' |
| 26 | self.username = username |
| 27 | self.password = password |
| 28 | self.destination_addresses = ','.join(destination_addresses) |
| 29 | |
| 30 | |
| 31 | @retry(stop=stop_after_attempt(3)) |
| 32 | def notify(self, message): |
| 33 | """Sends the message. |
| 34 | |
| 35 | Args: |
| 36 | message (str): The message to send. |
| 37 | |
| 38 | Returns: |
| 39 | dict: A dictionary containing the result of the attempt to send the email. |
| 40 | """ |
| 41 | |
| 42 | header = 'From: %s\n' % self.username |
| 43 | header += 'To: %s\n' % self.destination_addresses |
| 44 | header += 'Content-Type: text/plain\n' |
| 45 | header += 'Subject: Crypto-signal alert!\n\n' |
| 46 | message = header + message |
| 47 | |
| 48 | smtp_handler = smtplib.SMTP(self.smtp_server) |
| 49 | smtp_handler.starttls() |
| 50 | smtp_handler.login(self.username, self.password) |
| 51 | result = smtp_handler.sendmail(self.username, self.destination_addresses, message) |
| 52 | smtp_handler.quit() |
| 53 | return result |