views.py
def create_reply(request, topic_id):
if request.method == 'POST':
t = topic.objects.get(id=topic_id)
past_time = int(time.time() - t.locktime)
r = post()
r.topic = t
if request.POST['content']:
r.content = request.POST['content']
else:
return error(request, '不能空着')
# messages.add_message(request, messages.WARNING, _('content cannot be empty'))
# return HttpResponseRedirect(reverse('topic_view', kwargs={'topic_id': topic_id}))
if past_time > 300:
messages.add_message(request, messages.WARNING, _('overtime'))
return HttpResponseRedirect(reverse('topic_view', kwargs={'topic_id': topic_id}))
r.user = request.user
# when success to reply ,unlock the topic.
t.locker = '0'
t.locked = False
t.last_replier = request.user.username
r.floor = t.reply_count + 1
r.keyword = t.keyword
if t.keyword == "keywordwrite":
t.keyword = rand_key()
elif t.keyword == "freewrite":
t.keyword = None
user_has_subscribed = t.subscriber.filter(
username=request.user.username
)
if not user_has_subscribed:
t.subscriber.add(request.user)
t.save()
r.save()
sendemail.after_response(t.subscriber.all(),t)
sendemail(t.subscriber.all(), t)
return HttpResponseRedirect(reverse('topic_view', kwargs={'topic_id': t.id}))
elif request.method == 'GET':
return error(request, 'don\'t get')
sendemail是
from django.core.mail import EmailMessage
import after_response
@after_response.enable
def sendemail(users_list,topic):
def get_mail_list(users_list):
def getmail(user):
return user.email
mail_list = map(getmail,users_list)
return mail_list
link ='http://www.gushijielong.cn/topic/'+str(topic.id)
mail_list=get_mail_list(users_list)
mailtitle =u'你关注的主题:'+ topic.title + u'有新的接续了'
mailcontent = u'点击查看'+ topic.title + u'的新接续' + "<a href='" + link + u"'>点击</a>"
email = EmailMessage(mailtitle, mailcontent, to=mail_list)
email.content_subtype = "html"
print mailcontent
email.send()
此代码用于在主题有新回复时向订阅者发送电子邮件。 它在运行服务器中有效(我可以收到电子邮件),但在apache中却没有。 当我在服务器的控制台中执行它时,sendemail()运行良好。