flask发邮件遇到的两个问题及解决方案
一开始写的代码#!/usr/bin/env python# -*- coding: utf-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom flask import Flask, requestMAIL_SERVER =...
·
一开始写的代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from flask import Flask, request
MAIL_SERVER = 'smtp.163.com'
MAIL_USERNAME = 'my@163.com'
MAIL_PASSWORD = 'mypassword'
MAIL_PORT = 25
MAIL_NAME = '冰风漫天'
MAIL_ADDRESS = 'my@163.com'
def send_mail(subject, msg, receivers, cc=None, files=None):
message = MIMEMultipart('alternative')
mail_from = '{}<{}>'.format(MAIL_NAME, MAIL_ADDRESS)
message['From'] = mail_from
if cc:
cc = set(cc)
message['Cc'] = ','.join(cc)
message['To'] = ','.join(receivers)
message['Subject'] = subject
message.attach(MIMEText(msg, 'plain', 'utf-8'))
if MAIL_PORT == 25:
smtp_obj = smtplib.SMTP()
else:
smtp_obj = smtplib.SMTP_SSL()
if files:
for file in files.values():
msg_file = MIMEText(file.stream.read(), 'base64', 'utf8')
msg_file.add_header('content-disposition', 'attachment', filename=file.filename)
message.attach(msg_file)
smtp_obj.connect(MAIL_SERVER)
smtp_obj.login(MAIL_USERNAME, MAIL_PASSWORD)
smtp_obj.sendmail(mail_from, receivers, message.as_string())
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
subject = request.form.get('subject')
msg = request.form.get('msg')
receivers = request.form.get('receivers')
send_mail(subject, msg, receivers, files=request.files)
return '1'
app.run()
请求方式
import requests
requests.post("http://127.0.0.1:5000/",data={"subject":u"发个邮件2","msg":"qweasd","receivers":["youmail@163.com"]},files={'file1':('filename1.txt','文件内容1'),'file2':('filename2.txt','file data2')})
存在两个问题:
1.当收件人有多个时,只有一个人能收到
2.部分邮件服务器上接收邮件不正常
解决方案:
问题1:把receivers = request.form.get(‘receivers’)改成receivers = request.form.getlist(‘receivers’)
问题2:把message = MIMEMultipart(‘alternative’)改成message = MIMEMultipart(‘mixed’)
最后的代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from flask import Flask, request
MAIL_SERVER = 'smtp.163.com'
MAIL_USERNAME = 'my@163.com'
MAIL_PASSWORD = 'mypassword'
MAIL_PORT = 25
MAIL_NAME = '冰风漫天'
MAIL_ADDRESS = 'my@163.com'
def send_mail(subject, msg, receivers, cc=None, files=None):
message = MIMEMultipart('mixed')
mail_from = '{}<{}>'.format(MAIL_NAME, MAIL_ADDRESS)
message['From'] = mail_from
if cc:
cc = set(cc)
message['Cc'] = ','.join(cc)
message['To'] = ','.join(receivers)
message['Subject'] = subject
message.attach(MIMEText(msg, 'plain', 'utf-8'))
if MAIL_PORT == 25:
smtp_obj = smtplib.SMTP()
else:
smtp_obj = smtplib.SMTP_SSL()
if files:
for file in files.values():
msg_file = MIMEText(file.stream.read(), 'base64', 'utf8')
msg_file.add_header('content-disposition', 'attachment', filename=file.filename)
message.attach(msg_file)
smtp_obj.connect(MAIL_SERVER)
smtp_obj.login(MAIL_USERNAME, MAIL_PASSWORD)
smtp_obj.sendmail(mail_from, receivers, message.as_string())
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
subject = request.form.get('subject')
msg = request.form.get('msg')
receivers = request.form.getlist('receivers')
send_mail(subject, msg, receivers, files=request.files)
return '1'
app.run()
更多推荐
已为社区贡献1条内容
所有评论(0)