您的位置:首页 > 编程语言 > Go语言

django1.62发送邮件(个人总结)

2014-04-23 10:55 441 查看

第一步:如何设置settings文件?

首先先翻阅一下django1.6关于邮件的官方文档:网址:https://docs.djangoproject.com/en/1.6/ref/settings/总结:首先是配置settings,参考上面的官方文档:
#邮件配置
EMAIL_HOST = 'smtp.gmail.com'          #SMTP地址
EMAIL_PORT = 25                        #SMTP端口
EMAIL_HOST_USER = 'xxx@gmail.com'      #我自己的邮箱
EMAIL_HOST_PASSWORD = '******'         #我的邮箱密码
EMAIL_USE_TLS = True                   #与SMTP服务器通信时,是否启动TLS链接(安全链接)。默认是false

第二步:如何写View文件?

也是参考django的官网文档网址:https://docs.djangoproject.com/en/1.6/topics/email/

(1)简单的方式发送邮件

最简单的发送邮件方式:
1
from
 
django.core.mail 
import
 
send_mail
2
send_mail(
'subject'
,                  
#主题
3
          
'body'
,                     
#邮件内容
4
          
'xxxx@gmail.com'
,
          
#发件人,可以是英文名
5
          
[
'xxx@xxx,
xxx@xxx'
],
      
#收件人,可以是多个
6
          
fail_silently
=
True
          
#发送异常时不提示
7
          
)
官方例子:from 
django.core.mail 
import
 
send_mail
from
 
django.core.mail 
import
 send_mass_mail[/code]
send_mail('Subject', 'Message.', 'from@example.com',['john@example.com', 'jane@example.com'])
datatuple = (('Subject', 'Message.', 'from@example.com', ['john@example.com']),('Subject', 'Message.', 'from@example.com', ['jane@example.com']),)send_mass_mail(datatuple)

(2)通过EmailMessage发送邮件

通过EmailMessage不仅可以发送邮件,还可以发送html超文本邮件和携带附件。通过EmailMessage发送html超文本邮件:
# -*- coding: utf-8 -*-from django.core.mail import EmailMessagefrom django.template import loaderdef send_html_mail(request):html_content = loader.render_to_string("template.html", #需要渲染的html模板{'paramters':paramters} #需要传给模板的参数)subject=u'邮件主题' #主题recipient_list=['xxx@xxx','xxx@xxx'] #收件人列表sender='xxxx@gmail.com' #发件人,可以是英文名msg = EmailMessage(subject, html_content, sender, recipient_list)msg.content_subtype = "html" # Main content is now text/htmlmsg.attach_file('/images/a.png')msg.send()
另外也可以通过EmailMultiAlternatives 实现,官方例子:
from django.core.mail import EmailMultiAlternativessubject, from_email, to = 'hello', 'from@example.com', 'to@example.com'text_content = 'This is an important message.'html_content = '<p>This is an <strong>important</strong> message.</p>'msg = EmailMultiAlternatives(subject, text_content, from_email, [to])msg.attach_alternative(html_content, "text/html")msg.send()
msg = EmailMessage(subject, html_content, from_email, [to])msg.content_subtype = "html"  # Main content is now text/htmlmsg.send()
提示:声明html超文本有2种方式,如例子所示另外,如果使用模板,请看下面例子:
from django.core.mail import EmailMultiAlternativesfrom django.template import Context, loadert = loader.get_template('template.html')mail_list = ['xxx@xxx','xxx@xxx']user= 'xxx@xxx'context = {'paramters':paramters}subject, from_email, to = title, user, mail_listhtml_content = t.render(Context(context))msg = EmailMultiAlternatives(subject, html_content, from_email, to)msg.attach_alternative(html_content, "text/html")msg.attach_file(u'D:/My Documents/Python/doc/test.doc') # 添加附件发送msg.send() 

(3)通过Email Backends发送邮件

使用场合:配置了多个发送邮件的用户名和密码,现在需要用我指定的用户名和密码给用户发送邮件例子:
from django.core import mailconnection = mail.get_connection()   # Use default email connectionmessages = get_notification_email()connection.send_messages(messages)
from django.core import mailconnection = mail.get_connection()# Manually open the connectionconnection.open()# Construct an email message that uses the connectionemail1 = mail.EmailMessage('Hello', 'Body goes here', 'from@example.com',['to1@example.com'], connection=connection)email1.send() # Send the email# Construct two more messagesemail2 = mail.EmailMessage('Hello', 'Body goes here', 'from@example.com',['to2@example.com'])email3 = mail.EmailMessage('Hello', 'Body goes here', 'from@example.com',['to3@example.com'])# Send the two emails in a single call -connection.send_messages([email2, email3])# The connection was already open so send_messages() doesn't close it.# We need to manually close the connection.connection.close()
更多例子请参考:http://blog.csdn.net/ypq5566/article/details/24293147
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: