django msg.send()不能正常工作

时间:2011-04-12 11:01:26

标签: python django

当我通过django视图发送邮件时(下面的代码):

subject = "Audit report for all staff  "    
email_msg = "Hello Everyone," + '\n \n' + subject + '\n \n' + 'Thanks \n-Admin'
report_name = response['Content-Disposition'].split('=')[1]

msg = EmailMessage(
            subject = subject,
            body = email_msg,
            attachments=(('%s' % report_name,f.read(), 'text/csv'),),
            to = ['prateekm@localhost','prateek@gmail.com' ],
       )
    msg.send()

我的邮件仅发送到localhost电子邮件ID,而不是发送到gmail ID。如何纠正这个?

2 个答案:

答案 0 :(得分:0)

Django不会自动发送到远程服务器。它通过SMTP后端完成。你需要配置它。

http://docs.djangoproject.com/en/1.3/topics/email/#e-mail-backends

答案 1 :(得分:0)

要配置SMTP服务器,你必须在settings.py中提供类似的内容[已安装SMTP]

EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = '587'
EMAIL_HOST_USER = "xxx@gmail.com" #create a gmail id
EMAIL_HOST_PASSWORD = "xxxx"#pwd for that id
EMAIL_USE_TLS = True