Djano发送邮件
发布时间:2020-05-24 23:21:38 所属栏目:Python 来源:互联网
导读:Djano发送邮件
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 入门例子from django.core.mail import send_mail send_mail(u'邮件标题',u'邮件内容','[emailprotected]',['[emailprotected]'],fail_silently=False) send_mail()send_mail( subject, message, from_email, recipient_list, fail_silently=False, auth_user=None,auth_password=None, connection=None)
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','[emailprotected]',['[emailprotected]','[emailprotected]'])
message2 = ('Another Subject','Here is another message','[emailprotected]',['[emailprotected]'])
send_mass_mail((message1,message2),fail_silently=False)
send_mass_mail() vs. send_mail()mail_admins()mail_admins( subject, connection=None, html_message=None) Changed in Django 1.3: Please see the release notesmail_managers()mail_managers( subject, html_message=None)例子send_mail('Subject','Message.','[emailprotected]',['[emailprotected]','[emailprotected]'])
datatuple = (
('Subject','[emailprotected]',['[emailprotected]']),('Subject','[emailprotected]',['[emailprotected]']),)
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,['[emailprotected]'])
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是抄送)
email = EmailMessage('Hello','Body goes here','[emailprotected]',['[emailprotected]','[emailprotected]'],['[emailprotected]'],headers = {'Reply-To': '[emailprotected]'})
发送多用途邮件from django.core.mail import EmailMultiAlternatives subject,to = 'hello','[emailprotected]','[emailprotected]' 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
获取邮件发送后端的实例get_connection( backend=None, *args, **kwargs)SMTP backendEMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' Console backendEMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' File backendEMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' EMAIL_FILE_PATH = '/tmp/app-messages' # 将其改为本地的存放目录 In-memory backendEMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' Dummy backendEMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend' 自定义邮件发送后端发送多封邮件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','[emailprotected]',['[emailprotected]'],connection=connection)
email1.send() # 发送邮件
# 构造其他两个报文
email2 = mail.EmailMessage('Hello','[emailprotected]',['[emailprotected]'])
email3 = mail.EmailMessage('Hello','[emailprotected]',['[emailprotected]'])
# 在一个调用中发送两封邮件
connection.send_messages([email2,email3])
# 链接已打开,因此 send_messages() 不会关闭链接
# 要手动关闭链接
connection.close()
测试邮件发送python -m smtpd -n -c DebuggingServer localhost:1025 SMTPConnectionclass SMTPConnection Deprecated in Django 1.2.以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
