姜戈。发送带有应用程序的电子邮件

Django. Sending email with an application

我启动了一个新的应用程序 gmail 并使用我的 gmail 帐户在 settings.py 中配置了设置

我是 django 的新手,我不知道如何向我的网站用户发送电子邮件

我想创建一个 link,当我打开它时我的用户会收到我的消息

我编辑了 views.py 但我不知道我的函数 "wyslij" 应该 return

from django.shortcuts import render
from django.core.mail import send_mail
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import User
from userprofile.models import UserProfile


@user_passes_test(lambda u: u.is_superuser)
# Create your views here.
def wyslij(request):
    # Create the HttpResponse object with the appropriate PDF headers.


    uzyt = UserProfile.objects.all().order_by('user_id')



    for z, uzyt in enumerate(UserProfile.objects.all()):

        send_mail('The exam is comming', 'Hi, Your exam will be tomorrow!', 'santa.claus.for.grace@gmail.com', [uzyt.email], fail_silently=False)


    return response 

在urls.py

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
    url(r'^accounts/wyslij/$', 'gmail.views.wyslij'),
)

https://docs.djangoproject.com/en/1.7/topics/email/

一定要命名您的 EMAIL 变量并 .gitignore 您的设置文件

在 settings.py 中,您必须添加这些衬里:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxxxxxxxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxx'
EMAIL_PORT = 587    #or try with 25