Djano发送邮件
发布时间:2020-12-17 17:29:22 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 入门例子 from django.core.mail import send_mailsend_mail(u'邮件标题',u'邮件内容','[email?protected]',['[email?protected]'],fail_silently=Fal
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 入门例子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)
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) 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 notes
mail_managers()mail_managers( subject,? html_message=None)例子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是抄送)
email = EmailMessage('Hello','Body goes here','[email?protected]',['[email?protected]','[email?protected]'],['[email?protected]'],headers = {'Reply-To': '[email?protected]'})
发送多用途邮件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
获取邮件发送后端的实例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','[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() 测试邮件发送python -m smtpd -n -c DebuggingServer localhost:1025 SMTPConnectionclass? SMTPConnection
Deprecated in Django 1.2.
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |