从Web服务器发送电子邮件-django python
我在配置我的settings.py时遇到困难,无法从Web服务器发送任何发件人名称的电子邮件 这就是我所做的:从Web服务器发送电子邮件-django python,python,django,email,Python,Django,Email,我在配置我的settings.py时遇到困难,无法从Web服务器发送任何发件人名称的电子邮件 这就是我所做的: EMAIL_USE_TLS = True EMAIL_HOST = 'mail.wservices.ch' HOSTNAME = 'localhost' DEFAULT_FROM_EMAIL = 'info@domain.com' 并发送如下电子邮件: html_content = render_to_string('htmlmail.html', {}) text_content
EMAIL_USE_TLS = True
EMAIL_HOST = 'mail.wservices.ch'
HOSTNAME = 'localhost'
DEFAULT_FROM_EMAIL = 'info@domain.com'
并发送如下电子邮件:
html_content = render_to_string('htmlmail.html', {})
text_content = strip_tags(html_content)
msg = EmailMultiAlternatives('subject!',text_content,'info@domain.com',['to@domain.com'])
msg.attach_alternative(html_content, "text/html")
msg.send()
但我得到了:
{('to@domain.com': (554, '5.7.1 <to@domain.com>: Relay access denied')}
首先确保已正确安装django sendmail
$sudo apt get安装sendmail
在settings.py中:
从django.core.mail导入发送邮件
默认\u来自\u电子邮件=webmaster@localhost'
服务器\u电子邮件地址:root@localhost'
电子邮件\u主机='localhost'
电子邮件\u主机\u用户=“”
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
电子邮件端口=25#587
电子邮件\u使用\u TLS=错误
在views.py中:
从project.apps.contact导入联系人表单
def联系人说明(请求):
if request.method==“POST”:
表单=联系人表单(request.POST)
如果form.is_有效():
topic=form.cleaned_数据['topic']
message=表单。已清理的_数据['message']
sender=form.cleaned_data.get('sender','email_address'))
寄信(
话题,,
消息
发件人,
['myaddress@gmail.com'],失败_=False
)
#返回HttpResponseRedirect(反向('games.views.thanking',{},RequestContext(request)))
返回render_to_response('contact/thanky.html',{},RequestContext(request))#适合反向方法
其他:
form=ContactForm()
返回render_to_响应('contact.html',{'form':form},RequestContext(request))
contact.py:
从django将表单作为表单导入
从django.forms导入表单
主题选择=(
(“一般”、“一般查询”),
(‘游戏问题’,‘游戏问题’),
(‘建议’,‘建议’),
)
班级联络表(forms.Form):
topic=forms.ChoiceField(选项=topic\u选项)
发件人=forms.EmailField(必需=False)
message=forms.CharField(widget=forms.Textarea)
#这里的小部件将指定一个带有注释的表单,该表单使用更大的Textarea小部件,而不是默认的TextInput小部件。
def清洁_信息(自我):
message=self.cleaned_data.get('message','')
num_words=len(message.split())
如果num_words已经填充了里面列出的所有设置?示例正下方的内容。@kroolik,问题是我不知道邮件服务器的端口。@kroolik,我将在第二时间发布网站管理员的答案,在您的邮件提供商的网站上查找此类信息。或者尝试smtp
google query。在setting.py中添加以下内容:from django.core.mail import send_mail在顶部
It is possible to send mails from E-Mail-Server "mail.wservices.ch".I suggest to
use the local installed Mail-Server. Hostname: localhost
There you can set any sender name, they just have to exist.
https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
Make sure first you have properly install django-sendmail
$ sudo apt-get install sendmail
in the settings.py :
from django.core.mail import send_mail
DEFAULT_FROM_EMAIL='webmaster@localhost'
SERVER_EMAIL='root@localhost'
EMAIL_HOST = 'localhost'
EMAIL_HOST_USER=''
EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 25 #587
EMAIL_USE_TLS = False
in views.py:
from project.apps.contact import ContactForm
def contactnote(request):
if request.method=='POST':
form =ContactForm(request.POST)
if form.is_valid():
topic=form.cleaned_data['topic']
message=form.cleaned_data['message']
sender=form.cleaned_data.get('sender','email_address')
send_mail(
topic,
message,
sender,
['myaddress@gmail.com'],fail_silently=False
)
#return HttpResponseRedirect(reverse('games.views.thanks', {},RequestContext(request)))
return render_to_response('contact/thanks.html', {},RequestContext(request)) #good for the reverse method
else:
form=ContactForm()
return render_to_response('contact.html',{'form':form},RequestContext(request))
contact.py:
from django import forms as forms
from django.forms import Form
TOPIC_CHOICES=(
('general', 'General enquiry'),
('Gamebling problem','Gamebling problem'),
('suggestion','Suggestion'),
)
class ContactForm(forms.Form):
topic=forms.ChoiceField(choices=TOPIC_CHOICES)
sender=forms.EmailField(required=False)
message=forms.CharField(widget=forms.Textarea)
#the widget here would specify a form with a comment that uses a larger Textarea widget, rather than the default TextInput widget.
def clean_message(self):
message=self.cleaned_data.get('message','')
num_words=len(message.split())
if num_words <4:
raise forms.ValidationError("Not enough words!")
return message
Try it , this is a whole working example apps, modify it
to be send to to mailserver like a reply when it got an mail, very simple to modify it