尝试发送电子邮件附件时出错
Error when trying to send email attachment
我在程序中使用以下函数发送电子邮件:
def send_email(subject, sender, recipients, text_body):
FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd'])
form = ApplicationForm (request.files)
submit_name = form.file_upload.data.filename
mail = Mail(app)
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
with app.open_resource(filename) as fp:
msg.attach(filename, fp.read())
mail.send(msg)
电子邮件工作正常并发送给正确的用户,但是附件没有,我相信我可能引用不正确,因为文件附件来自表单。
我已经使用了下面的功能来保存附件并且效果很好所以我不确定为什么上面的功能不起作用,有人可以帮忙吗?
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
return redirect('home')
编辑:尝试提交时收到的错误消息是:
[Errno 2] No such file or directory: 'C:\Users\richard.danvers\application\answer.docx'
看起来好像 'uploads' 没有包含在路径中,有人知道如何包含这个吗?
您在附加文件之前发送电子邮件:
mail.send(msg)
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
...
需要在 msg.attach 参数中指定内容类型。
例如'text/plain'
我在程序中使用以下函数发送电子邮件:
def send_email(subject, sender, recipients, text_body):
FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd'])
form = ApplicationForm (request.files)
submit_name = form.file_upload.data.filename
mail = Mail(app)
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
with app.open_resource(filename) as fp:
msg.attach(filename, fp.read())
mail.send(msg)
电子邮件工作正常并发送给正确的用户,但是附件没有,我相信我可能引用不正确,因为文件附件来自表单。
我已经使用了下面的功能来保存附件并且效果很好所以我不确定为什么上面的功能不起作用,有人可以帮忙吗?
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
return redirect('home')
编辑:尝试提交时收到的错误消息是:
[Errno 2] No such file or directory: 'C:\Users\richard.danvers\application\answer.docx'
看起来好像 'uploads' 没有包含在路径中,有人知道如何包含这个吗?
您在附加文件之前发送电子邮件:
mail.send(msg)
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
...
需要在 msg.attach 参数中指定内容类型。
例如'text/plain'