gpt4 book ai didi

python - 从网络服务器发送电子邮件 - django python

转载 作者:行者123 更新时间:2023-11-28 21:57:40 27 4
gpt4 key购买 nike

我在配置 settings.py 时遇到困难,以便我可以使用任何发件人姓名从网络服务器发送电子邮件

这是我所做的:

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')}

在一个函数中,我有两个 msg.send() 调用,顺便说一句。

我做错了什么?

当我询问如何以编程方式从网络服务器发送邮件时,这是网站管理员的回答:

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

关于python - 从网络服务器发送电子邮件 - django python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19529641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com