如何通过Django发送电子邮件? [英] How to send email via Django?

查看:159
本文介绍了如何通过Django发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 settings.py 中,我有以下内容:

  EMAIL_BACKEND ='django.core.mail.backends.smtp.EmailBackend'

#发送电子邮件的主机。
EMAIL_HOST ='localhost'

#发送电子邮件的端口。
EMAIL_PORT = 1025

#EMAIL_HOST的可选SMTP验证信息。
EMAIL_HOST_USER =''
EMAIL_HOST_PASSWORD =''
EMAIL_USE_TLS = False

我的电子邮件程式码:

 从django.core.mail导入EmailMessage 
email = EmailMessage('Hello' 'World',to = ['user@gmail.com']
email.send()

当然,如果我通过 python -m smtpd -n -c DebuggingServer localhost:1025 设置调试服务器,我可以在我的终端上看到这个电子邮件。 / p>

但是,我如何实际将电子邮件发送到调试服务器,而是发送到user@gmail.com?



阅读你的答案后,让我得到一些直线:


  1. 你不能使用localhost(简单的ubuntu pc)发送电子邮件?


  2. 我以为在django 1.3 send_mail()有些不赞成, EmailMessage.send()代替?



解决方案

发送电子邮件t一个真正的SMTP服务器。如果你不想自己设置,那么你可以找到一些可以为你自己运行的公司,比如Google自己。


In my settings.py, I have the following:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

# Host for sending e-mail.
EMAIL_HOST = 'localhost'

# Port for sending e-mail.
EMAIL_PORT = 1025

# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

My email code:

from django.core.mail import EmailMessage
email = EmailMessage('Hello', 'World', to=['user@gmail.com'])
email.send()

Of course, if I setup a debugging server via python -m smtpd -n -c DebuggingServer localhost:1025, I can see the email in my terminal.

However, how do I actually send the email not to the debugging server but to user@gmail.com?

After reading your answers, let me get something straight:

  1. Can't you use localhost(simple ubuntu pc) to send e-mails?

  2. I thought in django 1.3 send_mail() is somewhat deprecated and EmailMessage.send() is used instead?

解决方案

Send the email to a real SMTP server. If you don't want to set up your own then you can find companies that will run one for you, such as Google themselves.

这篇关于如何通过Django发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆