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

标签 python django email

我在配置 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/

相关文章:

python - 使用 Python 测试 Adob​​e Analytics Instrumentation

python - PySide/PyQt 检测用户是否试图关闭窗口

java - 在python中设置区域设置编码

python - 从不带标题的 sqlite 中选择

django - 在 Django 模板中按类别列出产品

django - PyCharm + Python 3.6 + Django + 调试 + 生成器 == 痛苦的世界

php - 发送的电子邮件中显示的 HTML 代码

python - 将参数转换为字符串

python - 通过带有 excel xlsx 附件的 python 发送电子邮件(gmail)时出错

Java 正则表达式电子邮件