Use Django mail function built-in e-mail

Foreword

In Django will meet the needs of sending mail, Django built-in e-mail transmission function is defined in django.core.mailthe module. Send e-mail server requires SMTP, commonly used free server has: 163,126, QQ, e-mail below to 163, for example.

Thinking

Send e-mail using Django equivalent, prepared in advance an available email account, and set up the POP3 / the SMTP / IMAP . Then go to the mailbox to get an authorization code, Django log on to the mailbox by authorization code in the mail, and then specify one or more accounts to send mail through the mail.

Obtain authorization code

First, there must be a 163-mail account! Then log into select Settings POP3 ▶ / the SMTP / IMAP :

Then, as shown below, check the IMAP / SMTP services .

The client to enter an authorization code, select open .

Then, enter the phone verification procedures to obtain the verification code and enter, then click OK.

Keep in mind that your authorization code, it will only be shown on the page, but the current strategy is bound to give you a phone number to send the authorization code, if you forget, you get it back

Send Simple Mail example

This example can be applied to a message sent to one or more recipients. But not with accessories.

Configuration settings

Now, enter the Django settingsset:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True  # 是否使用TLS安全传输协议(用于在两个通信应用程序之间提供保密性和数据完整性。)
EMAIL_USE_SSL = False  # 是否使用SSL加密,qq企业邮箱要求使用
EMAIL_HOST = 'smtp.163.com'  # 发送邮件的邮箱 的 SMTP服务器,这里用了163邮箱
EMAIL_PORT = 25  # 发件箱的SMTP服务器端口
EMAIL_HOST_USER = '[email protected]'  # 发送邮件的邮箱地址
EMAIL_HOST_PASSWORD = 'xxxxxx'  # 发送邮件的邮箱密码(这里使用的是授权码)
EMAIL_TO_USER_LIST = ['[email protected]', '[email protected]']   # 此字段是可选的,用来配置收件人列表

注意,EMAIL_USE_TLSEMAIL_USE_SSL是互斥的,也就是说,不能同时为True

views配置

from django.core.mail import send_mail
def send_email(request):
    send_mail(
        subject='这里是邮件标题',
        message='这里是邮件内容',
        from_email='[email protected]',
        recipient_list=['[email protected]'],
        fail_silently=False
    )
    return HttpResponse('OK')

send_mail各参数如下:

  • subject,邮件标题
  • message,发送邮件内容
  • from_email,发件人
  • recipient_list,收件人,可以是多个
  • fail_silently,如果为False时,发送失败抛出错误

现在,有了上述这些关键代码之后,我们就可以向指定的邮箱发邮件了,如果收不到邮件,可有去看垃圾邮件中找找,可能被当成了垃圾邮件(我在qq邮箱中遇到这情况,解决办法是,点开这个邮件,点击我不是垃圾邮件,就好了)。

多封邮件发送多个收件人

有时候会遇到多封邮件同时发送多个人,怎么办呢?settings配置不变,修改views中代码即可:

from django.core.mail import send_mail, send_mass_mail
def send_email(request):
    message1 = ('邮件标题1', '内容1', '[email protected]', ['[email protected]'])
    message2 = ('邮件标题2', '内容2', '[email protected]', ['[email protected]'])
    send_mass_mail((message1, message2), fail_silently=False)
    return HttpResponse('OK')

当发送多封邮件时,就要使用send_mass_mail了。上例中的列表内,也可以填写多个收件人。

这里简要说下send_mailsend_mass_mail的区别:

send_mail每次发邮件都会建立一个连接,发多封邮件时建立多个连接。而 send_mass_mail 是建立单个连接发送多封邮件,所以一次性发送多封邮件时 send_mass_mail 要优于 send_mail

发送带附件的邮件

这里需要导入EmailMessage来完成。

from django.shortcuts import render, HttpResponse
from django.core.mail import EmailMessage
def send_email(request):
   msg = EmailMessage(
        subject='这是带附件的邮件标题',
        body='这是带附件的邮件内容',
        from_email='[email protected]',  # 也可以从settings中获取
        to=['[email protected]']
    )
    msg.attach_file('t2.xls')
    msg.send(fail_silently=False)
    return HttpResponse('OK')

上例中,attach_file使用当前文件系统下的某个文件做为附件。调用时,传入某个文件的完整路径,以及该附件的MIME类型(可选的)。 忽略MIME类型的话,Django会自动根据附件文件名来推测MIME类型。最简单的用法如下:

message.attach_file('/images/weather_map.png')

另外的,我们还可以使用attach传递三个参数:filenamecontent 和 mimetype. filename 是出现在邮件中的附件文件的名称,content 是附件的内容,而 mimetype 是附件所使用的MIME类型。 如果忽略 mimetype, Django会自动根据附件文件名来推测MIME内容类型。
例如:

message.attach('design.png', img_data, 'image/png')

see also:django实现发送邮件功能 | Django 发送邮件 | Django1.4版本文档关于发邮件部分

Guess you like

Origin www.cnblogs.com/sundawei7/p/11949239.html