Python代码: 封装Django的send_mail函数,方便在本地(没有邮件服务器)进行邮件相关的测试
01 def my_send_mail(request, subject, body, sender, recv_list, reply_to=None):
02 """send_mail wrapper: write to command line if it's called on localhost
03 """
04 local = "127.0.0.1"
05 if request.META['HTTP_HOST'][:len(local)] == local:
06 print "Subject:", subject
07 print "From:", sender
08 print "To:", recv_list
09 if reply_to:
10 print "Reply-To:", reply_to
11 print "Body:\n", body
12 else:
13 if reply_to:
14 email = EmailMessage(
15 subject,
16 body,
17 sender,
18 recv_list,
19 [],
20 headers = {'Reply-To': reply_to})
21 email.send()
22 else:
23 send_mail(subject, body, sender, recv_list)