2013-10-22 91 views
0

我有這樣我可以從一個網絡服務器與任何發件人姓名從Web服務器發送電子郵件 - Django的蟒蛇

這種發送電子郵件的配置我的settings.py困難是我做了什麼:

EMAIL_USE_TLS = True 
EMAIL_HOST = 'mail.wservices.ch' 
HOSTNAME = 'localhost' 
DEFAULT_FROM_EMAIL = '[email protected]' 

和發送電子郵件這樣的:

html_content = render_to_string('htmlmail.html', {}) 
text_content = strip_tags(html_content) 
msg = EmailMultiAlternatives('subject!',text_content,'[email protected]',['[email protected]']) 
msg.attach_alternative(html_content, "text/html") 
msg.send() 

但我得到:

{('[email protected]': (554, '5.7.1 <[email protected]>: 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 
+0

有您填寫[的文檔(HTTPS中所列出的所有設置://docs.djangoproject。 COM/EN /開發/主題/電子郵件/#快速的例子)?例子下的那些。 –

+0

@ kroolik,事情是我不知道郵件服務器的端口。 – doniyor

+0

@kroolik,我會在第二天發佈站長的回答 – doniyor

回答

4
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='[email protected]' 
    SERVER_EMAIL='[email protected]' 
    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, 
      ['[email protected]'],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 
+0

你測試過嗎? – doniyor

+0

是的,那我在Ubuntu上使用,它工作正常, – 2013-10-22 23:59:08

+0

在setting.py添加此:從django.core.mail導入send_mail在頂部 – 2013-10-23 00:01:13