0

I have written a script with will check .docx file format in a particular format and give me the output on print function.

import glob

if glob.glob('//gbhfrd01/directorates/Business Support/Test/john//Python/*.docx'):
    print("File exist")
else:
    print("File not exist")

I want to send email based on the output of the print funtion. For example if "File not Exist" send email to abc@test.com.

I have seen online how to send email with python but i don't know how can i send email based on the out put of my print function.

Any help on this would be really greatful.

Biswa
  • 331
  • 6
  • 22

3 Answers3

1

Do this :

import glob

if glob.glob('//gbhfrd01/directorates/Business Support/Test/john//Python/*.docx'):
    msg = "File exist"
    print(msg)
else:
    msg = "File not exist"
    print(msg)

if msg == "File not exist":
    # Write your email send logic here 

OR

Since in the else block of your function you print "File not exist" ,there itself write the email logic

 if glob.glob('//gbhfrd01/directorates/Business Support/Test/john//Python/*.docx'):
        print("File exist")
    else:
        print("File not exist")

        # Write your email logic here
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
1

import glob,ssl,smtplib

def send_mail(message=None):
    port = 587
    smtp_server = "smtp.gmail.com"
    sender_email = "my@gmail.com"
    receiver_email = "your@gmail.com"
    password = "password"

    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.starttls(context=context)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message)


if glob.glob('//gbhfrd01/directorates/Business Support/Test/john//Python/*.docx'):
    print("File exist")
else:
    print("File not exist")
    send_mail("File doesn't exist")

G1Rao
  • 424
  • 5
  • 11
  • 2
    You should have tell him to Enable less secure apps ( https://myaccount.google.com/lesssecureapps?pli=1) on the gmail account he is using, else it won't work and also use (Python>=3.6) – Abhay Salvi Apr 23 '20 at 12:47
  • Getting error after enable less secure apps as line 4 port = 587 ^ IndentationError: expected an indented block – Biswa Apr 23 '20 at 13:02
  • removed tab space after port = 587 – G1Rao Apr 23 '20 at 13:08
  • Is there any way i can send email without log in with my password. I m using a local smtp server of our office instead of gmail? – Biswa Apr 23 '20 at 14:01
  • [duplicate] https://stackoverflow.com/questions/6270782/how-to-send-an-email-with-python – G1Rao Apr 23 '20 at 14:11
1

Your code is ready to do what you want it to.

If you want to send the email if "file not exist" just write your email code underneath print("file not exist") like so:

import glob

if glob.glob('//gbhfrd01/directorates/Business Support/Test/john//Python/*.docx'):
    print("File exist")
else:
    print("File not exist")
    write your code here which sends the email.

If you want the email to be sent when "file exist" then put your code under "print("file exist") like so:

import glob

if glob.glob('//gbhfrd01/directorates/Business Support/Test/john//Python/*.docx'):
    print("File exist")
    write your code here which sends the email.

else:
    print("File not exist")