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 [email protected].

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.

2
  • If you already know how to send an email, just add the send command in the else, after the print... Commented Apr 23, 2020 at 12:45
  • Please check my comment below @JeevanRao's answer of you are going to use it... Commented Apr 23, 2020 at 12:48

3 Answers 3

1

import glob,ssl,smtplib

def send_mail(message=None):
    port = 587
    smtp_server = "smtp.gmail.com"
    sender_email = "[email protected]"
    receiver_email = "[email protected]"
    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")

Sign up to request clarification or add additional context in comments.

5 Comments

You should have tell him to Enable less secure apps ( myaccount.google.com/lesssecureapps?pli=1) on the gmail account he is using, else it won't work and also use (Python>=3.6)
Getting error after enable less secure apps as line 4 port = 587 ^ IndentationError: expected an indented block
removed tab space after port = 587
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?
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

Comments

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.