Email.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # import necessary packages
  2. import smtplib
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.text import MIMEText
  5. from email.mime.base import MIMEBase
  6. from email import encoders
  7. # def sendEmail(file_name,reciever_email,body,pdf_attach,subject):
  8. # # send_attachment.py
  9. # try:
  10. # # create message object instance
  11. # msg = MIMEMultipart()
  12. # # setup the parameters of the message
  13. # password = "kefonboard@2020"
  14. # msg['From'] = "admin@abacussoftware.services"
  15. # msg['To'] = reciever_email
  16. # msg['Subject'] = subject
  17. # msg.attach(MIMEText(body, 'html'))
  18. # if pdf_attach==True:
  19. # try:
  20. # pdfname='po_pdf/'+file_name
  21. # # open the file in bynary
  22. # binary_pdf = open(pdfname, 'rb')
  23. # payload = MIMEBase('application', 'octate-stream', Name='po_pdf.pdf')
  24. # payload.set_payload((binary_pdf).read())
  25. # # enconding the binary into base64
  26. # encoders.encode_base64(payload)
  27. # # add header with pdf name
  28. # payload.add_header('Content-Decomposition', 'attachment', filename=pdfname)
  29. # msg.attach(payload)
  30. # except Exception as err:
  31. # print('pdf attach err',err)
  32. # # create server
  33. # server = smtplib.SMTP('bom15.balasai.com: 587')
  34. # server.starttls()
  35. # # Login Credentials for sending the mail
  36. # server.login(msg['From'], password)
  37. # # send the message via the server.
  38. # server.sendmail(msg['From'], msg['To'], msg.as_string())
  39. # server.quit()
  40. # print('Email sent')
  41. # except Exception as err:
  42. # print(err)
  43. def sendEmail(file_name,reciever_email,body,pdf_attach,subject):
  44. # send_attachment.py
  45. try:
  46. # create message object instance
  47. msg = MIMEMultipart()
  48. # setup the parameters of the message
  49. # password = "kefonboard@2020"
  50. password = "onboardkef@2021"
  51. # msg['From'] = "admin@abacussoftware.services"
  52. msg['From'] = "onboard@kotakeducationfoundation.org"
  53. msg['To'] = reciever_email
  54. msg['Subject'] = subject
  55. msg.attach(MIMEText(body, 'html'))
  56. if pdf_attach==True:
  57. try:
  58. pdfname='po_pdf/'+file_name
  59. # open the file in bynary
  60. binary_pdf = open(pdfname, 'rb')
  61. payload = MIMEBase('application', 'octate-stream', Name='po_pdf.pdf')
  62. payload.set_payload((binary_pdf).read())
  63. # enconding the binary into base64
  64. encoders.encode_base64(payload)
  65. # add header with pdf name
  66. payload.add_header('Content-Decomposition', 'attachment', filename=pdfname)
  67. msg.attach(payload)
  68. except Exception as err:
  69. print('pdf attach err',err)
  70. server =smtplib.SMTP('smtp.gmail.com', 587)
  71. server.starttls()
  72. # Login Credentials for sending the mail
  73. server.login(msg['From'], password)
  74. # send the message via the server.
  75. server.sendmail(msg['From'], msg['To'], msg.as_string())
  76. server.quit()
  77. print('Email sent')
  78. except Exception as err:
  79. print(err)