我有一个应用程序,用户可以通过填写表格来联系我,用户只需填写他的详细信息和他的电子邮件和主题。
代码不会引发任何错误,但我可以在设置所有内容后没有收到邮件,但联系人详细信息会存储在数据库中,因为我希望它存储。
以下是我的代码。
Models.py
class Contact(models.Model):
name = models.CharField(max_length=100)
message = models.TextField()
sender = models.EmailField()
phone = models.CharField(max_length=10)
cc_myself = models.BooleanField(blank=True)
time = models.DateTimeField(auto_now_add=True, db_index=True)
def __str__(self):
return 'Message for {}'.format(self.sender)
Forms.py
class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ['name', 'sender', 'phone', 'message', 'cc_myself']
Views.py
def contact(request):
if request.method == 'POST':
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
name = contact_form.cleaned_data['name']
message = contact_form.cleaned_data['message']
sender = contact_form.cleaned_data['sender']
phone = contact_form.cleaned_data['phone']
cc_myself = contact_form.cleaned_data['cc_myself']
recipients = ['xxxx@gmail.com']
if cc_myself:
recipients.append(sender)
send_mail(name, message, sender, recipients)
contact_form.save()
messages.success(request, 'Message sent successfully')
return redirect('contact:contact')
else:
messages.error(request, 'Error sending your Message')
else:
contact_form = ContactForm(request.POST)
context = {
"contact_form": contact_form,
}
template = 'contact.html'
return render(request, template, context)
gmail服务器
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
终端输出
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Subject: adie Ugbe
From: abcd@gmail.com
To: xxxx@gmail.com
Date: Sun, 07 Jan 2018 11:11:10 -0000
Message-ID: <20180107111110.29855.10393@1.0.0.127.in-addr.arpa>
oh oh no ooo dddddddd se skan
-------------------------------------------------------------------------------
Successful
答案 0 :(得分:2)
首先,您错过了EMAIL_BACKEND
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
答案 1 :(得分:1)
从输出中,它清楚地显示电子邮件已记录到控制台,这意味着正在您的控制台上模拟发送过程。这在技术上构成了发送电子邮件。
无论如何,我怀疑这种情况正在发生,因为您将电子邮件后端设置为
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
您需要做的是将电子邮件后端更改为:
EMAIL_BACKEND =&#39; django.core.mail.backends.smtp.EmailBackend&#39;
答案 2 :(得分:0)
当我配置它时。它成功发送给我电子邮件。我已经完成了代码,请看。请访问代码链接:https://www.dropbox.com/s/1ouq5mklq5t51su/mail.zip?dl=0
settings.py:
#include "stdio.h"
#define FALSE 0
#define TRUE !FALSE
double *mallocMatrix(const int row, const int column)
{
return (double*)malloc(row*column*sizeof(double));
}
void matrixInit(double *matrix, const int row, const int column)
{
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++)
matrix[i*column+j] = 1;
}
int matEqual(double *mat1, double *mat2, const int row, const int column)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
int k=i*column+j;
if(mat1[k]!=mat2[k])
{
printf("Entry %d doens't match.\n",k);
return FALSE;
}
}
}
return TRUE;
}
void matrixSumCpu(double *m1, double *m2, double *n, const int row, const int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
int k = i * column + j;
n[k]=m1[k]+m2[k];
}
}
}
__global__ void _2dGrid2dBlockMatSum(double *m1, double *m2, double *n, const int row, const int column)
{
int rowIndex=blockIdx.x*blockDim.x+threadIdx.x;
int columnIndex=blockIdx.y*blockDim.y+threadIdx.y;
if(rowIndex<row&&columnIndex<column)
{
int i=rowIndex*column+columnIndex;//flatten
n[i]=m1[i]+m2[i];
}
}
void checkGpuMalloc(cudaError_t code)
{
if(code != cudaSuccess)
{
exit(-1);
printf("CUDA ERROR occured. ");
}
}
void printMatrix(double *mat, const int row, const int column)
{
const int rowToPrint=3;
const int columnToPrint=6;
for(int i=0;i<rowToPrint;i++)
{
for(int j=0;j<columnToPrint;j++)
printf("%lf", mat[i*column+j]);
if(column>columnToPrint)
printf("...");
printf("\n");
}
if(row>rowToPrint)
printf("...\n");
}
int main()
{
int row=1<<10, column=1<<10;
double *h_m1=NULL, *h_m2=NULL,*h_n1=NULL, *h_n2=NULL;//n=m1+m2
h_m1=mallocMatrix(row, column);
h_m2=mallocMatrix(row, column);
h_n1=mallocMatrix(row, column);
h_n2=mallocMatrix(row, column);
if(h_m1==NULL||h_m2==NULL||h_n1==NULL||h_n2==NULL)
{
printf("Unable to allocate enough memory on CPU\n");
exit(-1);
}
matrixInit(h_m1,row,column);
matrixInit(h_m2,row,column);
printf("Summing matrices on CPU...\n");
matrixSumCpu(h_m1,h_m2,h_n1,row,column);
double *d_m1=NULL, *d_m2=NULL, *d_n=NULL;
checkGpuMalloc(cudaMalloc((void**)&d_m1, row*column*sizeof(double)));
checkGpuMalloc(cudaMalloc((void**)&d_m2, row*column*sizeof(double)));
checkGpuMalloc(cudaMalloc((void**)&d_n, row*column*sizeof(double)));
cudaMemcpy(d_m1, h_m1, row*column*sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(d_m2, h_m2, row*column*sizeof(double), cudaMemcpyHostToDevice);
cudaMemset(d_n, 0, row*column*sizeof(double));
printf("Summing matrices on GPU with 2D grid and 2D blocks.\n");
printf("%d\n", (1<<5,1<<5));
_2dGrid2dBlockMatSum<<<(1<<5,1<<5),(1<<5, 1<<5)>>>(d_m1, d_m2, d_n, row, column);
cudaDeviceSynchronize();
cudaMemcpy(h_n2, d_n, row*column*sizeof(double), cudaMemcpyDeviceToHost);
if(matEqual(h_n1, h_n2, row, column))
printf("Matrices match.\n");
else
{
printf("Matrices don't match.\nResult on CPU:\n");
printMatrix(h_n1, row, column);
printf("Result on GPU:");
printMatrix(h_n2, row, column);
}
free(h_m1);
free(h_m2);
free(h_n1);
free(h_n2);
cudaFree(d_m1);
cudaFree(d_m2);
cudaFree(d_n);
cudaDeviceReset();
return 0;
}
views.py:
INSTALLED_APPS = [
'user',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxx'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
models.py:
from django.shortcuts import render,redirect
from user.models import *
from django.core.mail import send_mail
from user.forms import *
def contact(request):
if request.method == 'POST':
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
name = contact_form.cleaned_data['name']
message = contact_form.cleaned_data['message']
sender = contact_form.cleaned_data['sender']
phone = contact_form.cleaned_data['phone']
cc_myself = contact_form.cleaned_data['cc_myself']
recipients = ['xxxxx@gmail.com']
if cc_myself:
recipients.append(sender)
send_mail(name, message, sender, recipients)
contact_form.save()
print('Successful')
return redirect('contact')
else:
print('Fails')
else:
contact_form = ContactForm(request.POST)
context = {
"contact_form": contact_form,
}
template = 'contact.html'
return render(request, template, context)
contact.html:
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length=100)
message = models.TextField()
sender = models.EmailField()
phone = models.CharField(max_length=10)
cc_myself = models.BooleanField(blank=True)
time = models.DateTimeField(auto_now_add=True, db_index=True)
def __str__(self):
return 'Message for {}'.format(self.sender)
答案 3 :(得分:-1)
Django Setting就是诀窍!
Django在后台作业中发送电子邮件,如果要使用外部作业管理器,则必须正确设置此ENV VAR。