0
我能夠發送郵件。我打算附加文件從smtplib.First郵件轉換爲base64字符串,然後嘗試從POST男子附加文件。 但其給出以下錯誤:「從smtplib附件文件 - python
MultipartConversionError: Cannot attach additional subparts to non-multipart/*
從郵遞員我發送類似下面
{
"frommail":"[email protected]",
"password":"",
"tomail":"[email protected]",
"subject":"Test Mail",
"message":"mail message here",
"doctype":"",
"docname":"",
"document":"iVBORw0KGgoAAAANSUhEUgAAASoAAAEsCAYAAAB0Y/4yAAASfklEQVR4nO3df7BdVXXA8e9hMpkMgwylGeowlKGoFBFRU4ZSax1/0VpKrVqqIP6qdSpaRlE7olNLh7EttZaxaKfUWrQUEUEcpBR/R6EqYhWDQfkpIharCIQAMZCQZPWPfaMxebnvvvfuuWvfu7+fmTUvk5fMW2e/c9Y9Z"
}
如何解決this.Here我發送以base64字符串爲‘文件’字段。
import smtplib
from email.mime.text import MIMEText as text
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
@csrf_exempt
@api_view(['POST'])
def sendmail(request):
if request.method == 'POST':
data = request.data
fromMail = data["frommail"]
passwd = data["password"]
toMail = data["tomail"]
subject = data["subject"]
text1 = data["message"]
doctype = data['doctype']
docname = data['docname']
document = data['document']
msg = text(str(text1))
msg['Subject'] = subject
msg['From'] = fromMail
msg['To'] = toMail
part = MIMEBase('application', 'octet-stream')
#part.set_payload(open(document, 'rb').read())
#Encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(document))
msg.attach(part)
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromMail, passwd)
server.sendmail(fromMail, toMail, msg.as_string())
server.quit()
return JSONResponse({"Status":"Successfully sent email"})
except:
return JSONResponse({"Failure":"Unable to send mail."})
感謝您的留言。但它不能工作。同樣的錯誤。 – user1335606