| Total Complexity | 4 |
| Total Lines | 62 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | '''send email''' |
||
| 2 | import smtplib |
||
| 3 | from email.mime.multipart import MIMEMultipart |
||
| 4 | from email.mime.text import MIMEText |
||
| 5 | from helpers.queuehelper import QueueName |
||
| 6 | from backend.fcmapp import Component |
||
| 7 | |||
| 8 | EMAIL = Component('email') |
||
| 9 | |||
| 10 | def when_email(channel, method, properties, body): |
||
| 11 | '''when email event is raised''' |
||
| 12 | try: |
||
| 13 | doemail(body) |
||
| 14 | except Exception as ex: |
||
| 15 | EMAIL.app.logexception(ex) |
||
| 16 | |||
| 17 | def doemail(msg): |
||
| 18 | '''sends the email''' |
||
| 19 | melogin = EMAIL.app.readlogin('emaillogin.conf') |
||
| 20 | sendtoemailfile = EMAIL.app.readlogin('emailsendto.conf') |
||
| 21 | meuser = melogin.username |
||
| 22 | you = sendtoemailfile.username |
||
| 23 | msg = MIMEMultipart('alternative') |
||
| 24 | msg['Subject'] = "Mining Summary" |
||
| 25 | msg['From'] = meuser |
||
| 26 | msg['To'] = you |
||
| 27 | text = "Daily Summary\nLine2\n" |
||
| 28 | html = """\ |
||
| 29 | <html> |
||
| 30 | <head></head> |
||
| 31 | <body> |
||
| 32 | <p>Daily Summary<br> |
||
| 33 | Line2<br> |
||
| 34 | |||
| 35 | </p> |
||
| 36 | </body> |
||
| 37 | </html> |
||
| 38 | """ |
||
| 39 | |||
| 40 | part1 = MIMEText(text, 'plain') |
||
| 41 | part2 = MIMEText(html, 'html') |
||
| 42 | msg.attach(part1) |
||
| 43 | msg.attach(part2) |
||
| 44 | mail = smtplib.SMTP('smtp.gmail.com', 587) |
||
| 45 | |||
| 46 | mail.ehlo() |
||
| 47 | |||
| 48 | mail.starttls() |
||
| 49 | |||
| 50 | mail.login(melogin.username, melogin.password) |
||
| 51 | mail.sendmail(meuser, you, msg.as_string()) |
||
| 52 | mail.quit() |
||
| 53 | |||
| 54 | print('email sent') |
||
| 55 | |||
| 56 | def main(): |
||
| 57 | EMAIL.listeningqueue = EMAIL.app.subscribe(QueueName.Q_EMAIL, when_email) |
||
| 58 | EMAIL.listen() |
||
| 59 | |||
| 60 | if __name__ == "__main__": |
||
| 61 | main() |
||
| 62 |