If the world seems cold to you, kindle fires to warm it.
Sending e-mails with Django
网络 STMP 服务器的话,需要在 settings.py 中添加:
EMAIL_HOST = 'smtp.163.com' EMAIL_HOST_USER = 'your_account' EMAIL_HOST_PASSWORD = 'your_password' EMAIL_PORT = 25 EMAIL_USE_TLS = True
>>> from django.core.mail import send_mail >>> send_mail('Django mail', 'Test!!!!', 'your_account@163.com', ['recipient@xxx.com'], fail_silently=False)
from django.core.mail import send_mail from .forms import EmailPostForm def post_share(request, post_id): # 通过id检索post内容 post = get_object_or_404(Post, id=post_id, status='published') sent = False if request.method == 'POST': # 表单已提交 form = EmailPostForm(request.POST) if form.is_valid(): # 表单字段合法 cd = form.cleaned_data post_url = request.build_absolute_uri(post.get_absolute_url()) subject = '{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title) message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments']) send_mail(subject, message, 'your_account@163.com', [cd['to']]) sent = True else: form = EmailPostForm() return render(request, 'blog/post/share.html', {'post': post, 'form': form, 'sent': sent})
urlpatterns = [ # ... url(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'), ]
Activity