在django发送每个请求的邮件 [英] Send mail per request in django

查看:116
本文介绍了在django发送每个请求的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的是杀了我。我一直在处理这个日子。



当用户从我的django网络应用程序下载文件时,我想通过发送邮件通知上传者他的文件已被下载。问题是,如果我下载一个低文件大小(489kb),它将向上传者发送一条邮件。但是如果我下载一个3MB或以上的文件大小,它将向上传者发送 c $ c>多个邮件 / p>

我只是希望每次下载向上传者发送一封邮件通知。



视图:

  @login_required 
def document_view请求,emov_id):
fileload = Emov.objects.get(id = emov_id)
filename = fileload.mov_file.name.split('/')[ - 1]
filesize = fileload .mov_file.size
response = HttpResponse(fileload.mov_file,content_type ='')
response ['Content-Disposition'] ='attachment; filename =%s'%filename
response ['Content-Length'] = filesize
send_mail('你的文件刚刚被下载',loader.get_template('download.txt')。render ({'fileload':fileload})),'test@example.com',[fileload.email,])
返回响应

download.txt

 '您的文件{{fileload.name}}已被下载!

如何根据下载请求发送邮件?

解决方案

我会建议不同的方法...



当有人下载​​文件时,事件到您的数据库上的表

写入会话ID,文件名,用户名。

确保session_id + file_name + user_name 唯一键

这样,您可以获得更多可以帮助您的信息。



稍后(作为crontab批处理或保存监听器)发送电子邮件。

您甚至可以发送每日/每周报告等...


This is really killing me. I've been dealing with this for days.

When a user download a file from my django web app, I want to notify the uploader that his file has been downloaded by sending a mail. The problem is, If I should download a low file size (489kb), it will send a mail once to the uploader. But if I should download a file size of 3mb or above it will send more than one mail to the uploader.

I just want it to send one mail notification to the uploader per download.

views:

@login_required
def document_view(request,emov_id):
    fileload = Emov.objects.get(id=emov_id)
    filename = fileload.mov_file.name.split('/')[-1]
    filesize=fileload.mov_file.size
    response = HttpResponse(fileload.mov_file, content_type='') 
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    response['Content-Length'] = filesize    
    send_mail('Your file has just been downloaded',loader.get_template('download.txt').render(Context({'fileload':fileload})),'test@example.com',[fileload.email,])
    return response

download.txt

'Your file {{ fileload.name}} have been downloaded!'

How can I send mail per download request?

解决方案

I would suggest a different approach...

When someone download the file, log the event to a table on your database.
Write the Session ID, the file name, the user name.
Make sure that session_id+file_name+user_name are unique key
This way, you can get much more information that can help you later.

Later on (as a crontab batch, or save listener) send the emails.
You can even send a daily/weekly report and so on...

这篇关于在django发送每个请求的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆