Guest User

Untitled

a guest
Dec 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Zabbix SMTP Alert script for Office365.
  5. """
  6.  
  7. import sys
  8. import smtplib
  9. from email.MIMEText import MIMEText
  10. from email.Header import Header
  11. from email.Utils import formatdate
  12.  
  13. # Mail Account
  14. MAIL_ACCOUNT = 'your.account@provider.com'
  15. MAIL_PASSWORD = 'your mail password'
  16.  
  17. # Sender Name
  18. SENDER_NAME = u'Zabbix Alert'
  19.  
  20. # Mail Server
  21. SMTP_SERVER = 'smtp.office365.com'
  22. SMTP_PORT = 587
  23. # TLS
  24. SMTP_TLS = True
  25.  
  26. def send_mail(recipient, subject, body, encoding='utf-8'):
  27. session = None
  28. msg = MIMEText(body, 'plain', encoding)
  29. msg['Subject'] = Header(subject, encoding)
  30. msg['From'] = Header(SENDER_NAME, encoding)
  31. msg['To'] = recipient
  32. msg['Date'] = formatdate()
  33. try:
  34. session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
  35. if SMTP_TLS:
  36. session.ehlo()
  37. session.starttls()
  38. session.ehlo()
  39. session.login(MAIL_ACCOUNT, MAIL_PASSWORD)
  40. session.sendmail(MAIL_ACCOUNT, recipient, msg.as_string())
  41. except Exception as e:
  42. raise e
  43. finally:
  44. # close session
  45. if session:
  46. session.quit()
  47.  
  48. if __name__ == '__main__':
  49. """
  50. recipient = sys.argv[1]
  51. subject = sys.argv[2]
  52. body = sys.argv[3]
  53. """
  54. if len(sys.argv) == 4:
  55. send_mail(
  56. recipient=sys.argv[1],
  57. subject=sys.argv[2],
  58. body=sys.argv[3])
  59. else:
  60. print u"""requires 3 parameters (recipient, subject, body)
  61. \t$ zabbix-alert-smtp.py recipient subject body
  62. """
Add Comment
Please, Sign In to add comment