尝试使用 python smtplib 发送电子邮件时出现 "LazyImporter object is not callable" 错误
Getting "LazyImporter object is not callable" error when trying to send email using python smtplib
尝试发送电子邮件时出现 "LazyImporter' object is not callable" 错误
附件使用来自 gmail 的 python smtplib。
我在发件人 gmail
中启用了允许较少的安全应用程序设置
代码:
import smtplib
from email import MIMEBase
from email import MIMEText
from email.mime.multipart import MIMEMultipart
from email import Encoders
import os
def send_email(to, subject, text, filenames):
try:
gmail_user = 'xx@gmail.com'
gmail_pwd = 'xxxx'
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(to)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'% os.path.basename(file))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com:587") #465,587
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
mailServer.close()
print('successfully sent the mail')
except smtplib.SMTPException,error::
print str(error)
if __name__ == '__main__':
attachment_file = ['t1.txt','t2.csv']
to = "xxxxxx@gmail.com"
TEXT = "Hello everyone"
SUBJECT = "Testing sending using gmail"
send_email(to, SUBJECT, TEXT, attachment_file)
错误:文件 "test_mail.py",第 64 行,位于
send_email(至,主题,文本,attachment_file)
文件 "test_mail.py",第 24 行,在 send_email 中
msg.attach(MIME文本(文本))
类型错误:'LazyImporter' 对象不可调用
就像@How about nope 所说的那样,您使用导入语句导入的是 MIMEText 模块,而不是 class。我可以从您的代码中重现错误。当我改为从 email.mime.text 导入时,错误消失了。
尝试发送电子邮件时出现 "LazyImporter' object is not callable" 错误 附件使用来自 gmail 的 python smtplib。 我在发件人 gmail
中启用了允许较少的安全应用程序设置代码:
import smtplib
from email import MIMEBase
from email import MIMEText
from email.mime.multipart import MIMEMultipart
from email import Encoders
import os
def send_email(to, subject, text, filenames):
try:
gmail_user = 'xx@gmail.com'
gmail_pwd = 'xxxx'
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(to)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'% os.path.basename(file))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com:587") #465,587
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
mailServer.close()
print('successfully sent the mail')
except smtplib.SMTPException,error::
print str(error)
if __name__ == '__main__':
attachment_file = ['t1.txt','t2.csv']
to = "xxxxxx@gmail.com"
TEXT = "Hello everyone"
SUBJECT = "Testing sending using gmail"
send_email(to, SUBJECT, TEXT, attachment_file)
错误:文件 "test_mail.py",第 64 行,位于 send_email(至,主题,文本,attachment_file) 文件 "test_mail.py",第 24 行,在 send_email 中 msg.attach(MIME文本(文本)) 类型错误:'LazyImporter' 对象不可调用
就像@How about nope 所说的那样,您使用导入语句导入的是 MIMEText 模块,而不是 class。我可以从您的代码中重现错误。当我改为从 email.mime.text 导入时,错误消失了。