3

I am having difficulties in configuring my settings.py so that I can send email from a webserver with any sender name

This is what I have done:

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

And sending email like this:

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()

But I am getting:

{('[email protected]': (554, '5.7.1 <[email protected]>: Relay access denied')}

In one function, I have two msg.send() calls, BTW.

What am I doing wrong?

this is the answer webmaster when i asked how to send mails from webserver programmatically:

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
4
  • Have you filled all the settings listed inside the docs? The ones right below the example. Commented Oct 22, 2013 at 22:41
  • @kroolik, the thing is i dont know the port of mail server. Commented Oct 22, 2013 at 22:46
  • @kroolik, i will post the answer of webmaster in a second Commented Oct 22, 2013 at 22:48
  • Look for such info on your mail provider's site. Or try smtp <provider name> google query. Commented Oct 22, 2013 at 22:48

1 Answer 1

7
 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, 
            ['[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
Sign up to request clarification or add additional context in comments.

1 Comment

in the setting.py add this: from django.core.mail import send_mail at the top

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.