在Django中通过Gmail发送电子邮件

作者:我就是个世界 发表于:2011-11-13
While I was learning how to use django-registration app, I discover in the tutorial that I need to know how to send an email for the user to activate the registration.
我找到了这个指南,教我学习如何去使用 django-registration app,如何为用户发送电子邮件,以激活注册。
Thanks to folk at the Django chatroom, they advice me to google 'django gmail' and I discover a post at nathanostgard.com that show roughly how to do it. Here I'm going to extend a little on how to test it using shell.

1. Create a project, 创建一个项目[separator]
[code]django-admin.py startproject gmail[/code]
2. Edit settings.py with code below: 在settings.py 中添加如下代码
[code]
    EMAIL_USE_TLS = True
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_HOST_USER = 'youremail@gmail.com'
    EMAIL_HOST_PASSWORD = 'yourpassword'
    EMAIL_PORT = 587
[/code]
3. Run interactive mode, 运行交互模式
[code]python manage.py shell[/code]
4. Import the EmailMessage module, 导入EmailMessage模块
    [code]from django.core.mail import EmailMessage[/code]
5. Send the email, 发送邮件测试
[code]
    email = EmailMessage('Subject', 'Body', t=['mickeyckm@mangoorange.com'])
    email.save()
[/code]

Hope it help someone :) Reference [url=http://ltslashgt.com/2007/07/02/gmail-and-django/]here[/url] and [url=http://www.mangoorange.com/2008/09/15/sending-email-via-gmail-in-django/]here[/url].
希望能帮助别人, :)  参考资料: [url=http://ltslashgt.com/2007/07/02/gmail-and-django/]here[/url] and [url=http://www.mangoorange.com/2008/09/15/sending-email-via-gmail-in-django/]here[/url].

分享:

扫一扫在手机阅读、分享本文

请发表您的评论