Python 3.x 为什么要编码?base64给我类型错误:需要像object这样的字节,而不是NoneType

Python 3.x 为什么要编码?base64给我类型错误:需要像object这样的字节,而不是NoneType,python-3.x,outlook,smtplib,Python 3.x,Outlook,Smtplib,我有一个错误,TypeError:应该是像object这样的字节,而不是NoneType。 从以下位置引发的错误:encoders.encode_base64(eml_atch)第56行 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils imp

我有一个错误,TypeError:应该是像object这样的字节,而不是NoneType。 从以下位置引发的错误:encoders.encode_base64(eml_atch)第56行

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders
import os,datetime

CRLF = "\r\n"
login = "my@email.com"
password = "mypassword"
attendees = ["attendees1@gmail.com", "attendees2@gmail.com","attendees3@gmail.com"]
organizer = "ORGANIZER;CN=organiser:mailto:first"+CRLF+" @ada-asia.com"
fro = "Fahim Maula <fahim.maula@ada-asia.com>"

ddtstart = datetime.datetime.now()
dtoff = datetime.timedelta(days = 1)
dur = datetime.timedelta(hours = 1)
ddtstart = ddtstart +dtoff
dtend = ddtstart + dur
dtstamp = datetime.datetime.now().strftime("%Y%m%dT%H%M%SZ")
dtstart = ddtstart.strftime("%Y%m%dT%H%M%SZ")
dtend = dtend.strftime("%Y%m%dT%H%M%SZ")

description = "DESCRIPTION: test invitation from pyICSParser"+CRLF
attendee = ""
for att in attendees:
    attendee += "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE"+CRLF+" ;CN="+att+";X-NUM-GUESTS=0:"+CRLF+" mailto:"+att+CRLF
ical = "BEGIN:VCALENDAR"+CRLF+"PRODID:pyICSParser"+CRLF+"VERSION:2.0"+CRLF+"CALSCALE:GREGORIAN"+CRLF
ical+="METHOD:REQUEST"+CRLF+"BEGIN:VEVENT"+CRLF+"DTSTART:"+dtstart+CRLF+"DTEND:"+dtend+CRLF+"DTSTAMP:"+dtstamp+CRLF+organizer+CRLF
ical+= "UID:FIXMEUID"+dtstamp+CRLF
ical+= attendee+"CREATED:"+dtstamp+CRLF+description+"LAST-MODIFIED:"+dtstamp+CRLF+"LOCATION:"+CRLF+"SEQUENCE:0"+CRLF+"STATUS:CONFIRMED"+CRLF
ical+= "SUMMARY:test "+ddtstart.strftime("%Y%m%d @ %H:%M")+CRLF+"TRANSP:OPAQUE"+CRLF+"END:VEVENT"+CRLF+"END:VCALENDAR"+CRLF

eml_body = "Email body visible in the invite of outlook and outlook.com but not google calendar"
eml_body_bin = "This is the email body in binary - two steps"
msg = MIMEMultipart('mixed')
msg['Reply-To']=fro
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "pyICSParser invite"+dtstart
msg['From'] = fro
msg['To'] = ",".join(attendees)

part_email = MIMEText(eml_body,"html")
part_cal = MIMEText(ical,'calendar;method=REQUEST')

msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)

ical_atch = MIMEBase('application/ics',' ;name="%s"'%("invite.ics"))
ical_atch.set_payload(ical)
encoders.encode_base64(ical_atch)
ical_atch.add_header('Content-Disposition', 'attachment; filename="%s"'%("invite.ics"))

eml_atch = MIMEBase('text/plain','')
encoders.encode_base64(eml_atch)
eml_atch.add_header('Content-Transfer-Encoding', "")

msgAlternative.attach(part_email)
msgAlternative.attach(part_cal)

mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(login, password)
mailServer.sendmail(fro, attendees, msg.as_string())
mailServer.close()
导入smtplib
从email.mime.multipart导入MIMEMultipart
从email.mime.base导入MIMEBase
从email.mime.text导入MIMEText
从email.utils导入COMMASPACE,formatdate
从电子邮件导入编码器
导入操作系统,日期时间
CRLF=“\r\n”
登录名=”my@email.com"
password=“mypassword”
与会者=[”attendees1@gmail.com", "attendees2@gmail.com","attendees3@gmail.com"]
organizer=“organizer;CN=organizer:mailto:first”+CRLF+“@ada-asia.com”
fro=“法希姆·莫拉”
ddtstart=datetime.datetime.now()
dtoff=datetime.timedelta(天=1)
dur=datetime.timedelta(小时=1)
ddtstart=ddtstart+dtoff
dtend=ddtstart+dur
dtstamp=datetime.datetime.now().strftime(“%Y%m%dT%H%m%SZ”)
dtstart=ddtstart.strftime(“%Y%m%dT%H%m%SZ”)
dtend=dtend.strftime(“%Y%m%dT%H%m%SZ”)
description=“description:来自pyICSParser的测试邀请”+CRLF
attendee=“”
对于与会者中的att:
attendee+=“attendee;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE”+CRLF+“CN=”+att+“X-NUM-GUESTS=0:“+CRLF+”mailto:“+att+CRLF”
ical=“BEGIN:VCALENDAR”+CRLF+“PRODID:pyICSParser”+CRLF+“版本:2.0”+CRLF+“CALSCALE:GREGORIAN”+CRLF
ical+=“方法:请求”+CRLF+“开始:VEVENT”+CRLF+“DTSTART:“+DTSTART+CRLF+”DTEND:“+DTEND+CRLF+”DTSTAMP:“+DTSTAMP+CRLF+组织者+CRLF”
ical+=“UID:FIXMEUID”+dtstamp+CRLF
ical+=与会者+“已创建:”+dtstamp+CRLF+描述+“上次修改:”+dtstamp+CRLF+“位置:”+CRLF+“序列:0”+CRLF+“状态:已确认”+CRLF
ical+=“摘要:测试”+ddtstart.strftime(“%Y%m%d@%H:%m”)+CRLF+“传输:不透明”+CRLF+“结束:VEVEVENT”+CRLF+“结束:VCALENDAR”+CRLF
eml_body=“电子邮件正文在outlook和outlook.com的邀请中可见,但在谷歌日历中不可见”
eml_body_bin=“这是二进制的电子邮件正文-两步”
msg=MIMEMultipart('mixed')
msg['Reply-To']=fro
msg['Date']=formattate(localtime=True)
msg['Subject']=“pyICSParser invite”+dtstart
msg['From']=fro
msg['To']=“,”。加入(与会者)
part_email=MIMEText(eml_body,“html”)
part_cal=MIMEText(ical,'calendar;method=REQUEST')
msgAlternative=MIMEMultipart('备选方案')
附加消息(msgaltentive)
ical_atch=MIMEBase('application/ics',';name=“%s”%”(“invite.ics”))
ical_atch.set_有效载荷(ical)
编码器。编码基础64(ical_atch)
ical_atch.add_头('Content-Disposition','attachment;filename=“%s”'%”(“invite.ics”))
eml_atch=MIMEBase('text/plain','')
编码器。编码基础64(eml\U atch)
eml_atch.add_头('Content-Transfer-Encoding',“”)
msgaltentive.attach(part_电子邮件)
M可选附件(零件号)
mailServer=smtplib.SMTP('SMTP.gmail.com',587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(登录名,密码)
mailServer.sendmail(fro、与会者、msg.as_string())
mailServer.close()

事实上,这个脚本是我从你那里得到的,我不知道它是如何工作的。我想先测试一下,但我遇到了这个错误。如何修复此问题?

这似乎是Python2代码,Python3的情况有所改变

问题在于这一行:

eml_atch = MIMEBase('text/plain','')
将行更改为该行,它将工作:

eml_atch = MIMEText('', 'plain')
它应该有效地执行与原始行相同的操作。同时将导入更改为:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import os,datetime
COMMASPACE = ', '

什么是决定?