下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
入门例子
from django.core.mail import send_mail send_mail(u'邮件标题',u'邮件内容','[email protected]',['[email protected]'],fail_silently=False)
send_mail()
send_mail( subject, message, from_email, recipient_list, fail_silently=False, auth_user=None,auth_password=None, connection=None)- subject: 字符串,表示邮件标题。
- message: 字符串,表示邮件内容。
- from_email: 字符串,表示发件邮箱。
- recipient_list: 字符串列表,列表中每个成员都是一个邮箱地址,而且每个收件人都会在 “收件人/To:” 栏看到出现在 recipient_list 中的其他收件人。
- fail_silently: (可选)布尔值。为 False 时, send_mail 会抛出 smtplib.SMTPException 异常。smtplib 文档列出了所有可能的异常。 这些异常都是 SMTPException 的子类。
- auth_user: (可选)SMTP服务器的认证用户名。没提供该参数的情况下,Django会使用 EMAIL_HOST_USER 配置项的设置。
- auth_password: (可选)SMTP服务器的认证密码,没提供该参数的情况下,Django会使用EMAIL_HOST_PASSWORD 配置项的设置。
- connection: (可选)发送邮件的后端。没提供该参数的情况下,Django会使用默认后端的实例。可查看 Email backends 了解更多细节。
send_mass_mail()
send_mass_mail( datatuple, auth_password=None,connection=None)(subject,message,from_email,recipient_list)
message1 = ('Subject here','Here is the message','[email protected]',['[email protected]','[email protected]']) message2 = ('Another Subject','Here is another message','[email protected]',['[email protected]']) send_mass_mail((message1,message2),fail_silently=False)
mail_admins()
mail_admins( subject, connection=None, html_message=None)
Changed in Django 1.3:
Please see the release notes
例子
send_mail('Subject','Message.','[email protected]',['[email protected]','[email protected]'])
datatuple = ( ('Subject','[email protected]',['[email protected]']),('Subject','[email protected]',['[email protected]']),) send_mass_mail(datatuple)
防止邮件头注入
from django.core.mail import send_mail,BadHeaderError def send_email(request): subject = request.POST.get('subject','') message = request.POST.get('message','') from_email = request.POST.get('from_email','') if subject and message and from_email: try: send_mail(subject,['[email protected]']) except BadHeaderError: return HttpResponse('Invalid header found.') return HttpResponseRedirect('/contact/thanks/') else: # In reality we'd use a form class # to get proper validation errors. return HttpResponse('Make sure all fields are entered and valid.')
EmailMessage类
EmailMessage 对象
class EmailMessage
Changed in Django 1.3: 加入了 cc 参数(cc是抄送)
- subject: 邮件的标题行
- body: 邮件的主体内容文本,须是纯文本信息。
- from_email: 发送者的地址。 [email protected] 或 Fred <[email protected]> 格式都是合法的。如果忽略该参数,Django就会使用 DEFAULT_FROM_EMAIL 配置项。
- to: 收件人地址列表或元组。
- bcc: 发送邮件时用于”Bcc”头信息的一组列表或元组,也就是暗送的收件人。
- connection: 一个邮件后端实例。用同一个链接发送多封邮件就要用到该参数。忽略该参数时,会在调用 send()时自动创建一个新链接。
- attachments: 置于邮件报文内的附件列表。列表元素可以是 ``email.MIMEBase.MIMEBase`` 实例,也可以是(filename, content, mimetype) 三部分构成的元组。
- headers: 置于邮件报文内的其他头信息(header)的字典。字典的key是头信息的名称,字典的value是头信息的值。 这样做能确保头信息的名称和对应值会以正确的格式保存于邮件报文中。
- cc: 发送邮件时放于”Cc”头信息的一系列列表或元组。
email = EmailMessage('Hello','Body goes here','[email protected]',['[email protected]','[email protected]'],['[email protected]'],headers = {'Reply-To': '[email protected]'})
-
-
message.attach('design.png',img_data,'image/png')
-
message.attach_file('/images/weather_map.png')
发送多用途邮件
from django.core.mail import EmailMultiAlternatives subject,to = 'hello','[email protected]','[email protected]' text_content = 'This is an important message.' html_content = '<p>This is an <strong>important</strong> message.</p>' msg = EmailMultiAlternatives(subject,text_content,[to]) msg.attach_alternative(html_content,"text/html") msg.send()
msg = EmailMessage(subject,html_content,[to]) msg.content_subtype = "html" # 主内体现在变成 text/html msg.send()
Email backends 邮件发送后端
New in Django 1.2:
Please see the release notes
- open() 实例化一个长生命周期的邮件发送链接。
- close() 关闭当前邮件发送链接。
- send_messages(email_messages) 发送一组 EmailMessage 对象的列表。如果链接(connection)还未打开,该调用会隐性地打开链接,发完邮件后就关闭该链接。 如果该链接已经打开,那么它会保持打开状态直至邮件发送完毕。
获取邮件发送后端的实例
get_connection( backend=None, *args, **kwargs)发送多封邮件
from django.core import mail connection = mail.get_connection() # 使用默认邮件链接(connection) messages = get_notification_email() connection.send_messages(messages)
from django.core import mail connection = mail.get_connection() # 手动打开链接(connection) connection.open() # 使用该链接构造一个邮件报文 email1 = mail.EmailMessage('Hello','[email protected]',['[email protected]'],connection=connection) email1.send() # 发送邮件 # 构造其他两个报文 email2 = mail.EmailMessage('Hello','[email protected]',['[email protected]']) email3 = mail.EmailMessage('Hello','[email protected]',['[email protected]']) # 在一个调用中发送两封邮件 connection.send_messages([email2,email3]) # 链接已打开,因此 send_messages() 不会关闭链接 # 要手动关闭链接 connection.close()
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。