我在终端中编写了此代码以测试在 Django 中发送的消息,但出现错误并且找不到解决方案...:
from le_site.models import *
import smtplib
send_mail('Subject here', 'Here is the message.','name@yahoo.fr',['name@yahoo.fr'],fail_silently=False)
Run Code Online (Sandbox Code Playgroud)
错误(例外):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 56, in send_mail
fail_silently=fail_silently)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 37, in get_connection
klass = import_string(backend or settings.EMAIL_BACKEND)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 40, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting
EMAIL_BACKEND, but settings are not configured. You must either define
the environment variable DJANGO_SETTINGS_MODULE or call …Run Code Online (Sandbox Code Playgroud) 我正在使用allauth版本,0.35.0并且我想覆盖电子邮件模板以为其创建所需的HTML模板。allauth登录和注册等页面没有问题,但是我找不到任何电子邮件模板。
这只是.txtpath中的一个文件/templates/account/email/。但是,如何HTML为allauth诸如更改密码等任务的电子邮件设置消息?
任何帮助将不胜感激。
我目前正在阅读一本关于 Django 的书(针对初学者),并且我需要使用SendGrid SMTP Relay 对测试网站实现密码重置功能。
创建帐户并开始创建 SMTP 中继后,我看到以下屏幕:
基于此页面,我将以下代码行添加到我的setting.py文件中:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'xxxxx' #the string which is partialy hidden under the pink square
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Run Code Online (Sandbox Code Playgroud)
运行网站并尝试重置我的密码(超级用户的密码)后,我在控制台中收到带有重置链接的所需消息,但遗憾的是我的电子邮件没有收到任何消息。因此,我在尝试验证集成时收到以下错误消息。

到目前为止我尝试过的:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'我尝试从文件中删除settings.py(这只会让事情变得更糟)我已经尝试解决这个问题一天多了,任何帮助将不胜感激!
在Django EmailMultiAlternatives中有没有办法可以发送到多个"到"地址,而无需再次发送?
尝试用逗号分隔元组中的电子邮件,但只有第一个收件人收到电子邮件.
设定:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'donotreply@kamiltrojnar.pl'
EMAIL_HOST_PASSWORD = 'passwordsecret'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'donotreply@kamiltrojnar.pl'
SITE_ID = 2
Run Code Online (Sandbox Code Playgroud)
在控制台中输出:
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Testing email sending
From: donotreply@kamiltrojnar.pl
To: kamil.trojnar@gmail.com
Date: Tue, 13 Nov 2018 17:20:27 -0000
Message-ID <154212962720.11272.17395367179025062268@DESKTOP-EG7NCN4.home>
Run Code Online (Sandbox Code Playgroud)
视图:
def index(request):
send_mail("Testing email sending", 'AS title','donotreply@kamiltrojnar.pl',['kamil.trojnar@gmail.com'], fail_silently=False)
return render(request, 'userpanel/index.html')
Run Code Online (Sandbox Code Playgroud)
我没有收到任何错误,但是没有发送电子邮件。可能是什么问题?
如何处理模板标记 {% %} 内的变量,我需要为我的函数 send_mail_view() 提供参数
urlpatterns = [
path('send_mail_view/<sender_email>/<receiver_email>/<doc>',views.send_mail_view, name='send_mail_view'),
Run Code Online (Sandbox Code Playgroud)
]
def send_mail_view(request,sender_email,receiver_email,doc):
email=EmailMessage( 'i want to hire you Nitish ', 'please provide your resume', sender_email, [receiver_email] )
email.attach_file('mailing_app/179.pdf')
res=email.send(fail_silently=False)
success_msg="mail sent success fully"
return render(request,'mailing_app/success.html',{'success_msg':success_msg})
Run Code Online (Sandbox Code Playgroud)
def student_main_view(request,username):
user_obj=User.objects.filter(username=username)
pdf_student_model_obj=pdf_student_model.objects.filter(username__in=user_obj)
pdf_list=[]
for p in pdf_student_model_obj:
print("nikseva",type(p.pdf_id))
pdf_indexing_model_obj=pdf_indexing_model.objects.filter(pdf_id=str(p.pdf_id))
for x in pdf_indexing_model_obj:
pdf_list.append({'pdf_id':x.pdf_id,'pdf_title':x.pdf_title,'pdf_abstract':x.pdf_abstract,'pdf_path':x.pdf_path,'sender_email':"n@gmail.com",'receiver_email':username,})
print("pdf_list",pdf_list)
return render(request,'rnd_app/student_mainpage.html',{'pdf_list':pdf_list})
Run Code Online (Sandbox Code Playgroud)
{% for z in pdf_list %}
pdf_title :{{z.pdf_title}}
pdf_abstract:{{z.pdf_abstract}}<br>
pdf_path:{{z.pdf_path}}
<a href="{% url 'mailing_app:send_mail_view' {{request.user.username}} {{z.receiver_email}} {{z.pdf_path}} %}" class="btn1 ">Send …Run Code Online (Sandbox Code Playgroud) SendGrid 提供了将unique_args与电子邮件一起传递的功能,以便在事件 webhook 中识别电子邮件。 但问题是我无法弄清楚如何在 Django 中通过电子邮件发送这些 unique_args。这就是我目前尝试这样做的方式:
from django.core.mail import EmailMultiAlternatives
header ={
"unique_args": {
"customerAccountNumber": "55555",
"activationAttempt": "1",
"New Argument 1": "New Value 1",
"New Argument 2": "New Value 2",
"New Argument 3": "New Value 3",
"New Argument 4": "New Value 4"
}
}
subject, from_email, to = 'hello', 'EXAMPLE@FROM.com', 'EXAMPLE@TO.NET'
text_content = 'This is an important message.'
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to,],
headers=header,
)
msg.send()
Run Code Online (Sandbox Code Playgroud) 我有一个使用Django构建的网站,我遇到了以下难题.我有一封后端电子邮件,我们称之为luckyducky@gmail.com.现在我还有一个域名电子邮件,直接转发到luckyduck@gmail.com,名为support@luckyducky.com.
目前我的Django Email设置如下:
EMAIL_HOST_USER="luckyducky@gmail.com"
EMAIL_HOST_PASSWORD="password"
Run Code Online (Sandbox Code Playgroud)
这很好,但我希望从'support@luckyducky.com'发送电子邮件.
包括Python社区作为一个整体,也许问题可以在Django邮件包装器函数之外解决.
如何设置此重定向?这意味着,当我在Django后端配置的真正电子邮件是luckyducky@gmail.com时,如何在别名"support@luckyducky.com"下发送电子邮件?
我需要在我的主页底部嵌入一个联系表单,这是一个单页应用程序。我遵循了几个关于如何在另一个页面(例如,myportfolio.com/contact)上创建自己的联系表单的教程,这些教程很成功。不幸的是,我无法调整这些结果或找到其他教程,了解如何使该联系表单在显示所有其他模型数据的同一页面上可见并起作用(例如,myportfolio.com)。
我遵循了本教程:https://atsoftware.de/2015/02/django-contact-form-full-tutorial-custom-example-in-django-1-7。我只创建了一个应用程序。
我觉得这应该是一件很常见的事情,这让我觉得我忽略了一些显而易见的事情。
应用程序中的views.py
from django.views.generic import TemplateView
from portfolio.models import Experience
class HomePage(TemplateView):
template_name = 'portfolio/base.html'
def get_context_data(self, *args, **kwargs):
context = super(HomePage, self).get_context_data(*args, **kwargs)
context['experience_list'] = Experience.objects.order_by('-start_date')
return context
Run Code Online (Sandbox Code Playgroud)
contact_form.html
{% extends 'portfolio/base.html' %}
{% block contact %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
现在 contact_form.html 进入portfolio/base.html,如下所示:
基本.html
{# {% include "contact_form/contact_form.html" %}#}
{% block contact %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,什么也没有显示。但是,如果我注释掉第二行并删除第一行中的注释,则会出现提交按钮。
现在我意识到我假设 django-contact-form 将获取此表单并知道如何处理它,就像在 myportflio.com/contact 场景中可以访问它一样。但是,我知道之前曾通过 urls.py 文件引用 …
django django-templates django-forms django-email contact-form
这个问题在这里有一些元素,但没有最终答案。
有使用easy_pdf生成PDF的视图
from easy_pdf.views import PDFTemplateResponseMixin
class PostPDFDetailView(PDFTemplateResponseMixin,DetailView):
model = models.Post
template_name = 'post/post_pdf.html'
Run Code Online (Sandbox Code Playgroud)
然后,我想将此生成的 PDF 附加到以下电子邮件中:
@receiver(post_save, sender=Post)
def first_mail(sender, instance, **kwargs):
if kwargs['created']:
user_email = instance.client.email
subject, from_email, to = 'New account', 'contact@example.com', user_email
post_id = str(instance.id)
domain = Site.objects.get_current().domain
post_pdf = domain + '/post/' + post_id + '.pdf'
text_content = render_to_string('post/mail_post.txt')
html_content = render_to_string('post/mail_post.html')
# create the email, and attach the HTML version as well.
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.attach_file(post_pdf, 'application/pdf') …Run Code Online (Sandbox Code Playgroud) django-email ×10
django ×9
python ×6
sendgrid ×2
contact-form ×1
django-forms ×1
django-views ×1
email ×1
mailgun ×1
pdf ×1
smtp ×1