mail实用程序发送消息,但django没有

时间:2017-07-04 13:01:34

标签: django email smtp sendmail centos6

我有一些问题。我想通过django应用程序发送电子邮件,但我不能:“smtplib.SMTPRecipientsRefused:{'':( 451,'临时本地问题 - 请稍后再试')}

所以,我试图通过邮件实用程序发送:

 echo -ne "Some test body letter" | mail -s "Test subject" '<my email>'

它会在我的电子邮件地址发送电子邮件。

但以下脚本打印错误消息:

#! /usr/bin/python2.7 
# -*- coding: utf-8 -*-
import os
import time
import urllib2
import django
from datetime import datetime

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
os.environ['EMAIL_BACKEND'] = 'django.core.mail.backends.smtp.EmailBackend'
os.environ['EMAIL_HOST'] = 'localhost'
django.setup()

from django.core.mail import EmailMultiAlternatives
from email.MIMEImage import MIMEImage

import time
import sys
import datetime 
from django.template.loader import get_template
from django.template import Context

from django.core.mail import send_mail

import smtplib


subject, from_email, to = 'Test subject', 'root@<server hostname>', '<my email>'

html_content = "Some test body letter"

print "Attempt 1. smtplib"
try:
    s = smtplib.SMTP('localhost')
    s.sendmail(from_email, [to], html_content)
    s.quit()
except Exception as e:
    print "ERROR: %s" % str(e)

print "Attempt 2. send_mail"
try:
    send_mail(
        subject,
        html_content,
        from_email,
        [to],
        fail_silently=False,
    )
except Exception as e:
    print "ERROR: %s" % str(e)


print "Attempt 3. EmailMultiAlternatives"
try:
    msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()
except Exception as e:
    print "ERROR: %s" % str(e)

输出:

Attempt 1. smtplib
ERROR: {'<my email>': (451, 'Temporary local problem - please try later')}
Attempt 2. send_mail
ERROR: {u'<my email>': (451, 'Temporary local problem - please try later')}
Attempt 3. EmailMultiAlternatives
ERROR: {u'<my email>': (451, 'Temporary local problem - please try later')}

请帮帮我!!!

Server CentOS 6,Django --- 1.9。

更新 修理它。只需安装postfix并为其更改端口即可完成所有操作。 有source

0 个答案:

没有答案