这封电子邮件附件和PDF有问题吗?(Trouble with this email attach and PDF?)
我正在使用我在这里找到的代码。 我试图发送一个pdf作为附件,但它无法正常工作。 我可以发好电子邮件
def Email(): import os import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() msg.attach(MIMEText(file("John.pdf").read())) server = smtplib.SMTP('smtp.mail.yahoo.com') To = '' SUBJECT = name TEXT = 'This is your bill' '\n' ,int(thesum),\ 'Your total distance is ',int(totalDistance),\ 'Yur best route is '' , '.join(root),\ server.ehlo() server.starttls() server.ehlo() msg = 'Subject: %s\n\n%s' % (SUBJECT, TEXT) server.login("", "") server.sendmail("",To, msg) print('sent') Email()我收到以下错误:
msg.attach(MIMEText(file("John.pdf").read())) NameError: name 'file' is not definedI am using code I have found here. I am trying to send a pdf as an attachment but it is not working. I can send the email fine
def Email(): import os import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() msg.attach(MIMEText(file("John.pdf").read())) server = smtplib.SMTP('smtp.mail.yahoo.com') To = '' SUBJECT = name TEXT = 'This is your bill' '\n' ,int(thesum),\ 'Your total distance is ',int(totalDistance),\ 'Yur best route is '' , '.join(root),\ server.ehlo() server.starttls() server.ehlo() msg = 'Subject: %s\n\n%s' % (SUBJECT, TEXT) server.login("", "") server.sendmail("",To, msg) print('sent') Email()I get the following error:
msg.attach(MIMEText(file("John.pdf").read())) NameError: name 'file' is not defined最满意答案
file不是一个功能。 也许你想做这样的事情:
with open('John.pdf', 'r') as f: s = f.read()然后添加s作为附件。
msg.attach(MIMEText(s))(但我不确定如何使用与电子邮件相关的东西;也许你必须对它进行base64编码并使用MIMEApplication()来获取pdf文件;也许其他人可以为你解答这个问题)
def Email(): import os import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() msg.attach(MIMEText("John.pdf")) server = smtplib.SMTP('smtp.mail.yahoo.com') To = '' SUBJECT = name TEXT = 'This is your bill' '\n' ,int(thesum),\ 'Your total distance is ',int(totalDistance),\ 'Yur best route is '' , '.join(root),\ server.ehlo() server.starttls() server.ehlo() msg = 'Subject: %s\n\n%s' % (SUBJECT, TEXT) server.login("", "") server.sendmail("",To, msg.as_string()) print('sent')更多推荐
发布评论