azwmail.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import smtplib,ssl
  2. import imaplib
  3. import os
  4. from email.message import EmailMessage
  5. import keyring
  6. import time
  7. import sys
  8. current_path = os.path.dirname(os.path.abspath(__file__))
  9. username = ''
  10. service_name = 'workmail'
  11. password = None
  12. sys.platform
  13. if sys.platform == 'linux':
  14. ufilename = 'uname.txt'
  15. pfilename ='paswd.txt'
  16. else:
  17. ufilename = os.path.join(current_path,'uname.txt')
  18. def return_username_file():
  19. global username
  20. if os.path.exists(ufilename):
  21. with open(ufilename,'r') as f:
  22. username = f.readline()
  23. else:
  24. with open(ufilename,'w') as f:
  25. email = input("Please enter robot email id : ")
  26. f.write(email)
  27. with open(ufilename,'r') as f:
  28. username = f.readline()
  29. return username
  30. def return_password_file():
  31. global pfilename
  32. if os.path.exists(pfilename):
  33. with open(pfilename,'r') as f:
  34. password = f.readline()
  35. else:
  36. with open(pfilename,'w') as f:
  37. password = input("Please enter robot password, this will be stored in plain text : ")
  38. f.write(password)
  39. with open(pfilename,'r') as f:
  40. password = f.readline()
  41. return password
  42. def get_credentials():
  43. username = return_username_file()
  44. if sys.platform == 'linux':
  45. creds = return_password_file()
  46. return username,creds
  47. creds = None
  48. creds = keyring.get_password(service_name=service_name,username=username)
  49. if creds is None:
  50. set_credentials()
  51. creds = keyring.get_password(service_name=service_name,username=username)
  52. return username,creds
  53. def set_credentials():
  54. _username = return_username_file()
  55. _password = input(f"Enter Password for {username}: ")
  56. keyring.set_password(service_name=service_name,username=_username,password=_password)
  57. def return_email_session():
  58. username,password =get_credentials()
  59. server = None
  60. #sometime server connection is not done , so try 10 time befor giving up
  61. for _ in range(10):
  62. try:
  63. server = smtplib.SMTP_SSL('smtp.mail.us-east-1.awsapps.com',465)
  64. server.ehlo()
  65. server.login(username,password)
  66. break
  67. except:
  68. server = None
  69. time.sleep(10)
  70. if server is None:
  71. #even after 10 attemts server connection is not established
  72. #screw this and return none
  73. return None
  74. return server
  75. def imap_session():
  76. username,password =get_credentials()
  77. server = imaplib.IMAP4_SSL('imap.mail.us-east-1.awsapps.com',993)
  78. server.login(username,password)
  79. return server
  80. def send_email2(send_to,body,subject,attacment_dir= None):
  81. username,_ = get_credentials()
  82. e_msg = EmailMessage()
  83. e_msg.set_content(body)
  84. e_msg['Subject'] = subject
  85. e_msg['From'] = username
  86. e_msg['To'] = send_to
  87. server = return_email_session()
  88. if server is None:
  89. #connection to servr not established exiting
  90. print('connection with server could not be established.no email')
  91. return 0
  92. if not attacment_dir is None:
  93. for fname in os.listdir(attacment_dir):
  94. full_path = os.path.join(attacment_dir,fname)
  95. if not os.path.isfile(full_path):
  96. continue
  97. # use a generic bag-of-bits type.
  98. #https://docs.python.org/3/library/email.examples.html#id3
  99. maintype,subtype = 'application','octet-stream'
  100. with open(full_path,'rb') as fp:
  101. e_msg.add_attachment(fp.read(),maintype=maintype,subtype=subtype,filename=fname)
  102. try:
  103. server.send_message(e_msg)
  104. except Exception as e:
  105. print(e)
  106. try:
  107. server.quit()
  108. except Exception as e:
  109. print(e)
  110. if __name__ == "__main__":
  111. send_email2(send_to='pankaj.kushwaha@rho.ai',body='THIS IS new with atta',subject='THIS IS SUB',attacment_dir=None)