1

I'm trying to create two functions, one that takes a screenshot and then calls the other function that should send an email with the screenshot attached. Both of them work if they're separated, but when I try to piece them together a huge error message pops up.

I tried putting it in the same block of code, separating them in two functions, different ways of calling the function... Nothing seems to work

import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import os.path

def enviar_email():
    email = 'email'
    password = 'password'
    subject = 'This is the subject'
    message = 'This is my message'
    file_location = 'ss1.jpg'

    msg = MIMEMultipart()
    msg['From'] = email
    msg['To'] = email
    msg['Subject'] = subject

    msg.attach(MIMEText(message, 'plain'))

    # Setup the attachment
    filename = os.path.basename(file_location)
    attachment = open(file_location, "rb").read()
    image = MIMEImage(attachment, name=filename)
    msg.attach(image)


    # Attach the attachment to the MIMEMultipart object
    # msg.attach(part)


    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email, password)
    text = msg.as_string()
    server.sendmail(email, email, text)
    server.quit()

def main():
    imagem = ImageGrab.grab()
    imagem.save('ss1.jpg', 'jpeg')
    enviar_email()

main()

***************************************
Error I get: 



RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File ".\enviar_email.py", line 51, in <module>
    main()
  File ".\enviar_email.py", line 47, in main
    imagem = ImageGrab.grab()
Vitor
  • 43
  • 2
  • 7

1 Answers1

0

I solved it by doing this.

if __name__ == '__main__':
    main()
Vitor
  • 43
  • 2
  • 7